From e10a18875152488300a1ed19bddd53100d28d5c9 Mon Sep 17 00:00:00 2001 From: naskya <m@naskya.net> Date: Tue, 11 Jun 2024 23:23:00 +0900 Subject: [PATCH] refactor (backend): port publishNotesStream to backend-rs --- packages/backend-rs/index.d.ts | 1 + packages/backend-rs/index.js | 3 ++- packages/backend-rs/src/service/stream.rs | 1 + packages/backend-rs/src/service/stream/notes.rs | 13 +++++++++++++ packages/backend/src/services/note/create.ts | 9 +++------ packages/backend/src/services/stream.ts | 9 +++++---- 6 files changed, 25 insertions(+), 11 deletions(-) create mode 100644 packages/backend-rs/src/service/stream/notes.rs diff --git a/packages/backend-rs/index.d.ts b/packages/backend-rs/index.d.ts index 5f51bba686..e7d8bb0e76 100644 --- a/packages/backend-rs/index.d.ts +++ b/packages/backend-rs/index.d.ts @@ -1381,6 +1381,7 @@ export interface AbuseUserReportLike { comment: string } export function publishToModerationStream(moderatorId: string, report: AbuseUserReportLike): Promise<void> +export function publishToNotesStream(note: Note): Promise<void> export enum ChatEvent { Message = 0, Read = 1, diff --git a/packages/backend-rs/index.js b/packages/backend-rs/index.js index 6559eef30f..565ca43ce3 100644 --- a/packages/backend-rs/index.js +++ b/packages/backend-rs/index.js @@ -310,7 +310,7 @@ if (!nativeBinding) { throw new Error(`Failed to load native binding`) } -const { SECOND, MINUTE, HOUR, DAY, USER_ONLINE_THRESHOLD, USER_ACTIVE_THRESHOLD, FILE_TYPE_BROWSERSAFE, fetchMeta, updateMetaCache, metaToPugArgs, loadConfig, stringToAcct, acctToString, fetchNodeinfo, nodeinfo_2_1, nodeinfo_2_0, updateNodeinfoCache, Protocol, Inbound, Outbound, greet, initializeRustLogger, showServerInfo, isBlockedServer, isSilencedServer, isAllowedServer, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, isUnicodeEmoji, sqlLikeEscape, safeForSql, formatMilliseconds, getImageSizeFromUrl, isQuote, isSafeUrl, latestVersion, toMastodonId, fromMastodonId, getNoteSummary, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, decodeReaction, countReactions, toDbReaction, removeOldAttestationChallenges, cpuInfo, cpuUsage, memoryUsage, storageUsage, AntennaSrc, DriveFileUsageHint, MutedNoteReason, NoteVisibility, NotificationType, PageVisibility, PollNoteVisibility, RelayStatus, UserEmojiModPerm, UserProfileFfvisibility, UserProfileMutingNotificationTypes, updateAntennasOnNewNote, updateAntennaCache, watchNote, unwatchNote, PushNotificationKind, sendPushNotification, publishToChannelStream, publishToChatStream, ChatIndexEvent, publishToChatIndexStream, publishToBroadcastStream, publishToGroupChatStream, publishToModerationStream, ChatEvent, getTimestamp, genId, genIdAt, generateSecureRandomString, generateUserToken } = nativeBinding +const { SECOND, MINUTE, HOUR, DAY, USER_ONLINE_THRESHOLD, USER_ACTIVE_THRESHOLD, FILE_TYPE_BROWSERSAFE, fetchMeta, updateMetaCache, metaToPugArgs, loadConfig, stringToAcct, acctToString, fetchNodeinfo, nodeinfo_2_1, nodeinfo_2_0, updateNodeinfoCache, Protocol, Inbound, Outbound, greet, initializeRustLogger, showServerInfo, isBlockedServer, isSilencedServer, isAllowedServer, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, isUnicodeEmoji, sqlLikeEscape, safeForSql, formatMilliseconds, getImageSizeFromUrl, isQuote, isSafeUrl, latestVersion, toMastodonId, fromMastodonId, getNoteSummary, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, decodeReaction, countReactions, toDbReaction, removeOldAttestationChallenges, cpuInfo, cpuUsage, memoryUsage, storageUsage, AntennaSrc, DriveFileUsageHint, MutedNoteReason, NoteVisibility, NotificationType, PageVisibility, PollNoteVisibility, RelayStatus, UserEmojiModPerm, UserProfileFfvisibility, UserProfileMutingNotificationTypes, updateAntennasOnNewNote, updateAntennaCache, watchNote, unwatchNote, PushNotificationKind, sendPushNotification, publishToChannelStream, publishToChatStream, ChatIndexEvent, publishToChatIndexStream, publishToBroadcastStream, publishToGroupChatStream, publishToModerationStream, publishToNotesStream, ChatEvent, getTimestamp, genId, genIdAt, generateSecureRandomString, generateUserToken } = nativeBinding module.exports.SECOND = SECOND module.exports.MINUTE = MINUTE @@ -391,6 +391,7 @@ module.exports.publishToChatIndexStream = publishToChatIndexStream module.exports.publishToBroadcastStream = publishToBroadcastStream module.exports.publishToGroupChatStream = publishToGroupChatStream module.exports.publishToModerationStream = publishToModerationStream +module.exports.publishToNotesStream = publishToNotesStream module.exports.ChatEvent = ChatEvent module.exports.getTimestamp = getTimestamp module.exports.genId = genId diff --git a/packages/backend-rs/src/service/stream.rs b/packages/backend-rs/src/service/stream.rs index 4a72fc7ec0..baf3a278d3 100644 --- a/packages/backend-rs/src/service/stream.rs +++ b/packages/backend-rs/src/service/stream.rs @@ -5,6 +5,7 @@ pub mod chat_index; pub mod custom_emoji; pub mod group_chat; pub mod moderation; +pub mod notes; use crate::{ config::CONFIG, diff --git a/packages/backend-rs/src/service/stream/notes.rs b/packages/backend-rs/src/service/stream/notes.rs new file mode 100644 index 0000000000..6c2336e347 --- /dev/null +++ b/packages/backend-rs/src/service/stream/notes.rs @@ -0,0 +1,13 @@ +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; + +#[crate::export(js_name = "publishToNotesStream")] +pub async fn publish(note: &Note) -> Result<(), Error> { + publish_to_stream(&Stream::Notes, None, Some(serde_json::to_string(note)?)).await +} diff --git a/packages/backend/src/services/note/create.ts b/packages/backend/src/services/note/create.ts index b37c007152..1dd2653845 100644 --- a/packages/backend/src/services/note/create.ts +++ b/packages/backend/src/services/note/create.ts @@ -1,9 +1,5 @@ import * as mfm from "mfm-js"; -import { - publishMainStream, - publishNotesStream, - publishNoteStream, -} from "@/services/stream.js"; +import { publishMainStream, publishNoteStream } from "@/services/stream.js"; import DeliverManager from "@/remote/activitypub/deliver-manager.js"; import renderNote from "@/remote/activitypub/renderer/note.js"; import renderCreate from "@/remote/activitypub/renderer/create.js"; @@ -49,6 +45,7 @@ import { genIdAt, isQuote, isSilencedServer, + publishToNotesStream, } from "backend-rs"; import { countSameRenotes } from "@/misc/count-same-renotes.js"; import { deliverToRelays, getCachedRelays } from "../relay.js"; @@ -661,7 +658,7 @@ export default async ( 30, ); } - publishNotesStream(noteToPublish); + publishToNotesStream(toRustObject(noteToPublish)); } } finally { await lock.release(); diff --git a/packages/backend/src/services/stream.ts b/packages/backend/src/services/stream.ts index 36914d4d41..c60d5ce974 100644 --- a/packages/backend/src/services/stream.ts +++ b/packages/backend/src/services/stream.ts @@ -193,9 +193,10 @@ class Publisher { // ); // }; - public publishNotesStream = (note: Note): void => { - this.publish("notesStream", null, note); - }; + /* ported to backend-rs */ + // public publishNotesStream = (note: Note): void => { + // this.publish("notesStream", null, note); + // }; /* ported to backend-rs */ // public publishAdminStream = <K extends keyof AdminStreamTypes>( @@ -221,7 +222,7 @@ export const publishUserEvent = publisher.publishUserEvent; export const publishMainStream = publisher.publishMainStream; export const publishDriveStream = publisher.publishDriveStream; export const publishNoteStream = publisher.publishNoteStream; -export const publishNotesStream = publisher.publishNotesStream; +// export const publishNotesStream = publisher.publishNotesStream; // export const publishChannelStream = publisher.publishChannelStream; export const publishUserListStream = publisher.publishUserListStream; // export const publishAntennaStream = publisher.publishAntennaStream;