fix: make reaction UI updates when reacting faster in 1-1 and closed groups

pull/2449/head
William Grant 3 years ago
parent fd7efe5b64
commit 07c56e79f2

@ -93,6 +93,7 @@ import {
} from '../session/apis/open_group_api/sogsv3/knownBlindedkeys'; } from '../session/apis/open_group_api/sogsv3/knownBlindedkeys';
import { sogsV3FetchPreviewAndSaveIt } from '../session/apis/open_group_api/sogsv3/sogsV3FetchFile'; import { sogsV3FetchPreviewAndSaveIt } from '../session/apis/open_group_api/sogsv3/sogsV3FetchFile';
import { Reaction } from '../types/Reaction'; import { Reaction } from '../types/Reaction';
import { handleMessageReaction } from '../util/reactions';
export class ConversationModel extends Backbone.Model<ConversationAttributes> { export class ConversationModel extends Backbone.Model<ConversationAttributes> {
public updateLastMessage: () => any; public updateLastMessage: () => any;
@ -736,7 +737,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
const chatMessagePrivate = new VisibleMessage(chatMessageParams); const chatMessagePrivate = new VisibleMessage(chatMessageParams);
await getMessageQueue().sendToPubKey(destinationPubkey, chatMessagePrivate); await getMessageQueue().sendToPubKey(destinationPubkey, chatMessagePrivate);
await handleMessageReaction(reaction, UserUtils.getOurPubKeyStrFromCache(), true);
return; return;
} }
@ -748,6 +749,7 @@ export class ConversationModel extends Backbone.Model<ConversationAttributes> {
}); });
// we need the return await so that errors are caught in the catch {} // we need the return await so that errors are caught in the catch {}
await getMessageQueue().sendToGroup(closedGroupVisibleMessage); await getMessageQueue().sendToGroup(closedGroupVisibleMessage);
await handleMessageReaction(reaction, UserUtils.getOurPubKeyStrFromCache(), true);
return; return;
} }

@ -321,7 +321,11 @@ async function handleSwarmMessage(
// this call has to be made inside the queueJob! // this call has to be made inside the queueJob!
// We handle reaction DataMessages separately // We handle reaction DataMessages separately
if (!msgModel.get('isPublic') && rawDataMessage.reaction) { if (!msgModel.get('isPublic') && rawDataMessage.reaction) {
await handleMessageReaction(rawDataMessage.reaction, msgModel.get('source')); await handleMessageReaction(
rawDataMessage.reaction,
msgModel.get('source'),
msgModel.get('source') === UserUtils.getOurPubKeyStrFromCache()
);
confirm(); confirm();
return; return;
} }

@ -54,7 +54,8 @@ describe('ReactionMessage', () => {
// Handling reaction // Handling reaction
const updatedMessage = await handleMessageReaction( const updatedMessage = await handleMessageReaction(
reaction as SignalService.DataMessage.IReaction, reaction as SignalService.DataMessage.IReaction,
ourNumber ourNumber,
true
); );
expect(updatedMessage?.get('reacts'), 'original message should have reacts').to.not.be expect(updatedMessage?.get('reacts'), 'original message should have reacts').to.not.be
@ -85,7 +86,8 @@ describe('ReactionMessage', () => {
// Handling reaction // Handling reaction
const updatedMessage = await handleMessageReaction( const updatedMessage = await handleMessageReaction(
reaction as SignalService.DataMessage.IReaction, reaction as SignalService.DataMessage.IReaction,
ourNumber ourNumber,
true
); );
expect(updatedMessage?.get('reacts'), 'original message reacts should be undefined').to.be expect(updatedMessage?.get('reacts'), 'original message reacts should be undefined').to.be

@ -124,7 +124,7 @@ export type ReactionList = Record<
count: number; count: number;
index: number; // relies on reactsIndex in the message model index: number; // relies on reactsIndex in the message model
senders: Array<string>; senders: Array<string>;
you?: boolean; // whether we are in the senders because sometimes we dont have the full list of senders yet. you: boolean; // whether we are in the senders list, used within 1-1 and closed groups for ignoring duplicate data messages, used within opengroups since we dont always have the full list of senders.
} }
>; >;

@ -139,7 +139,8 @@ export const sendMessageReaction = async (messageId: string, emoji: string) => {
*/ */
export const handleMessageReaction = async ( export const handleMessageReaction = async (
reaction: SignalService.DataMessage.IReaction, reaction: SignalService.DataMessage.IReaction,
sender: string sender: string,
you: boolean
) => { ) => {
if (!reaction.emoji) { if (!reaction.emoji) {
window?.log?.warn(`There is no emoji for the reaction ${reaction}.`); window?.log?.warn(`There is no emoji for the reaction ${reaction}.`);
@ -151,22 +152,27 @@ export const handleMessageReaction = async (
return; return;
} }
if (originalMessage.get('isPublic')) {
window.log.warn("handleMessageReaction() shouldn't be used in opengroups");
return;
}
const reacts: ReactionList = originalMessage.get('reacts') ?? {}; const reacts: ReactionList = originalMessage.get('reacts') ?? {};
reacts[reaction.emoji] = reacts[reaction.emoji] || { count: null, senders: [] }; reacts[reaction.emoji] = reacts[reaction.emoji] || { count: null, senders: [] };
const details = reacts[reaction.emoji] ?? {}; const details = reacts[reaction.emoji] ?? {};
const senders = details.senders; const senders = details.senders;
let count = details.count || 0; let count = details.count || 0;
window.log.info( if (originalMessage.get('isPublic')) {
`${sender} ${reaction.action === Action.REACT ? 'added' : 'removed'} a ${ window.log.warn("handleMessageReaction() shouldn't be used in opengroups");
reaction.emoji return;
} reaction` } else {
); if (details.you && senders.includes(sender)) {
if (reaction.action === Action.REACT) {
window.log.warn('Received duplicate message for your reaction. Ignoring it');
return;
} else {
details.you = false;
}
} else {
details.you = you;
}
}
switch (reaction.action) { switch (reaction.action) {
case Action.REACT: case Action.REACT:
@ -191,6 +197,7 @@ export const handleMessageReaction = async (
if (count > 0) { if (count > 0) {
reacts[reaction.emoji].count = count; reacts[reaction.emoji].count = count;
reacts[reaction.emoji].senders = details.senders; reacts[reaction.emoji].senders = details.senders;
reacts[reaction.emoji].you = details.you;
if (details && details.index === undefined) { if (details && details.index === undefined) {
reacts[reaction.emoji].index = originalMessage.get('reactsIndex') ?? 0; reacts[reaction.emoji].index = originalMessage.get('reactsIndex') ?? 0;
@ -206,6 +213,14 @@ export const handleMessageReaction = async (
}); });
await originalMessage.commit(); await originalMessage.commit();
if (!you) {
window.log.info(
`${sender} ${reaction.action === Action.REACT ? 'added' : 'removed'} a ${
reaction.emoji
} reaction`
);
}
return originalMessage; return originalMessage;
}; };

Loading…
Cancel
Save