Skip to content

Commit 6c1cb2a

Browse files
committed
feat: add optimistic reaction removal capability to llc
1 parent ea47d2b commit 6c1cb2a

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

src/channel_state.ts

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ export class ChannelState {
337337
count: oldOwnReactionTypeData.count - 1,
338338
sum_scores: oldOwnReactionTypeData.sum_scores - (ownReaction.score ?? 1),
339339
};
340+
// If there are no reactions left in this group, simply remove it.
340341
if (messageFromState.reaction_groups[ownReaction.type].count < 1) {
341342
delete messageFromState.reaction_groups[ownReaction.type];
342343
}
@@ -421,16 +422,65 @@ export class ChannelState {
421422
}
422423

423424
removeReaction(reaction: ReactionResponse, message?: MessageResponse) {
424-
if (!message) return;
425-
const messageWithReaction = message;
426-
this._updateMessage(message, (msg) => {
427-
messageWithReaction.own_reactions = this._removeOwnReactionFromMessage(
428-
msg.own_reactions,
429-
reaction,
430-
);
431-
return this.formatMessage(messageWithReaction);
425+
const messageWithRemovedReaction = message;
426+
let messageFromState: FormatMessageResponse | undefined;
427+
if (!messageWithRemovedReaction) {
428+
messageFromState = this.findMessage(reaction.message_id);
429+
}
430+
431+
if (!messageWithRemovedReaction && !messageFromState) {
432+
return;
433+
}
434+
435+
const messageToUpdate = messageWithRemovedReaction ?? messageFromState;
436+
const updateData = {
437+
id: messageToUpdate?.id,
438+
parent_id: messageToUpdate?.parent_id,
439+
pinned: messageToUpdate?.pinned,
440+
show_in_channel: messageToUpdate?.show_in_channel,
441+
};
442+
this._updateMessage(updateData, (msg) => {
443+
if (messageWithRemovedReaction) {
444+
messageWithRemovedReaction.own_reactions = this._removeOwnReactionFromMessage(
445+
msg.own_reactions,
446+
reaction,
447+
);
448+
return this.formatMessage(messageWithRemovedReaction);
449+
}
450+
451+
if (messageFromState) {
452+
const reactionToRemove = messageFromState.own_reactions?.find(
453+
(r) => r.type === reaction.type,
454+
);
455+
if (
456+
reactionToRemove &&
457+
messageFromState.reaction_groups?.[reactionToRemove.type]
458+
) {
459+
const newReactionGroup =
460+
messageFromState.reaction_groups[reactionToRemove.type];
461+
messageFromState.reaction_groups[reactionToRemove.type] = {
462+
...newReactionGroup,
463+
count: newReactionGroup.count - 1,
464+
sum_scores: newReactionGroup.sum_scores - (reactionToRemove.score ?? 1),
465+
};
466+
// If there are no reactions left in this group, simply remove it.
467+
if (messageFromState.reaction_groups[reactionToRemove.type].count < 1) {
468+
delete messageFromState.reaction_groups[reactionToRemove.type];
469+
}
470+
}
471+
messageFromState.own_reactions = messageFromState.own_reactions?.filter(
472+
(r) => r.type !== reaction.type,
473+
);
474+
const userId = this._channel.getClient().userID;
475+
messageFromState.latest_reactions = messageFromState.latest_reactions?.filter(
476+
(r) => !(r.user_id === userId && r.type === reaction.type),
477+
);
478+
return messageFromState;
479+
}
480+
481+
return msg;
432482
});
433-
return messageWithReaction;
483+
return messageWithRemovedReaction;
434484
}
435485

436486
_updateQuotedMessageReferences({

0 commit comments

Comments
 (0)