From 83be996a3dc55e87f5544b07f899eb21ce4d0534 Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Sun, 22 Oct 2023 14:07:04 +0200 Subject: [PATCH] upd: store old date and use it in previous versions --- .../migration/1697970083001-oldDateNoteEdit.js | 11 +++++++++++ packages/backend/src/core/NoteEditService.ts | 3 +++ packages/backend/src/models/NoteEdit.ts | 5 +++++ .../src/server/api/endpoints/notes/versions.ts | 3 ++- .../frontend/src/scripts/get-note-versions-menu.ts | 10 +++++----- packages/misskey-js/src/entities.ts | 1 + 6 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 packages/backend/migration/1697970083001-oldDateNoteEdit.js diff --git a/packages/backend/migration/1697970083001-oldDateNoteEdit.js b/packages/backend/migration/1697970083001-oldDateNoteEdit.js new file mode 100644 index 0000000000..17565a0914 --- /dev/null +++ b/packages/backend/migration/1697970083001-oldDateNoteEdit.js @@ -0,0 +1,11 @@ +export class OldDateNoteEdit1697970083001 { + name = "OldDateNoteEdit1697970083001"; + + async up(queryRunner) { + await queryRunner.query(`ALTER TABLE "note_edit" ADD COLUMN "oldDate" TIMESTAMP WITH TIME ZONE`); + } + + async down(queryRunner) { + await queryRunner.query(`ALTER TABLE "note_edit" DROP COLUMN "oldDate"`); + } +} diff --git a/packages/backend/src/core/NoteEditService.ts b/packages/backend/src/core/NoteEditService.ts index 7b8aab7f3e..1961fe72ee 100644 --- a/packages/backend/src/core/NoteEditService.ts +++ b/packages/backend/src/core/NoteEditService.ts @@ -388,6 +388,8 @@ export class NoteEditService implements OnApplicationShutdown { } if (Object.keys(update).length > 0) { + const exists = await this.noteEditRepository.findOneBy({ noteId: oldnote.id }); + await this.noteEditRepository.insert({ id: this.idService.gen(), noteId: oldnote.id, @@ -395,6 +397,7 @@ export class NoteEditService implements OnApplicationShutdown { newText: update.text || undefined, cw: update.cw || undefined, fileIds: undefined, + oldDate: exists ? oldnote.updatedAt as Date : this.idService.parse(oldnote.id).date, updatedAt: new Date(), }); diff --git a/packages/backend/src/models/NoteEdit.ts b/packages/backend/src/models/NoteEdit.ts index 440f9b8208..9ec989dd50 100644 --- a/packages/backend/src/models/NoteEdit.ts +++ b/packages/backend/src/models/NoteEdit.ts @@ -48,4 +48,9 @@ export class NoteEdit { comment: "The updated date of the Note.", }) public updatedAt: Date; + + @Column("timestamp with time zone", { + comment: "The old date from before the edit", + }) + public oldDate: Date; } diff --git a/packages/backend/src/server/api/endpoints/notes/versions.ts b/packages/backend/src/server/api/endpoints/notes/versions.ts index 18a68110f7..d236cea64e 100644 --- a/packages/backend/src/server/api/endpoints/notes/versions.ts +++ b/packages/backend/src/server/api/endpoints/notes/versions.ts @@ -50,12 +50,13 @@ export default class extends Endpoint { // eslint- for (const edit of edits) { editArray.push({ + oldDate: edit.oldDate, updatedAt: edit.updatedAt, text: edit.oldText, }); } - editArray = editArray.sort((a, b) => { return new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime(); }); + editArray = editArray.sort((a, b) => { return new Date(b.oldDate).getTime() - new Date(a.oldDate).getTime(); }); return editArray; }); diff --git a/packages/frontend/src/scripts/get-note-versions-menu.ts b/packages/frontend/src/scripts/get-note-versions-menu.ts index 599ae9c89f..46e3bab3a7 100644 --- a/packages/frontend/src/scripts/get-note-versions-menu.ts +++ b/packages/frontend/src/scripts/get-note-versions-menu.ts @@ -24,7 +24,7 @@ export async function getNoteVersionsMenu(props: { os.popup(defineAsyncComponent(() => import('@/components/SkOldNoteWindow.vue')), { note: appearNote, oldText: info.text, - updatedAt: info.updatedAt, + updatedAt: info.oldDate ? info.oldDate : info.updatedAt, }, { }, 'closed'); } @@ -36,13 +36,13 @@ export async function getNoteVersionsMenu(props: { await statePromise.then((versions) => { for (const edit of versions) { - const _time = edit.updatedAt == null ? NaN : - typeof edit.updatedAt === 'number' ? edit.updatedAt : - (edit.updatedAt instanceof Date ? edit.updatedAt : new Date(edit.updatedAt)).getTime(); + const _time = edit.oldDate == null ? NaN : + typeof edit.oldDate === 'number' ? edit.oldDate : + (edit.oldDate instanceof Date ? edit.oldDate : new Date(edit.oldDate)).getTime(); menu.push({ icon: 'ph-pencil ph-bold ph-lg', - text: dateTimeFormat.format(_time), + text: _time ? dateTimeFormat.format(_time) : dateTimeFormat.format(new Date(edit.updatedAt)), action: () => openVersion(edit), }); } diff --git a/packages/misskey-js/src/entities.ts b/packages/misskey-js/src/entities.ts index 8d954886d9..0cd43044bd 100644 --- a/packages/misskey-js/src/entities.ts +++ b/packages/misskey-js/src/entities.ts @@ -235,6 +235,7 @@ export type NoteEdit = { cw: string; fileIds: DriveFile['id'][]; updatedAt?: DateString; + oldDate: DateString; } export type Notification = {