Skip to content

Commit

Permalink
Merge pull request #726 from tsirlucas/remove-quoted-messages
Browse files Browse the repository at this point in the history
Add remove quoted messages logics
  • Loading branch information
vishalnarkhede authored Jul 28, 2021
2 parents 17473e3 + 7f45538 commit 59011d4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1563,6 +1563,9 @@ export class Channel<
if (event.message) {
if (event.hard_delete) channelState.removeMessage(event.message);
else channelState.addMessageSorted(event.message, false, false);

channelState.removeQuotedMessageReferences(event.message);

if (event.message.pinned) {
channelState.removePinnedMessage(event.message);
}
Expand Down
45 changes: 45 additions & 0 deletions src/channel_state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,51 @@ export class ChannelState<
return messageWithReaction;
}

removeQuotedMessageReferences(
message: MessageResponse<
AttachmentType,
ChannelType,
CommandType,
MessageType,
ReactionType,
UserType
>,
) {
const parseMessage = (
m: ReturnType<
ChannelState<
AttachmentType,
ChannelType,
CommandType,
EventType,
MessageType,
ReactionType,
UserType
>['formatMessage']
>,
) =>
(({
...m,
created_at: m.created_at.toString(),
pinned_at: m.pinned_at?.toString(),
updated_at: m.updated_at?.toString(),
} as unknown) as MessageResponse<
AttachmentType,
ChannelType,
CommandType,
MessageType,
ReactionType,
UserType
>);

const updatedMessages = this.messages
.filter((msg) => msg.quoted_message_id === message.id)
.map(parseMessage)
.map((msg) => ({ ...msg, quoted_message: { ...message, attachments: [] } }));

this.addMessagesSorted(updatedMessages, true);
}

/**
* Updates all instances of given message in channel state
* @param message
Expand Down
32 changes: 32 additions & 0 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,38 @@ describe('Channel _handleChannelEvent', function () {
});
expect(channel.state.unreadCount).to.be.equal(30);
});

it('message.delete removes quoted messages references', function () {
const originalMessage = generateMsg({ silent: true });
channel._handleChannelEvent({
type: 'message.new',
user: { id: 'id' },
message: originalMessage,
});

const quotingMessage = generateMsg({
silent: true,
quoted_message: originalMessage,
quoted_message_id: originalMessage.id,
});

channel._handleChannelEvent({
type: 'message.new',
user: { id: 'id2' },
message: quotingMessage,
});

channel._handleChannelEvent({
type: 'message.deleted',
user: { id: 'id' },
message: { ...originalMessage, deleted_at: new Date().toISOString() },
});

expect(
channel.state.messages.find((msg) => msg.id === quotingMessage.id)
.quoted_message.deleted_at,
).to.be.ok;
});
});

describe('Channels - Constructor', function () {
Expand Down

0 comments on commit 59011d4

Please sign in to comment.