From d50e81e475b96343db230db97bbc80571cfd7690 Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Sun, 22 Oct 2023 13:16:30 +0200 Subject: [PATCH] upd: improve note edit table & improve previous version view Closes transfem-org/Sharkey#105 --- .../migration/1697970083000-alterNoteEdit.js | 13 + packages/backend/src/core/NoteEditService.ts | 195 ++++++----- packages/backend/src/models/NoteEdit.ts | 7 +- .../src/models/json-schema/note-edit.ts | 7 +- .../server/api/endpoints/notes/versions.ts | 2 +- .../src/components/SkOldNoteWindow.vue | 328 ++++++++++++++++++ .../src/scripts/get-note-versions-menu.ts | 9 +- packages/misskey-js/src/entities.ts | 3 +- 8 files changed, 463 insertions(+), 101 deletions(-) create mode 100644 packages/backend/migration/1697970083000-alterNoteEdit.js create mode 100644 packages/frontend/src/components/SkOldNoteWindow.vue diff --git a/packages/backend/migration/1697970083000-alterNoteEdit.js b/packages/backend/migration/1697970083000-alterNoteEdit.js new file mode 100644 index 0000000000..11accb3c54 --- /dev/null +++ b/packages/backend/migration/1697970083000-alterNoteEdit.js @@ -0,0 +1,13 @@ +export class AlterNoteEdit1697970083000 { + name = "AlterNoteEdit1697970083000"; + + async up(queryRunner) { + await queryRunner.query(`ALTER TABLE "note_edit" RENAME COLUMN "text" TO "newText"`); + await queryRunner.query(`ALTER TABLE "note_edit" ADD COLUMN "oldText" text`); + } + + async down(queryRunner) { + await queryRunner.query(`ALTER TABLE "note_edit" RENAME COLUMN "newText" TO "text"`); + await queryRunner.query(`ALTER TABLE "note_edit" DROP COLUMN "oldText"`); + } +} diff --git a/packages/backend/src/core/NoteEditService.ts b/packages/backend/src/core/NoteEditService.ts index 1cbf0ee7f0..7b8aab7f3e 100644 --- a/packages/backend/src/core/NoteEditService.ts +++ b/packages/backend/src/core/NoteEditService.ts @@ -387,104 +387,109 @@ export class NoteEditService implements OnApplicationShutdown { update.hasPoll = !!data.poll; } - await this.noteEditRepository.insert({ - id: this.idService.gen(), - noteId: oldnote.id, - text: data.text || undefined, - cw: data.cw, - fileIds: undefined, - updatedAt: new Date(), - }); - - const note = new MiNote({ - 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, - renoteId: data.renote ? data.renote.id : null, - channelId: data.channel ? data.channel.id : null, - threadId: data.reply - ? data.reply.threadId - ? data.reply.threadId - : data.reply.id - : null, - name: data.name, - text: data.text, - hasPoll: data.poll != null, - cw: data.cw ?? null, - tags: tags.map(tag => normalizeForSearch(tag)), - emojis, - reactions: oldnote.reactions, - userId: user.id, - localOnly: data.localOnly!, - reactionAcceptance: data.reactionAcceptance, - visibility: data.visibility as any, - visibleUserIds: data.visibility === 'specified' - ? data.visibleUsers - ? data.visibleUsers.map(u => u.id) - : [] - : [], - - attachedFileTypes: data.files ? data.files.map(file => file.type) : [], - - // 以下非正規化データ - replyUserId: data.reply ? data.reply.userId : null, - replyUserHost: data.reply ? data.reply.userHost : null, - renoteUserId: data.renote ? data.renote.userId : null, - renoteUserHost: data.renote ? data.renote.userHost : null, - userHost: user.host, - }); - - if (data.uri != null) note.uri = data.uri; - if (data.url != null) note.url = data.url; - - if (mentionedUsers.length > 0) { - note.mentions = mentionedUsers.map(u => u.id); - const profiles = await this.userProfilesRepository.findBy({ userId: In(note.mentions) }); - note.mentionedRemoteUsers = JSON.stringify(mentionedUsers.filter(u => this.userEntityService.isRemoteUser(u)).map(u => { - const profile = profiles.find(p => p.userId === u.id); - const url = profile != null ? profile.url : null; - return { - uri: u.uri, - url: url ?? undefined, - username: u.username, - host: u.host, - } as IMentionedRemoteUsers[0]; - })); - } - - if (data.poll != null) { - // Start transaction - await this.db.transaction(async transactionalEntityManager => { - await transactionalEntityManager.update(MiNote, oldnote.id, note); - - const poll = new MiPoll({ - noteId: note.id, - choices: data.poll!.choices, - expiresAt: data.poll!.expiresAt, - multiple: data.poll!.multiple, - votes: new Array(data.poll!.choices.length).fill(0), - noteVisibility: note.visibility, - userId: user.id, - userHost: user.host, - }); - - if (!oldnote.hasPoll) { - await transactionalEntityManager.insert(MiPoll, poll); - } else { - await transactionalEntityManager.update(MiPoll, oldnote.id, poll); - } + if (Object.keys(update).length > 0) { + await this.noteEditRepository.insert({ + id: this.idService.gen(), + noteId: oldnote.id, + oldText: update.text ? oldnote.text : undefined, + newText: update.text || undefined, + cw: update.cw || undefined, + fileIds: undefined, + updatedAt: new Date(), }); + + const note = new MiNote({ + 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, + renoteId: data.renote ? data.renote.id : null, + channelId: data.channel ? data.channel.id : null, + threadId: data.reply + ? data.reply.threadId + ? data.reply.threadId + : data.reply.id + : null, + name: data.name, + text: data.text, + hasPoll: data.poll != null, + cw: data.cw ?? null, + tags: tags.map(tag => normalizeForSearch(tag)), + emojis, + reactions: oldnote.reactions, + userId: user.id, + localOnly: data.localOnly!, + reactionAcceptance: data.reactionAcceptance, + visibility: data.visibility as any, + visibleUserIds: data.visibility === 'specified' + ? data.visibleUsers + ? data.visibleUsers.map(u => u.id) + : [] + : [], + + attachedFileTypes: data.files ? data.files.map(file => file.type) : [], + + // 以下非正規化データ + replyUserId: data.reply ? data.reply.userId : null, + replyUserHost: data.reply ? data.reply.userHost : null, + renoteUserId: data.renote ? data.renote.userId : null, + renoteUserHost: data.renote ? data.renote.userHost : null, + userHost: user.host, + }); + + if (data.uri != null) note.uri = data.uri; + if (data.url != null) note.url = data.url; + + if (mentionedUsers.length > 0) { + note.mentions = mentionedUsers.map(u => u.id); + const profiles = await this.userProfilesRepository.findBy({ userId: In(note.mentions) }); + note.mentionedRemoteUsers = JSON.stringify(mentionedUsers.filter(u => this.userEntityService.isRemoteUser(u)).map(u => { + const profile = profiles.find(p => p.userId === u.id); + const url = profile != null ? profile.url : null; + return { + uri: u.uri, + url: url ?? undefined, + username: u.username, + host: u.host, + } as IMentionedRemoteUsers[0]; + })); + } + + if (data.poll != null) { + // Start transaction + await this.db.transaction(async transactionalEntityManager => { + await transactionalEntityManager.update(MiNote, oldnote.id, note); + + const poll = new MiPoll({ + noteId: note.id, + choices: data.poll!.choices, + expiresAt: data.poll!.expiresAt, + multiple: data.poll!.multiple, + votes: new Array(data.poll!.choices.length).fill(0), + noteVisibility: note.visibility, + userId: user.id, + userHost: user.host, + }); + + if (!oldnote.hasPoll) { + await transactionalEntityManager.insert(MiPoll, poll); + } else { + await transactionalEntityManager.update(MiPoll, oldnote.id, poll); + } + }); + } else { + await this.notesRepository.update(oldnote.id, note); + } + + setImmediate('post edited', { signal: this.#shutdownController.signal }).then( + () => this.postNoteEdited(note, user, data, silent, tags!, mentionedUsers!), + () => { /* aborted, ignore this */ }, + ); + + return note; } else { - await this.notesRepository.update(oldnote.id, note); + return oldnote; } - - setImmediate('post edited', { signal: this.#shutdownController.signal }).then( - () => this.postNoteEdited(note, user, data, silent, tags!, mentionedUsers!), - () => { /* aborted, ignore this */ }, - ); - - return note; } @bindThis diff --git a/packages/backend/src/models/NoteEdit.ts b/packages/backend/src/models/NoteEdit.ts index 547b135e56..440f9b8208 100644 --- a/packages/backend/src/models/NoteEdit.ts +++ b/packages/backend/src/models/NoteEdit.ts @@ -24,7 +24,12 @@ export class NoteEdit { @Column("text", { nullable: true, }) - public text: string | null; + public oldText: string | null; + + @Column("text", { + nullable: true, + }) + public newText: string | null; @Column("varchar", { length: 512, diff --git a/packages/backend/src/models/json-schema/note-edit.ts b/packages/backend/src/models/json-schema/note-edit.ts index e877f3f946..a58e2aa1de 100644 --- a/packages/backend/src/models/json-schema/note-edit.ts +++ b/packages/backend/src/models/json-schema/note-edit.ts @@ -26,7 +26,12 @@ export const packedNoteEdit = { nullable: false, format: "id", }, - text: { + oldText: { + type: "string", + optional: true, + nullable: true, + }, + newText: { type: "string", optional: true, nullable: true, diff --git a/packages/backend/src/server/api/endpoints/notes/versions.ts b/packages/backend/src/server/api/endpoints/notes/versions.ts index 9733d781a4..e6831f3208 100644 --- a/packages/backend/src/server/api/endpoints/notes/versions.ts +++ b/packages/backend/src/server/api/endpoints/notes/versions.ts @@ -51,7 +51,7 @@ export default class extends Endpoint { // eslint- for (const edit of edits) { editArray.push({ updatedAt: new Date(edit.updatedAt).toLocaleString('UTC', { hour: 'numeric', minute: 'numeric', second: 'numeric', year: 'numeric', month: 'short', day: 'numeric' }), - text: edit.text, + text: edit.oldText, }); } diff --git a/packages/frontend/src/components/SkOldNoteWindow.vue b/packages/frontend/src/components/SkOldNoteWindow.vue new file mode 100644 index 0000000000..bd0b87bf6c --- /dev/null +++ b/packages/frontend/src/components/SkOldNoteWindow.vue @@ -0,0 +1,328 @@ + + + + + diff --git a/packages/frontend/src/scripts/get-note-versions-menu.ts b/packages/frontend/src/scripts/get-note-versions-menu.ts index 12b81c750d..4191920638 100644 --- a/packages/frontend/src/scripts/get-note-versions-menu.ts +++ b/packages/frontend/src/scripts/get-note-versions-menu.ts @@ -1,4 +1,4 @@ -import { Ref } from 'vue'; +import { Ref, defineAsyncComponent } from 'vue'; import * as Misskey from 'misskey-js'; import { i18n } from '@/i18n.js'; import * as os from '@/os.js'; @@ -20,7 +20,12 @@ export async function getNoteVersionsMenu(props: { const cleanups = [] as (() => void)[]; function openVersion(info): void { - os.alert({ type: 'info', title: `Edits from ${info.updatedAt}`, text: info.text }); + os.popup(defineAsyncComponent(() => import('@/components/SkOldNoteWindow.vue')), { + note: appearNote, + oldText: info.text, + updatedAt: info.updatedAt, + }, { + }, 'closed'); } const menu: MenuItem[] = []; diff --git a/packages/misskey-js/src/entities.ts b/packages/misskey-js/src/entities.ts index baf59e7283..8d954886d9 100644 --- a/packages/misskey-js/src/entities.ts +++ b/packages/misskey-js/src/entities.ts @@ -230,7 +230,8 @@ export type NoteReaction = { export type NoteEdit = { noteId: Note['id']; note: Note; - text: string; + newText: string; + oldText: string; cw: string; fileIds: DriveFile['id'][]; updatedAt?: DateString;