Remove notification on reaction remove/change

This commit is contained in:
Fedor Indutny 2021-03-03 11:03:11 -08:00 committed by Josh Perez
parent c4dc3f3bcb
commit 18fb2b806e
2 changed files with 58 additions and 11 deletions

View file

@ -47,6 +47,7 @@
// isExpiringMessage: boolean; // isExpiringMessage: boolean;
// reaction: { // reaction: {
// emoji: string; // emoji: string;
// fromId: string;
// }; // };
// } // }
notificationData: null, notificationData: null,
@ -56,16 +57,40 @@
this.update(); this.update();
}, },
removeBy({ conversationId, messageId }) { // Remove the last notification if both conditions hold:
const shouldClear = //
Boolean(this.notificationData) && // 1. Either `conversationId` or `messageId` matches (if present)
((conversationId && // 2. `reactionFromId` matches (if present)
this.notificationData.conversationId === conversationId) || removeBy({ conversationId, messageId, reactionFromId }) {
(messageId && this.notificationData.messageId === messageId)); if (!this.notificationData) {
if (shouldClear) { return;
this.clear();
this.update();
} }
let shouldClear = false;
if (
conversationId &&
this.notificationData.conversationId === conversationId
) {
shouldClear = true;
}
if (messageId && this.notificationData.messageId === messageId) {
shouldClear = true;
}
if (!shouldClear) {
return;
}
if (
reactionFromId &&
this.notificationData.reaction &&
this.notificationData.reaction.fromId !== reactionFromId
) {
return;
}
this.clear();
this.update();
}, },
fastUpdate() { fastUpdate() {

View file

@ -3642,6 +3642,12 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
const messageId = this.idForLogging(); const messageId = this.idForLogging();
const count = reactions.length; const count = reactions.length;
const conversation = window.ConversationController.get(
this.get('conversationId')
);
let staleReactionFromId: string | undefined;
if (reaction.get('remove')) { if (reaction.get('remove')) {
window.log.info('Removing reaction for message', messageId); window.log.info('Removing reaction for message', messageId);
const newReactions = reactions.filter( const newReactions = reactions.filter(
@ -3650,6 +3656,8 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
re.fromId !== reaction.get('fromId') re.fromId !== reaction.get('fromId')
); );
this.set({ reactions: newReactions }); this.set({ reactions: newReactions });
staleReactionFromId = reaction.get('fromId');
} else { } else {
window.log.info('Adding reaction for message', messageId); window.log.info('Adding reaction for message', messageId);
const newReactions = reactions.filter( const newReactions = reactions.filter(
@ -3658,9 +3666,12 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
newReactions.push(reaction.toJSON()); newReactions.push(reaction.toJSON());
this.set({ reactions: newReactions }); this.set({ reactions: newReactions });
const conversation = window.ConversationController.get( const oldReaction = reactions.find(
this.get('conversationId') re => re.fromId === reaction.get('fromId')
); );
if (oldReaction) {
staleReactionFromId = oldReaction.fromId;
}
// Only notify for reactions to our own messages // Only notify for reactions to our own messages
if (conversation && this.isOutgoing() && !reaction.get('fromSync')) { if (conversation && this.isOutgoing() && !reaction.get('fromSync')) {
@ -3668,6 +3679,10 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
} }
} }
if (staleReactionFromId) {
this.clearNotifications(reaction.get('fromId'));
}
const newCount = this.get('reactions').length; const newCount = this.get('reactions').length;
window.log.info( window.log.info(
`Done processing reaction for message ${messageId}. Went from ${count} to ${newCount} reactions.` `Done processing reaction for message ${messageId}. Went from ${count} to ${newCount} reactions.`
@ -3704,6 +3719,13 @@ export class MessageModel extends window.Backbone.Model<MessageAttributesType> {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.getConversation()!.updateLastMessage(); this.getConversation()!.updateLastMessage();
} }
clearNotifications(reactionFromId?: string): void {
window.Whisper.Notifications.removeBy({
messageId: this.id,
reactionFromId,
});
}
} }
window.Whisper.Message = MessageModel as typeof window.WhatIsThis; window.Whisper.Message = MessageModel as typeof window.WhatIsThis;