Apply reactions optimistically

This commit is contained in:
Fedor Indutny 2021-05-13 12:10:20 -07:00 committed by Scott Nonnenberg
parent 18abe93022
commit 03eaa9eb3e
6 changed files with 54 additions and 33 deletions

View file

@ -9,6 +9,7 @@ import {
MessageModelCollectionType,
WhatIsThis,
MessageAttributesType,
ReactionModelType,
ConversationAttributesType,
VerificationOptions,
} from '../model-types.d';
@ -3067,6 +3068,11 @@ export class ConversationModel extends window.Backbone
fromSync: true,
});
// Apply reaction optimistically
const oldReaction = await window.Whisper.Reactions.onReaction(
reactionModel
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const destination = this.getSendTarget()!;
const recipients = this.getRecipients();
@ -3176,9 +3182,23 @@ export class ConversationModel extends window.Backbone
throw new Error('No successful delivery for reaction');
}
window.Whisper.Reactions.onReaction(reactionModel);
return result;
}).catch(() => {
let reverseReaction: ReactionModelType;
if (oldReaction) {
// Either restore old reaction
reverseReaction = window.Whisper.Reactions.add({
...oldReaction,
fromId: window.ConversationController.getOurConversationId(),
timestamp,
});
} else {
// Or remove a new one on failure
reverseReaction = reactionModel.clone();
reverseReaction.set('remove', !reverseReaction.get('remove'));
}
window.Whisper.Reactions.onReaction(reverseReaction);
});
}

View file

@ -5,6 +5,7 @@ import {
CustomError,
MessageAttributesType,
RetryOptions,
ReactionAttributesType,
ShallowChallengeError,
QuotedMessageType,
WhatIsThis,
@ -4084,9 +4085,9 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
async handleReaction(
reaction: typeof window.WhatIsThis,
shouldPersist = true
): Promise<void> {
): Promise<ReactionAttributesType | undefined> {
if (this.get('deletedForEveryone')) {
return;
return undefined;
}
// We allow you to react to messages with outgoing errors only if it has sent
@ -4095,7 +4096,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
this.hasErrors() &&
(this.isIncoming() || this.getMessagePropStatus() !== 'partial-sent')
) {
return;
return undefined;
}
const reactions = this.get('reactions') || [];
@ -4108,6 +4109,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
let reactionToRemove: Partial<ReactionType> | undefined;
let oldReaction: ReactionAttributesType | undefined;
if (reaction.get('remove')) {
window.log.info('Removing reaction for message', messageId);
const newReactions = reactions.filter(
@ -4137,9 +4139,7 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
newReactions.push(reaction.toJSON());
this.set({ reactions: newReactions });
const oldReaction = reactions.find(
re => re.fromId === reaction.get('fromId')
);
oldReaction = reactions.find(re => re.fromId === reaction.get('fromId'));
if (oldReaction) {
reactionToRemove = {
emoji: oldReaction.emoji,
@ -4177,6 +4177,8 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
Message: window.Whisper.Message,
});
}
return oldReaction;
}
async handleDeleteForEveryone(