diff --git a/packages/backend-rs/index.d.ts b/packages/backend-rs/index.d.ts index b6042d2682..85fd9fa97a 100644 --- a/packages/backend-rs/index.d.ts +++ b/packages/backend-rs/index.d.ts @@ -1162,6 +1162,18 @@ export enum ChatIndexEvent { Read = 'read' } export function publishToChatIndexStream(userId: string, kind: ChatIndexEvent, object: any): void +export interface PackedEmoji { + id: string + aliases: Array + name: string + category: string | null + host: string | null + url: string + license: string | null + width: number | null + height: number | null +} +export function publishToBroadcastStream(emoji: PackedEmoji): void export interface AbuseUserReportLike { id: string targetUserId: string diff --git a/packages/backend-rs/index.js b/packages/backend-rs/index.js index 7244999306..663bf917f9 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, loadEnv, loadConfig, stringToAcct, acctToString, addNoteToAntenna, isBlockedServer, isSilencedServer, isAllowedServer, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, isUnicodeEmoji, sqlLikeEscape, safeForSql, formatMilliseconds, getImageSizeFromUrl, getNoteSummary, toMastodonId, fromMastodonId, fetchMeta, metaToPugArgs, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, decodeReaction, countReactions, toDbReaction, removeOldAttestationChallenges, AntennaSrcEnum, DriveFileUsageHintEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, initializeRustLogger, watchNote, unwatchNote, publishToChannelStream, ChatEvent, publishToChatStream, ChatIndexEvent, publishToChatIndexStream, publishToModerationStream, getTimestamp, genId, genIdAt, secureRndstr } = nativeBinding +const { SECOND, MINUTE, HOUR, DAY, USER_ONLINE_THRESHOLD, USER_ACTIVE_THRESHOLD, FILE_TYPE_BROWSERSAFE, loadEnv, loadConfig, stringToAcct, acctToString, addNoteToAntenna, isBlockedServer, isSilencedServer, isAllowedServer, checkWordMute, getFullApAccount, isSelfHost, isSameOrigin, extractHost, toPuny, isUnicodeEmoji, sqlLikeEscape, safeForSql, formatMilliseconds, getImageSizeFromUrl, getNoteSummary, toMastodonId, fromMastodonId, fetchMeta, metaToPugArgs, nyaify, hashPassword, verifyPassword, isOldPasswordAlgorithm, decodeReaction, countReactions, toDbReaction, removeOldAttestationChallenges, AntennaSrcEnum, DriveFileUsageHintEnum, MutedNoteReasonEnum, NoteVisibilityEnum, NotificationTypeEnum, PageVisibilityEnum, PollNotevisibilityEnum, RelayStatusEnum, UserEmojimodpermEnum, UserProfileFfvisibilityEnum, UserProfileMutingnotificationtypesEnum, initializeRustLogger, watchNote, unwatchNote, publishToChannelStream, ChatEvent, publishToChatStream, ChatIndexEvent, publishToChatIndexStream, publishToBroadcastStream, publishToModerationStream, getTimestamp, genId, genIdAt, secureRndstr } = nativeBinding module.exports.SECOND = SECOND module.exports.MINUTE = MINUTE @@ -370,6 +370,7 @@ module.exports.ChatEvent = ChatEvent module.exports.publishToChatStream = publishToChatStream module.exports.ChatIndexEvent = ChatIndexEvent module.exports.publishToChatIndexStream = publishToChatIndexStream +module.exports.publishToBroadcastStream = publishToBroadcastStream module.exports.publishToModerationStream = publishToModerationStream 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 dbf0c621c3..3f53dea131 100644 --- a/packages/backend-rs/src/service/stream.rs +++ b/packages/backend-rs/src/service/stream.rs @@ -2,6 +2,7 @@ pub mod antenna; pub mod channel; pub mod chat; pub mod chat_index; +pub mod custom_emoji; pub mod moderation; use crate::config::CONFIG; @@ -13,7 +14,7 @@ pub enum Stream { #[strum(serialize = "internal")] Internal, #[strum(serialize = "broadcast")] - Broadcast, + CustomEmoji, #[strum(to_string = "adminStream:{moderator_id}")] Moderation { moderator_id: String }, #[strum(to_string = "user:{user_id}")] @@ -84,7 +85,7 @@ mod unit_test { #[test] fn channel_to_string() { assert_eq!(Stream::Internal.to_string(), "internal"); - assert_eq!(Stream::Broadcast.to_string(), "broadcast"); + assert_eq!(Stream::CustomEmoji.to_string(), "broadcast"); assert_eq!( Stream::Moderation { moderator_id: "9tb42br63g5apjcq".to_string() diff --git a/packages/backend-rs/src/service/stream/custom_emoji.rs b/packages/backend-rs/src/service/stream/custom_emoji.rs new file mode 100644 index 0000000000..21158fc761 --- /dev/null +++ b/packages/backend-rs/src/service/stream/custom_emoji.rs @@ -0,0 +1,27 @@ +use crate::service::stream::{publish_to_stream, Error, Stream}; +use serde::{Deserialize, Serialize}; + +// TODO: define schema type in other place +#[derive(Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +#[crate::export(object)] +pub struct PackedEmoji { + pub id: String, + pub aliases: Vec, + pub name: String, + pub category: Option, + pub host: Option, + pub url: String, + pub license: Option, + pub width: Option, + pub height: Option, +} + +#[crate::export(js_name = "publishToBroadcastStream")] +pub fn publish(emoji: &PackedEmoji) -> Result<(), Error> { + publish_to_stream( + &Stream::CustomEmoji, + Some("emojiAdded".to_string()), + Some(format!("{{\"emoji\":{}}}", serde_json::to_string(emoji)?)), + ) +} diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/add.ts b/packages/backend/src/server/api/endpoints/admin/emoji/add.ts index 5408c9dc7a..59139904bc 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/add.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/add.ts @@ -1,10 +1,14 @@ import define from "@/server/api/define.js"; import { Emojis, DriveFiles } from "@/models/index.js"; -import { type ImageSize, genId, getImageSizeFromUrl } from "backend-rs"; +import { + type ImageSize, + genId, + getImageSizeFromUrl, + publishToBroadcastStream, +} from "backend-rs"; import { insertModerationLog } from "@/services/insert-moderation-log.js"; import { ApiError } from "@/server/api/error.js"; import rndstr from "rndstr"; -import { publishBroadcastStream } from "@/services/stream.js"; import { db } from "@/db/postgre.js"; import { apiLogger } from "@/server/api/logger.js"; import { inspect } from "node:util"; @@ -75,9 +79,7 @@ export default define(meta, paramDef, async (ps, me) => { await db.queryResultCache!.remove(["meta_emojis"]); - publishBroadcastStream("emojiAdded", { - emoji: await Emojis.pack(emoji.id), - }); + publishToBroadcastStream(await Emojis.pack(emoji)); insertModerationLog(me, "addEmoji", { emojiId: emoji.id, diff --git a/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts b/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts index ec8701a6c5..9b08076b35 100644 --- a/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts +++ b/packages/backend/src/server/api/endpoints/admin/emoji/copy.ts @@ -1,10 +1,14 @@ import define from "@/server/api/define.js"; import { Emojis } from "@/models/index.js"; -import { type ImageSize, genId, getImageSizeFromUrl } from "backend-rs"; +import { + type ImageSize, + genId, + getImageSizeFromUrl, + publishToBroadcastStream, +} from "backend-rs"; import { ApiError } from "@/server/api/error.js"; import type { DriveFile } from "@/models/entities/drive-file.js"; import { uploadFromUrl } from "@/services/drive/upload-from-url.js"; -import { publishBroadcastStream } from "@/services/stream.js"; import { db } from "@/db/postgre.js"; import { apiLogger } from "@/server/api/logger.js"; import { inspect } from "node:util"; @@ -102,9 +106,7 @@ export default define(meta, paramDef, async (ps, me) => { await db.queryResultCache!.remove(["meta_emojis"]); - publishBroadcastStream("emojiAdded", { - emoji: await Emojis.pack(copied.id), - }); + publishToBroadcastStream(await Emojis.pack(copied)); return { id: copied.id, diff --git a/packages/backend/src/services/stream.ts b/packages/backend/src/services/stream.ts index 4ee0a62553..1a821302ce 100644 --- a/packages/backend/src/services/stream.ts +++ b/packages/backend/src/services/stream.ts @@ -10,7 +10,7 @@ import type { StreamChannels, // AdminStreamTypes, // AntennaStreamTypes, - BroadcastTypes, + // BroadcastTypes, // ChannelStreamTypes, DriveStreamTypes, GroupMessagingStreamTypes, @@ -64,16 +64,17 @@ class Publisher { ); }; - public publishBroadcastStream = ( - type: K, - value?: BroadcastTypes[K], - ): void => { - this.publish( - "broadcast", - type, - typeof value === "undefined" ? null : value, - ); - }; + /* ported to backend-rs */ + // public publishBroadcastStream = ( + // type: K, + // value?: BroadcastTypes[K], + // ): void => { + // this.publish( + // "broadcast", + // type, + // typeof value === "undefined" ? null : value, + // ); + // }; public publishMainStream = ( userId: User["id"], @@ -215,7 +216,7 @@ export default publisher; export const publishInternalEvent = publisher.publishInternalEvent; export const publishUserEvent = publisher.publishUserEvent; -export const publishBroadcastStream = publisher.publishBroadcastStream; +// export const publishBroadcastStream = publisher.publishBroadcastStream; export const publishMainStream = publisher.publishMainStream; export const publishDriveStream = publisher.publishDriveStream; export const publishNoteStream = publisher.publishNoteStream;