Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,18 @@ export class Thread extends WithSubscriptions {
this.messagePaginator.reflectQuotedMessageUpdate(formatMessage(message));
};

public handleUserUpdated = (event: PipelineEvent) => {
const { user } = event;
if (!user) return;
this.messagePaginator.reflectUserUpdate(user);

// the parent message might not yet be loaded in channel.messagePaginator
const parent = this.client.messageStore.get(this.id);
if (parent?.user?.id === user.id && parent.user !== user) {
this.client.messageStore.upsert({ ...parent, user });
}
};

// Applies a user ban or deletion to this thread's replies.
public handleUserMessagesDeleted = (event: PipelineEvent) => {
if (!event.user) return;
Expand Down
5 changes: 4 additions & 1 deletion src/thread_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -338,14 +338,17 @@ export class ThreadManager extends WithSubscriptions {
),
),
),
// A banned or deleted user's replies can be in any thread, so these two are the exception.
// A user's replies can be in any thread, so the user events are the exception.
...(['user.messages.deleted', 'user.deleted'] as const).map((eventType) =>
this.client.on(eventType, (event) =>
this.threadStore
.values()
.forEach((thread) => thread.handleUserMessagesDeleted(event)),
),
),
this.client.on('user.updated', (event) =>
this.threadStore.values().forEach((thread) => thread.handleUserUpdated(event)),
),
].map(({ unsubscribe }) => unsubscribe);

return () => unsubscribeFunctions.forEach((unsubscribe) => unsubscribe());
Expand Down
61 changes: 61 additions & 0 deletions test/unit/threads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2375,6 +2375,67 @@ describe('Threads 2.0', () => {
});
});

it('replaces an updated user in the thread paginator\'s replies on "user.updated"', () => {
const thread = createTestThread();
subscribeThread(thread);
const authorId = 'renamed-user';
const replyId = uuidv4();
thread.messagePaginator.ingestPage({
page: [
formatMessage(
generateMsg({
id: replyId,
parent_id: thread.id,
user: { id: authorId, name: 'Old name' },
}) as MessageResponse,
),
],
isHead: true,
isTail: true,
setActive: true,
});

client.dispatchEvent({
type: 'user.updated',
user: { id: authorId, name: 'New name' },
created_at: nowNs(),
});

expect(thread.messagePaginator.getItem(replyId)?.user?.name).to.equal(
'New name',
);
expect(
thread.messagePaginator.items?.find((reply) => reply.id === replyId)?.user
?.name,
).to.equal('New name');

thread.unregisterSubscriptions();
});

it('replaces an updated user in the parent message on "user.updated" when no channel paginator holds it', () => {
const authorId = 'renamed-parent-author';
const thread = createTestThread({
parentMessageOverrides: { user: { id: authorId, name: 'Old name' } },
});
subscribeThread(thread);
expect(channel.messagePaginator.getItem(thread.id)).to.be.undefined;
expect(thread.state.getLatestValue().parentMessage.user?.name).to.equal(
'Old name',
);

client.dispatchEvent({
type: 'user.updated',
user: { id: authorId, name: 'New name' },
created_at: nowNs(),
});

expect(thread.state.getLatestValue().parentMessage.user?.name).to.equal(
'New name',
);

thread.unregisterSubscriptions();
});

it('applies "reaction.new" to a reply the thread paginator already holds', () => {
const thread = createTestThread();
subscribeThread(thread);
Expand Down
Loading