From 7d2e98963d202caef8c679444cb42e5e76c4be12 Mon Sep 17 00:00:00 2001 From: naskya Date: Thu, 25 Jul 2024 06:39:40 +0900 Subject: [PATCH] refactor (backend): port publishNoteUpdatesStream to backend-rs --- packages/backend-rs/index.d.ts | 2 ++ packages/backend-rs/index.js | 1 + packages/backend-rs/src/service/stream.rs | 3 +++ .../backend-rs/src/service/stream/note_edit.rs | 18 ++++++++++++++++++ packages/backend/src/const.ts | 1 - .../server/api/endpoints/notes/translate.ts | 6 +----- .../backend/src/services/drive/add-file.ts | 6 +----- packages/backend/src/services/note/edit.ts | 9 +++------ packages/backend/src/services/stream.ts | 17 +++++++++-------- 9 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 packages/backend-rs/src/service/stream/note_edit.rs diff --git a/packages/backend-rs/index.d.ts b/packages/backend-rs/index.d.ts index 3385d524a0..63d845f587 100644 --- a/packages/backend-rs/index.d.ts +++ b/packages/backend-rs/index.d.ts @@ -1117,6 +1117,8 @@ export declare function publishToModerationStream(moderatorId: string, report: A export declare function publishToNotesStream(note: Note): Promise +export declare function publishToNoteUpdatesStream(note: Note): Promise + export interface PugArgs { img: string | null title: string diff --git a/packages/backend-rs/index.js b/packages/backend-rs/index.js index 4d6d72e1cb..673bdc8d52 100644 --- a/packages/backend-rs/index.js +++ b/packages/backend-rs/index.js @@ -424,6 +424,7 @@ module.exports.publishToDriveFolderStream = nativeBinding.publishToDriveFolderSt module.exports.publishToGroupChatStream = nativeBinding.publishToGroupChatStream module.exports.publishToModerationStream = nativeBinding.publishToModerationStream module.exports.publishToNotesStream = nativeBinding.publishToNotesStream +module.exports.publishToNoteUpdatesStream = nativeBinding.publishToNoteUpdatesStream module.exports.PushNotificationKind = nativeBinding.PushNotificationKind module.exports.PushSubscriptionType = nativeBinding.PushSubscriptionType module.exports.RelayStatus = nativeBinding.RelayStatus diff --git a/packages/backend-rs/src/service/stream.rs b/packages/backend-rs/src/service/stream.rs index d1acbfa4a7..cd386e7115 100644 --- a/packages/backend-rs/src/service/stream.rs +++ b/packages/backend-rs/src/service/stream.rs @@ -6,6 +6,7 @@ pub mod custom_emoji; pub mod drive; pub mod group_chat; pub mod moderation; +pub mod note_edit; pub mod notes; use crate::{ @@ -30,6 +31,7 @@ pub enum Stream { note_id: String, }, Notes, + NoteEdit, UserList { list_id: String, }, @@ -86,6 +88,7 @@ pub async fn publish_to_stream( Stream::User { user_id } => format!("user:{user_id}"), Stream::Channel { channel_id } => format!("channelStream:{channel_id}"), Stream::Note { note_id } => format!("noteStream:{note_id}"), + Stream::NoteEdit => format!("noteUpdatesStream"), Stream::Notes => "notesStream".to_owned(), Stream::UserList { list_id } => format!("userListStream:{list_id}"), Stream::Main { user_id } => format!("mainStream:{user_id}"), diff --git a/packages/backend-rs/src/service/stream/note_edit.rs b/packages/backend-rs/src/service/stream/note_edit.rs new file mode 100644 index 0000000000..3b54a35359 --- /dev/null +++ b/packages/backend-rs/src/service/stream/note_edit.rs @@ -0,0 +1,18 @@ +use crate::{ + model::entity::note, + service::stream::{publish_to_stream, Error, Stream}, +}; + +// for napi export +// https://github.com/napi-rs/napi-rs/issues/2060 +type Note = note::Model; + +#[macros::export(js_name = "publishToNoteUpdatesStream")] +pub async fn publish(note: &Note) -> Result<(), Error> { + publish_to_stream( + &Stream::NoteEdit, + Some("updated"), + Some(serde_json::to_string(note)?), + ) + .await +} diff --git a/packages/backend/src/const.ts b/packages/backend/src/const.ts index fee1e3e1ea..78aafc3075 100644 --- a/packages/backend/src/const.ts +++ b/packages/backend/src/const.ts @@ -18,7 +18,6 @@ export const DB_MAX_NOTE_TEXT_LENGTH = 100000; */ export const DB_MAX_IMAGE_COMMENT_LENGTH = 8192; - export const MAX_NOTE_TEXT_LENGTH = Math.min( config.maxNoteLength ?? 3000, DB_MAX_NOTE_TEXT_LENGTH, diff --git a/packages/backend/src/server/api/endpoints/notes/translate.ts b/packages/backend/src/server/api/endpoints/notes/translate.ts index c939ab231a..9362c2c9fe 100644 --- a/packages/backend/src/server/api/endpoints/notes/translate.ts +++ b/packages/backend/src/server/api/endpoints/notes/translate.ts @@ -44,9 +44,5 @@ export default define(meta, paramDef, async (ps, user) => { return 204; } - return translate( - note.text, - note.lang as string | null, - ps.targetLang, - ); + return translate(note.text, note.lang as string | null, ps.targetLang); }); diff --git a/packages/backend/src/services/drive/add-file.ts b/packages/backend/src/services/drive/add-file.ts index c729a5e7a0..3525082bc0 100644 --- a/packages/backend/src/services/drive/add-file.ts +++ b/packages/backend/src/services/drive/add-file.ts @@ -6,11 +6,7 @@ import type S3 from "aws-sdk/clients/s3.js"; // TODO: migrate to SDK v3 import sharp from "sharp"; import { IsNull } from "typeorm"; import { publishMainStream } from "@/services/stream.js"; -import { - fetchMeta, - genId, - publishToDriveFileStream, -} from "backend-rs"; +import { fetchMeta, genId, publishToDriveFileStream } from "backend-rs"; import { FILE_TYPE_BROWSERSAFE } from "@/const.js"; import { contentDisposition } from "@/misc/content-disposition.js"; import { getFileInfo } from "@/misc/get-file-info.js"; diff --git a/packages/backend/src/services/note/edit.ts b/packages/backend/src/services/note/edit.ts index 8b5116a99b..04bfb3c9bd 100644 --- a/packages/backend/src/services/note/edit.ts +++ b/packages/backend/src/services/note/edit.ts @@ -1,8 +1,5 @@ import * as mfm from "mfm-js"; -import { - publishNoteStream, - publishNoteUpdatesStream, -} from "@/services/stream.js"; +import { publishNoteStream } from "@/services/stream.js"; import DeliverManager from "@/remote/activitypub/deliver-manager.js"; import renderNote from "@/remote/activitypub/renderer/note.js"; import { renderActivity } from "@/remote/activitypub/renderer/index.js"; @@ -21,7 +18,7 @@ import { import type { DriveFile } from "@/models/entities/drive-file.js"; import { In } from "typeorm"; import type { ILocalUser, IRemoteUser } from "@/models/entities/user.js"; -import { genId } from "backend-rs"; +import { genId, publishToNoteUpdatesStream } from "backend-rs"; import type { IPoll } from "@/models/entities/poll.js"; import { deliverToRelays } from "../relay.js"; import renderUpdate from "@/remote/activitypub/renderer/update.js"; @@ -194,7 +191,7 @@ export default async function ( updatedAt: update.updatedAt, }); - publishNoteUpdatesStream("updated", note); + publishToNoteUpdatesStream(note); (async () => { const noteActivity = await renderNote(note, false); diff --git a/packages/backend/src/services/stream.ts b/packages/backend/src/services/stream.ts index d1094b1454..d5a89c82c5 100644 --- a/packages/backend/src/services/stream.ts +++ b/packages/backend/src/services/stream.ts @@ -21,7 +21,7 @@ import type { NoteStreamTypes, UserListStreamTypes, UserStreamTypes, - NoteUpdatesStreamTypes, + // NoteUpdatesStreamTypes, } from "@/server/api/stream/types.js"; class Publisher { @@ -113,12 +113,13 @@ class Publisher { }); }; - public publishNoteUpdatesStream = ( - type: K, - value: NoteUpdatesStreamTypes[K], - ): void => { - this.publish("noteUpdatesStream", type, value); - }; + /* ported to backend-rs */ + // public publishNoteUpdatesStream = ( + // type: K, + // value: NoteUpdatesStreamTypes[K], + // ): void => { + // this.publish("noteUpdatesStream", type, value); + // }; /* ported to backend-rs */ // public publishChannelStream = ( @@ -232,7 +233,7 @@ export const publishMainStream = publisher.publishMainStream; // export const publishDriveStream = publisher.publishDriveStream; export const publishNoteStream = publisher.publishNoteStream; // export const publishNotesStream = publisher.publishNotesStream; -export const publishNoteUpdatesStream = publisher.publishNoteUpdatesStream; +// export const publishNoteUpdatesStream = publisher.publishNoteUpdatesStream; // export const publishChannelStream = publisher.publishChannelStream; export const publishUserListStream = publisher.publishUserListStream; // export const publishAntennaStream = publisher.publishAntennaStream;