From 38a35bda27c48d28632e1e92f3354eefd09558c7 Mon Sep 17 00:00:00 2001 From: Kaity A Date: Tue, 2 May 2023 22:42:33 +1000 Subject: [PATCH] Readd deepClone and move updated into note capture --- packages/client/src/components/MkNote.vue | 2 +- .../client/src/components/MkNoteDetailed.vue | 21 +-------- packages/client/src/components/MkNoteSub.vue | 4 +- .../client/src/scripts/use-note-capture.ts | 44 +++++++++++++------ 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/packages/client/src/components/MkNote.vue b/packages/client/src/components/MkNote.vue index b809ba8a1..7e3fd6be5 100644 --- a/packages/client/src/components/MkNote.vue +++ b/packages/client/src/components/MkNote.vue @@ -259,7 +259,7 @@ const props = defineProps<{ const inChannel = inject("inChannel", null); -let note = $ref(props.note); +let note = $ref(deepClone(props.note)); // plugin if (noteViewInterruptors.length > 0) { diff --git a/packages/client/src/components/MkNoteDetailed.vue b/packages/client/src/components/MkNoteDetailed.vue index aed5c61ae..62b12a909 100644 --- a/packages/client/src/components/MkNoteDetailed.vue +++ b/packages/client/src/components/MkNoteDetailed.vue @@ -108,7 +108,7 @@ const props = defineProps<{ const inChannel = inject("inChannel", null); -let note = $ref(props.note); +let note = $ref(deepClone(props.note)); const enableEmojiReactions = defaultStore.state.enableEmojiReactions; @@ -339,25 +339,6 @@ async function onNoteUpdated(noteData: NoteUpdatedEvent): Promise { } break; - case "updated": - let updatedNote = appearNote; - if (found > 0) { - updatedNote = replies.value[found - 1]; - } - - const editedNote = await os.api("notes/show", { - noteId: id, - }); - - const keys = new Set(); - Object.keys(editedNote) - .concat(Object.keys(updatedNote)) - .forEach((key) => keys.add(key)); - keys.forEach((key) => { - updatedNote[key] = editedNote[key]; - }); - break; - case "deleted": if (found === 0) { isDeleted.value = true; diff --git a/packages/client/src/components/MkNoteSub.vue b/packages/client/src/components/MkNoteSub.vue index d7a43a25e..ca4267c3c 100644 --- a/packages/client/src/components/MkNoteSub.vue +++ b/packages/client/src/components/MkNoteSub.vue @@ -183,6 +183,7 @@ import { reactionPicker } from "@/scripts/reaction-picker"; import { i18n } from "@/i18n"; import { useNoteCapture } from "@/scripts/use-note-capture"; import { defaultStore } from "@/store"; +import { deepClone } from "@/scripts/clone"; const router = useRouter(); @@ -203,7 +204,7 @@ const props = withDefaults( } ); -let note = $ref(props.note); +let note = $ref(deepClone(props.note)); const isRenote = note.renote != null && @@ -236,6 +237,7 @@ const enableEmojiReactions = defaultStore.state.enableEmojiReactions; useNoteCapture({ rootEl: el, note: $$(appearNote), + isDeletedRef: isDeleted, }); function reply(viaKeyboard = false): void { diff --git a/packages/client/src/scripts/use-note-capture.ts b/packages/client/src/scripts/use-note-capture.ts index 42ddcb440..5d0ca5529 100644 --- a/packages/client/src/scripts/use-note-capture.ts +++ b/packages/client/src/scripts/use-note-capture.ts @@ -2,6 +2,7 @@ import { onUnmounted, Ref } from "vue"; import * as misskey from "calckey-js"; import { stream } from "@/stream"; import { $i } from "@/account"; +import * as os from "@/os"; export function useNoteCapture(props: { rootEl: Ref; @@ -11,7 +12,7 @@ export function useNoteCapture(props: { const note = props.note; const connection = $i ? stream : null; - function onStreamNoteUpdated(noteData): void { + async function onStreamNoteUpdated(noteData): Promise { const { type, id, body } = noteData; if (id !== note.value.id) return; @@ -47,7 +48,7 @@ export function useNoteCapture(props: { note.value.reactions[reaction] = Math.max(0, currentCount - 1); if ($i && body.userId === $i.id) { - note.value.myReaction = null; + note.value.myReaction = undefined; } break; } @@ -55,18 +56,20 @@ export function useNoteCapture(props: { case "pollVoted": { const choice = body.choice; - const choices = [...note.value.poll.choices]; - choices[choice] = { - ...choices[choice], - votes: choices[choice].votes + 1, - ...($i && body.userId === $i.id - ? { - isVoted: true, - } - : {}), - }; + if (note.value.poll) { + const choices = [...note.value.poll.choices]; + choices[choice] = { + ...choices[choice], + votes: choices[choice].votes + 1, + ...($i && body.userId === $i.id + ? { + isVoted: true, + } + : {}), + }; + note.value.poll.choices = choices; + } - note.value.poll.choices = choices; break; } @@ -74,6 +77,21 @@ 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; + } } }