From 44872ac9617035a0aadcb918d6ae6160bb8fd6c4 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Mon, 6 Dec 2021 09:22:44 -0800 Subject: [PATCH] handleReaction: don't fetch/save across an await boundary --- ts/models/messages.ts | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/ts/models/messages.ts b/ts/models/messages.ts index 253c271d87..d84ec5efb1 100644 --- a/ts/models/messages.ts +++ b/ts/models/messages.ts @@ -3145,9 +3145,7 @@ export class MessageModel extends window.Backbone.Model { return; } - const oldReactions = this.get('reactions') || []; - - let newReactions: typeof oldReactions; + const previousLength = (this.get('reactions') || []).length; if (reaction.get('source') === ReactionSource.FromThisDevice) { log.info( `handleReaction: sending reaction to ${this.idForLogging()} from this device` @@ -3165,11 +3163,14 @@ export class MessageModel extends window.Backbone.Model { ), }; - newReactions = reactionUtil.addOutgoingReaction( - oldReactions, + const reactions = reactionUtil.addOutgoingReaction( + this.get('reactions') || [], newReaction ); + this.set({ reactions }); } else { + const oldReactions = this.get('reactions') || []; + let reactions: Array; const oldReaction = oldReactions.find( re => re.fromId === reaction.get('fromId') ); @@ -3184,16 +3185,17 @@ export class MessageModel extends window.Backbone.Model { ); if (reaction.get('source') === ReactionSource.FromSync) { - newReactions = oldReactions.filter( + reactions = oldReactions.filter( re => re.fromId !== reaction.get('fromId') || re.timestamp > reaction.get('timestamp') ); } else { - newReactions = oldReactions.filter( + reactions = oldReactions.filter( re => re.fromId !== reaction.get('fromId') ); } + this.set({ reactions }); await window.Signal.Data.removeReactionFromConversation({ emoji: reaction.get('emoji'), @@ -3218,10 +3220,11 @@ export class MessageModel extends window.Backbone.Model { reactionToAdd = reaction.toJSON(); } - newReactions = oldReactions.filter( + reactions = oldReactions.filter( re => re.fromId !== reaction.get('fromId') ); - newReactions.push(reactionToAdd); + reactions.push(reactionToAdd); + this.set({ reactions }); if ( isOutgoing(this.attributes) && @@ -3242,12 +3245,11 @@ export class MessageModel extends window.Backbone.Model { } } - this.set({ reactions: newReactions }); - + const currentLength = (this.get('reactions') || []).length; log.info( 'handleReaction:', `Done processing reaction for message ${this.idForLogging()}.`, - `Went from ${oldReactions.length} to ${newReactions.length} reactions.` + `Went from ${previousLength} to ${currentLength} reactions.` ); if (reaction.get('source') === ReactionSource.FromThisDevice) {