From c1268c04f8d0eb914ddf9f0a78e1368a6d7303b5 Mon Sep 17 00:00:00 2001 From: Johann150 Date: Mon, 5 Jun 2023 22:26:05 +0200 Subject: [PATCH] server: add noteStream handling for updated notes --- .../backend/src/server/api/stream/index.ts | 45 ++++++++++++++++--- .../backend/src/server/api/stream/types.ts | 3 ++ packages/foundkey-js/src/streaming.types.ts | 6 +++ 3 files changed, 48 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/server/api/stream/index.ts b/packages/backend/src/server/api/stream/index.ts index ace59d32d..bb0b17310 100644 --- a/packages/backend/src/server/api/stream/index.ts +++ b/packages/backend/src/server/api/stream/index.ts @@ -3,7 +3,7 @@ import { WebSocket } from 'ws'; import { readNote } from '@/services/note/read.js'; import { User } from '@/models/entities/user.js'; import { Channel as ChannelModel } from '@/models/entities/channel.js'; -import { Followings, Mutings, RenoteMutings, UserProfiles, ChannelFollowings, Blockings } from '@/models/index.js'; +import { Followings, Mutings, Notes, RenoteMutings, UserProfiles, ChannelFollowings, Blockings } from '@/models/index.js'; import { AccessToken } from '@/models/entities/access-token.js'; import { UserProfile } from '@/models/entities/user-profile.js'; import { publishChannelStream, publishGroupMessagingStream, publishMessagingStream } from '@/services/stream.js'; @@ -14,6 +14,7 @@ import { channels } from './channels/index.js'; import Channel from './channel.js'; import { StreamEventEmitter, StreamMessages } from './types.js'; import Logger from '@/services/logger.js'; +import { IdentifiableError } from '@/misc/identifiable-error.js'; const logger = new Logger('streaming'); @@ -254,11 +255,43 @@ export class Connection { } private async onNoteStreamMessage(data: StreamMessages['note']['payload']) { - this.sendMessageToWs('noteUpdated', { - id: data.body.id, - type: data.type, - body: data.body.body, - }); + if (data.type === 'updated') { + const note = data.body.body.note; + // FIXME analogous to Channel.withPackedNote, but for some reason, the note + // stream is not handled as a channel but instead handled at the top level + // so this code is duplicated here I guess. + try { + // because `note` was previously JSON.stringify'ed, the fields that + // were objects before are now strings and have to be restored or + // removed from the object + note.createdAt = new Date(note.createdAt); + note.reply = null; + note.renote = null; + note.user = null; + note.channel = null; + + const packed = await Notes.pack(note, this.user, { detail: true }); + + this.sendMessageToWs('noteUpdated', { + id: data.body.id, + type: 'updated', + body: { note: packed }, + }); + } catch (err) { + if (err instanceof IdentifiableError && err.id === '9725d0ce-ba28-4dde-95a7-2cbb2c15de24') { + // skip: note not visible to user + return; + } else { + logger.error(err); + } + } + } else { + this.sendMessageToWs('noteUpdated', { + id: data.body.id, + type: data.type, + body: data.body.body, + }); + } } /** diff --git a/packages/backend/src/server/api/stream/types.ts b/packages/backend/src/server/api/stream/types.ts index 6fab792ae..08ac6f694 100644 --- a/packages/backend/src/server/api/stream/types.ts +++ b/packages/backend/src/server/api/stream/types.ts @@ -128,6 +128,9 @@ export interface NoteStreamTypes { reaction: string; userId: User['id']; }; + updated: { + note: Note; + }; } type NoteStreamEventTypes = { [key in keyof NoteStreamTypes]: { diff --git a/packages/foundkey-js/src/streaming.types.ts b/packages/foundkey-js/src/streaming.types.ts index 84c948538..942b1975c 100644 --- a/packages/foundkey-js/src/streaming.types.ts +++ b/packages/foundkey-js/src/streaming.types.ts @@ -142,6 +142,12 @@ export type NoteUpdatedEvent = { choice: number; userId: User['id']; }; +} | { + id: Note['id']; + type: 'updated'; + body: { + note: Note; + }; }; export type BroadcastEvents = {