From 4afbf3b91e058554f80768d198ab203e43c5ff6b Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 22 Feb 2024 18:23:46 +0000 Subject: [PATCH] some fixes for note edits --- packages/backend/src/core/NoteEditService.ts | 10 +++++++++- packages/backend/src/core/NoteReadService.ts | 6 +++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/core/NoteEditService.ts b/packages/backend/src/core/NoteEditService.ts index 6a469c9634..e540abfcf0 100644 --- a/packages/backend/src/core/NoteEditService.ts +++ b/packages/backend/src/core/NoteEditService.ts @@ -241,6 +241,14 @@ export class NoteEditService implements OnApplicationShutdown { throw new Error('not the author'); } + // we never want to change the replyId, so fetch the original "parent" + if (oldnote.replyId) { + data.reply = await this.notesRepository.findOneBy({ id: oldnote.replyId }); + } + else { + data.reply = undefined; + } + // チャンネル外にリプライしたら対象のスコープに合わせる // (クライアントサイドでやっても良い処理だと思うけどとりあえずサーバーサイドで) if (data.reply && data.channel && data.reply.channelId !== data.channel.id) { @@ -435,7 +443,7 @@ export class NoteEditService implements OnApplicationShutdown { id: oldnote.id, updatedAt: data.updatedAt ? data.updatedAt : new Date(), fileIds: data.files ? data.files.map(file => file.id) : [], - replyId: data.reply ? data.reply.id : null, + replyId: oldnote.replyId, renoteId: data.renote ? data.renote.id : null, channelId: data.channel ? data.channel.id : null, threadId: data.reply diff --git a/packages/backend/src/core/NoteReadService.ts b/packages/backend/src/core/NoteReadService.ts index feef024602..45f9c454e9 100644 --- a/packages/backend/src/core/NoteReadService.ts +++ b/packages/backend/src/core/NoteReadService.ts @@ -66,7 +66,11 @@ export class NoteReadService implements OnApplicationShutdown { noteUserId: note.userId, }; - await this.noteUnreadsRepository.insert(unread); + /* we may be called from NoteEditService, for a note that's + already present in the `note_unread` table: `upsert` makes sure + we don't throw a "duplicate key" error, while still updating + the other columns if they've changed */ + await this.noteUnreadsRepository.upsert(unread,['userId', 'noteId']); // 2秒経っても既読にならなかったら「未読の投稿がありますよ」イベントを発行する setTimeout(2000, 'unread note', { signal: this.#shutdownController.signal }).then(async () => {