From 0a3d93b59b33d0c6dc177d524b05886093aac7c3 Mon Sep 17 00:00:00 2001 From: Maurice Date: Wed, 11 Nov 2026 20:41:25 +0000 Subject: [PATCH] fix(collab): a deleted chat message keeps no text Deleting a message set the flag and kept the text in the row and in every backup; only the client hid it. The delete now blanks the text with the flag, and a migration clears what earlier deletes left. The row stays for the placeholder. --- ...70_a_deleted_chat_message_keeps_no_text.ts | 15 ++++++ .../repositories/CollabMessages.repository.ts | 8 ++- .../db/deleted-chat-text-migration.test.ts | 49 +++++++++++++++++++ server/tests/unit/db/legacy-baseline.test.ts | 1 + server/tests/unit/nest/collab.service.test.ts | 3 ++ 5 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 server/src/db/migrations/Migration20200101046370_a_deleted_chat_message_keeps_no_text.ts create mode 100644 server/tests/unit/db/deleted-chat-text-migration.test.ts diff --git a/server/src/db/migrations/Migration20200101046370_a_deleted_chat_message_keeps_no_text.ts b/server/src/db/migrations/Migration20200101046370_a_deleted_chat_message_keeps_no_text.ts new file mode 100644 index 000000000..c3245aa95 --- /dev/null +++ b/server/src/db/migrations/Migration20200101046370_a_deleted_chat_message_keeps_no_text.ts @@ -0,0 +1,15 @@ +import { Migration } from '@mikro-orm/migrations'; + +/** + * A chat message deleted before this release kept its text in the row; only the + * client hid it. Deleting now blanks the text with the flag + * (`CollabMessagesRepository.softDelete`), and this clears what older deletes + * left behind. The row itself stays for the "message deleted" placeholder. + */ +export class Migration20200101046370_a_deleted_chat_message_keeps_no_text extends Migration { + override name = 'Migration20200101046370_a_deleted_chat_message_keeps_no_text'; + + override up(): void { + this.addSql("UPDATE collab_messages SET text = '' WHERE deleted = 1 AND text != ''"); + } +} diff --git a/server/src/db/repositories/CollabMessages.repository.ts b/server/src/db/repositories/CollabMessages.repository.ts index 1686c40d4..88b258ca5 100644 --- a/server/src/db/repositories/CollabMessages.repository.ts +++ b/server/src/db/repositories/CollabMessages.repository.ts @@ -179,9 +179,13 @@ export class CollabMessagesRepository extends TrekRepository { return await this.insert({ trip: trip_id, user: user_id, text, reply_to, origin }); } - /** CB54 (`deleteMessage`, inside its transaction) — `UPDATE collab_messages SET deleted = 1 WHERE id = ?`, soft delete — the row survives for the placeholder. */ + /** + * CB54 (`deleteMessage`, inside its transaction) — `UPDATE collab_messages SET deleted = 1, text = '' WHERE id = ?`, + * soft delete: the row survives for the placeholder, its text does not, so a deleted message is gone from the + * database and every backup made after it, not only hidden from the client. + */ async softDelete(id: number): Promise { - await this.nativeUpdate({ id }, { deleted: 1 }); + await this.nativeUpdate({ id }, { deleted: 1, text: '' }); } // ------------------------------------------------------------------------- diff --git a/server/tests/unit/db/deleted-chat-text-migration.test.ts b/server/tests/unit/db/deleted-chat-text-migration.test.ts new file mode 100644 index 000000000..04507aef4 --- /dev/null +++ b/server/tests/unit/db/deleted-chat-text-migration.test.ts @@ -0,0 +1,49 @@ +/** + * `Migration20200101046370_a_deleted_chat_message_keeps_no_text`: a message deleted + * before the release loses its text; a live message keeps its own. + */ +import { createMigrationOrm, migrateTo, pendingNames, rawExec, rawQuery } from '../../helpers/migration-step'; +import type { MikroORM } from '@mikro-orm/sqlite'; + +import { describe, expect, it } from 'vitest'; + +const TARGET = 'Migration20200101046370_a_deleted_chat_message_keeps_no_text'; + +async function ormBeforeTarget(): Promise { + const orm = await createMigrationOrm(); + const names = await pendingNames(orm); + const index = names.indexOf(TARGET); + expect(index).toBeGreaterThan(0); + await migrateTo(orm, names[index - 1]); + return orm; +} + +describe('a deleted chat message keeps no text', () => { + it('CHAT-DELETED-TEXT-001: blanks the text of deleted messages and leaves live ones alone', async () => { + const orm = await ormBeforeTarget(); + try { + await rawExec( + orm, + "INSERT INTO users (id, username, email, password_hash) VALUES (1, 'owner', 'owner@test', 'x')", + ); + await rawExec(orm, "INSERT INTO trips (id, user_id, title) VALUES (10, 1, 'Kyoto')"); + await rawExec( + orm, + "INSERT INTO collab_messages (id, trip_id, user_id, text, deleted) VALUES (1, 10, 1, 'meet at 8', 0), (2, 10, 1, 'my door code is 4711', 1)", + ); + + await migrateTo(orm, TARGET); + + const rows = await rawQuery<{ id: number; text: string; deleted: number }>( + orm, + 'SELECT id, text, deleted FROM collab_messages ORDER BY id', + ); + expect(rows).toEqual([ + { id: 1, text: 'meet at 8', deleted: 0 }, + { id: 2, text: '', deleted: 1 }, + ]); + } finally { + await orm.close(true); + } + }, 30000); +}); diff --git a/server/tests/unit/db/legacy-baseline.test.ts b/server/tests/unit/db/legacy-baseline.test.ts index f8956a5ae..ef7d8fd91 100644 --- a/server/tests/unit/db/legacy-baseline.test.ts +++ b/server/tests/unit/db/legacy-baseline.test.ts @@ -104,6 +104,7 @@ const UNNUMBERED = [ 'Migration20200101046340_a_released_object_outlasts_every_undo_before_it_goes', 'Migration20200101046350_a_place_lies_on_the_real_world', 'Migration20200101046360_a_new_install_starts_with_more_addons_on', + 'Migration20200101046370_a_deleted_chat_message_keeps_no_text', ]; // The last step the positional runner ever had; planLegacyBaseline refuses a // schema_version past it. diff --git a/server/tests/unit/nest/collab.service.test.ts b/server/tests/unit/nest/collab.service.test.ts index 8ccddd411..f0105be87 100644 --- a/server/tests/unit/nest/collab.service.test.ts +++ b/server/tests/unit/nest/collab.service.test.ts @@ -343,6 +343,9 @@ describe('listMessages', () => { expect(msgs).toHaveLength(1); expect(msgs[0].text).toBe(''); expect(msgs[0].deleted).toBe(1); + // Gone from the row itself, not only blanked on the way out. + const row = await findRow(orm, CollabMessages, { id: r.message!.id }); + expect(row?.text).toBe(''); }); it('COLLAB-SVC-013b: blanks reply_text when the quoted message was deleted', async () => {