Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(threads): restore reply soft-deletion #1352

Merged
merged 1 commit into from
Sep 6, 2024
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
9 changes: 7 additions & 2 deletions src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,13 @@ export class Thread<SCG extends ExtendableGenerics = DefaultGenerics> {

private subscribeReplyDeleted = () =>
this.client.on('message.deleted', (event) => {
if (event.message?.parent_id === this.id && event.hard_delete) {
return this.deleteReplyLocally({ message: event.message });
if (event.message?.parent_id !== this.id) return;

if (event.hard_delete) {
this.deleteReplyLocally({ message: event.message });
} else {
// Handle soft delete (updates deleted_at timestamp)
this.upsertReplyLocally({ message: event.message });
}
}).unsubscribe;

Expand Down
37 changes: 36 additions & 1 deletion test/unit/threads.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ describe('Threads 2.0', () => {
});

describe('Event: message.deleted', () => {
it('deletes reply if the message was hard-deleted', () => {
it('deletes reply from local store if it was hard-deleted', () => {
const createdAt = new Date().getTime();
// five messages "created" second apart
const messages = Array.from(
Expand Down Expand Up @@ -823,6 +823,41 @@ describe('Threads 2.0', () => {

thread.unregisterSubscriptions();
});

it('updates deleted_at property of the reply if it was soft deleted', () => {
const createdAt = new Date().getTime();
// five messages "created" second apart
const messages = Array.from(
{ length: 5 },
(_, i) =>
generateMsg({
parent_id: parentMessageResponse.id,
created_at: new Date(createdAt + 1000 * i).toISOString(),
}) as MessageResponse,
);
const thread = createTestThread({ latest_replies: messages });
thread.registerSubscriptions();

const messageToDelete = messages[2];

expect(messageToDelete.deleted_at).to.be.undefined;

const deletedAt = new Date();
client.dispatchEvent({
type: 'message.deleted',
message: { ...messageToDelete, type: 'deleted', deleted_at: deletedAt.toISOString() },
});

const stateAfter = thread.state.getLatestValue();
expect(stateAfter.replies).to.have.lengthOf(5);
expect(stateAfter.replies[2].id).to.equal(messageToDelete.id);
expect(stateAfter.replies[2]).to.not.equal(messageToDelete);
expect(stateAfter.replies[2].deleted_at).to.be.a('date');
expect(stateAfter.replies[2].deleted_at!.toISOString()).to.equal(deletedAt.toISOString());
expect(stateAfter.replies[2].type).to.equal('deleted');

thread.unregisterSubscriptions();
});
});

describe('Events: message.updated, reaction.new, reaction.deleted', () => {
Expand Down
Loading