From 5d40ca6568637a907243f668a9d5ea691a1777e7 Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Fri, 22 Sep 2023 23:44:07 +0200 Subject: [PATCH] upd: update notes live on feed --- packages/backend/src/core/NoteEditService.ts | 4 +++- .../backend/src/server/api/stream/types.ts | 3 +++ .../frontend/src/scripts/use-note-capture.ts | 19 ++++++++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/core/NoteEditService.ts b/packages/backend/src/core/NoteEditService.ts index 1a34fa1154..21d7758019 100644 --- a/packages/backend/src/core/NoteEditService.ts +++ b/packages/backend/src/core/NoteEditService.ts @@ -525,7 +525,9 @@ export class NoteEditService implements OnApplicationShutdown { // Pack the note const noteObj = await this.noteEntityService.pack(note); - this.globalEventService.publishNotesStream(noteObj); + this.globalEventService.publishNoteStream(note.id, 'updated', { + updatedAt: note.updatedAt!, + }); this.roleService.addNoteToRoleTimeline(noteObj); diff --git a/packages/backend/src/server/api/stream/types.ts b/packages/backend/src/server/api/stream/types.ts index 90e0a61f26..9bdab19de4 100644 --- a/packages/backend/src/server/api/stream/types.ts +++ b/packages/backend/src/server/api/stream/types.ts @@ -142,6 +142,9 @@ export interface NoteStreamTypes { reaction: string; userId: MiUser['id']; }; + updated: { + updatedAt: Date; + } } type NoteStreamEventTypes = { [key in keyof NoteStreamTypes]: { diff --git a/packages/frontend/src/scripts/use-note-capture.ts b/packages/frontend/src/scripts/use-note-capture.ts index c618532570..088e1d6c83 100644 --- a/packages/frontend/src/scripts/use-note-capture.ts +++ b/packages/frontend/src/scripts/use-note-capture.ts @@ -7,6 +7,7 @@ import { onUnmounted, Ref } from 'vue'; import * as Misskey from 'misskey-js'; import { useStream } from '@/stream.js'; import { $i } from '@/account.js'; +import * as os from '@/os.js'; export function useNoteCapture(props: { rootEl: Ref; @@ -16,7 +17,7 @@ export function useNoteCapture(props: { const note = props.note; const connection = $i ? useStream() : null; - function onStreamNoteUpdated(noteData): void { + async function onStreamNoteUpdated(noteData): void { const { type, id, body } = noteData; if (id !== note.value.id) return; @@ -75,6 +76,22 @@ export function useNoteCapture(props: { props.isDeletedRef.value = true; break; } + + case "updated": { + const editedNote = await os.api("notes/show", { + noteId: id, + }); + + const keys = new Set(); + Object.keys(editedNote) + .concat(Object.keys(note.value)) + .forEach((key) => keys.add(key)); + keys.forEach((key) => { + note.value[key] = editedNote[key]; + }); + break; + } + } }