From 38cd4bafde0e92e5d029c65d982c252779438547 Mon Sep 17 00:00:00 2001 From: naskya Date: Sat, 27 Apr 2024 08:35:01 +0900 Subject: [PATCH] refactor (backend): port publishGroupMessagingStream to backend-rs --- packages/backend-rs/index.d.ts | 1 + packages/backend-rs/index.js | 3 +- packages/backend-rs/src/service/stream.rs | 1 + .../src/service/stream/group_chat.rs | 13 +++++++ .../api/common/read-messaging-message.ts | 10 +++--- .../backend/src/server/api/stream/index.ts | 6 ++-- .../backend/src/services/messages/create.ts | 14 ++++---- .../backend/src/services/messages/delete.ts | 11 +++--- packages/backend/src/services/stream.ts | 34 +++++++++---------- 9 files changed, 54 insertions(+), 39 deletions(-) create mode 100644 packages/backend-rs/src/service/stream/group_chat.rs diff --git a/packages/backend-rs/index.d.ts b/packages/backend-rs/index.d.ts index 85fd9fa97a..5b9d0ee894 100644 --- a/packages/backend-rs/index.d.ts +++ b/packages/backend-rs/index.d.ts @@ -1174,6 +1174,7 @@ export interface PackedEmoji { height: number | null } export function publishToBroadcastStream(emoji: PackedEmoji): void +export function publishToGroupChatStream(groupId: string, kind: ChatEvent, object: any): void export interface AbuseUserReportLike { id: string targetUserId: string diff --git a/packages/backend-rs/index.js b/packages/backend-rs/index.js index 663bf917f9..b351840dfe 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, publishToBroadcastStream, 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, publishToGroupChatStream, publishToModerationStream, getTimestamp, genId, genIdAt, secureRndstr } = nativeBinding module.exports.SECOND = SECOND module.exports.MINUTE = MINUTE @@ -371,6 +371,7 @@ module.exports.publishToChatStream = publishToChatStream module.exports.ChatIndexEvent = ChatIndexEvent module.exports.publishToChatIndexStream = publishToChatIndexStream module.exports.publishToBroadcastStream = publishToBroadcastStream +module.exports.publishToGroupChatStream = publishToGroupChatStream 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 3f53dea131..279d343f10 100644 --- a/packages/backend-rs/src/service/stream.rs +++ b/packages/backend-rs/src/service/stream.rs @@ -3,6 +3,7 @@ pub mod channel; pub mod chat; pub mod chat_index; pub mod custom_emoji; +pub mod group_chat; pub mod moderation; use crate::config::CONFIG; diff --git a/packages/backend-rs/src/service/stream/group_chat.rs b/packages/backend-rs/src/service/stream/group_chat.rs new file mode 100644 index 0000000000..1e676bbef5 --- /dev/null +++ b/packages/backend-rs/src/service/stream/group_chat.rs @@ -0,0 +1,13 @@ +use crate::service::stream::{chat::ChatEvent, publish_to_stream, Error, Stream}; + +// We want to merge `kind` and `object` into a single enum +// https://github.com/napi-rs/napi-rs/issues/2036 + +#[crate::export(js_name = "publishToGroupChatStream")] +pub fn publish(group_id: String, kind: ChatEvent, object: &serde_json::Value) -> Result<(), Error> { + publish_to_stream( + &Stream::GroupChat { group_id }, + Some(kind.to_string()), + Some(serde_json::to_string(object)?), + ) +} diff --git a/packages/backend/src/server/api/common/read-messaging-message.ts b/packages/backend/src/server/api/common/read-messaging-message.ts index 47b956e158..f322431608 100644 --- a/packages/backend/src/server/api/common/read-messaging-message.ts +++ b/packages/backend/src/server/api/common/read-messaging-message.ts @@ -1,9 +1,7 @@ -import { - publishMainStream, - publishGroupMessagingStream, -} from "@/services/stream.js"; +import { publishMainStream } from "@/services/stream.js"; import { publishToChatStream, + publishToGroupChatStream, publishToChatIndexStream, ChatEvent, ChatIndexEvent, @@ -130,9 +128,9 @@ export async function readGroupMessagingMessage( } // Publish event - publishGroupMessagingStream(groupId, "read", { + publishToGroupChatStream(groupId, ChatEvent.Read, { ids: reads, - userId: userId, + userId, }); publishToChatIndexStream(userId, ChatIndexEvent.Read, reads); diff --git a/packages/backend/src/server/api/stream/index.ts b/packages/backend/src/server/api/stream/index.ts index 4079573ffb..3bb0ca073b 100644 --- a/packages/backend/src/server/api/stream/index.ts +++ b/packages/backend/src/server/api/stream/index.ts @@ -14,10 +14,10 @@ import { } from "@/models/index.js"; import type { AccessToken } from "@/models/entities/access-token.js"; import type { UserProfile } from "@/models/entities/user-profile.js"; -import { publishGroupMessagingStream } from "@/services/stream.js"; import { publishToChannelStream, publishToChatStream, + publishToGroupChatStream, ChatEvent, } from "backend-rs"; import type { UserGroup } from "@/models/entities/user-group.js"; @@ -531,8 +531,8 @@ export default class Connection { ChatEvent.Typing, this.user.id, ); - } else if (param.group) { - publishGroupMessagingStream(param.group, "typing", this.user.id); + } else if (param.group != null) { + publishToGroupChatStream(param.group, ChatEvent.Typing, this.user.id); } } } diff --git a/packages/backend/src/services/messages/create.ts b/packages/backend/src/services/messages/create.ts index 269d1d0e3c..d025f57fca 100644 --- a/packages/backend/src/services/messages/create.ts +++ b/packages/backend/src/services/messages/create.ts @@ -10,16 +10,14 @@ import { import { genId, publishToChatStream, + publishToGroupChatStream, publishToChatIndexStream, toPuny, ChatEvent, ChatIndexEvent, } from "backend-rs"; import type { MessagingMessage } from "@/models/entities/messaging-message.js"; -import { - publishMainStream, - publishGroupMessagingStream, -} from "@/services/stream.js"; +import { publishMainStream } from "@/services/stream.js"; import { pushNotification } from "@/services/push-notification.js"; import { Not } from "typeorm"; import type { Note } from "@/models/entities/note.js"; @@ -86,11 +84,11 @@ export async function createMessage( ); publishMainStream(recipientUser.id, "messagingMessage", messageObj); } - } else if (recipientGroup) { - // グループのストリーム - publishGroupMessagingStream(recipientGroup.id, "message", messageObj); + } else if (recipientGroup != null) { + // group's stream + publishToGroupChatStream(recipientGroup.id, ChatEvent.Message, messageObj); - // メンバーのストリーム + // member's stream const joinings = await UserGroupJoinings.findBy({ userGroupId: recipientGroup.id, }); diff --git a/packages/backend/src/services/messages/delete.ts b/packages/backend/src/services/messages/delete.ts index 745c89380d..5fc4c812e2 100644 --- a/packages/backend/src/services/messages/delete.ts +++ b/packages/backend/src/services/messages/delete.ts @@ -1,8 +1,11 @@ import { config } from "@/config.js"; import { MessagingMessages, Users } from "@/models/index.js"; import type { MessagingMessage } from "@/models/entities/messaging-message.js"; -import { publishGroupMessagingStream } from "@/services/stream.js"; -import { publishToChatStream, ChatEvent } from "backend-rs"; +import { + publishToChatStream, + publishToGroupChatStream, + ChatEvent, +} from "backend-rs"; import { renderActivity } from "@/remote/activitypub/renderer/index.js"; import renderDelete from "@/remote/activitypub/renderer/delete.js"; import renderTombstone from "@/remote/activitypub/renderer/tombstone.js"; @@ -42,7 +45,7 @@ async function postDeleteMessage(message: MessagingMessage) { ); deliver(user, activity, recipient.inbox); } - } else if (message.groupId) { - publishGroupMessagingStream(message.groupId, "deleted", message.id); + } else if (message.groupId != null) { + publishToGroupChatStream(message.groupId, ChatEvent.Deleted, message.id); } } diff --git a/packages/backend/src/services/stream.ts b/packages/backend/src/services/stream.ts index 1a821302ce..36914d4d41 100644 --- a/packages/backend/src/services/stream.ts +++ b/packages/backend/src/services/stream.ts @@ -2,7 +2,7 @@ import { redisClient } from "@/db/redis.js"; import type { User } from "@/models/entities/user.js"; import type { Note } from "@/models/entities/note.js"; import type { UserList } from "@/models/entities/user-list.js"; -import type { UserGroup } from "@/models/entities/user-group.js"; +// import type { UserGroup } from "@/models/entities/user-group.js"; import { config } from "@/config.js"; // import type { Antenna } from "@/models/entities/antenna.js"; // import type { Channel } from "@/models/entities/channel.js"; @@ -13,7 +13,7 @@ import type { // BroadcastTypes, // ChannelStreamTypes, DriveStreamTypes, - GroupMessagingStreamTypes, + // GroupMessagingStreamTypes, InternalStreamTypes, MainStreamTypes, // MessagingIndexStreamTypes, @@ -163,19 +163,20 @@ class Publisher { // ); // }; - public publishGroupMessagingStream = < - K extends keyof GroupMessagingStreamTypes, - >( - groupId: UserGroup["id"], - type: K, - value?: GroupMessagingStreamTypes[K], - ): void => { - this.publish( - `messagingStream:${groupId}`, - type, - typeof value === "undefined" ? null : value, - ); - }; + /* ported to backend-rs */ + // public publishGroupMessagingStream = < + // K extends keyof GroupMessagingStreamTypes, + // >( + // groupId: UserGroup["id"], + // type: K, + // value?: GroupMessagingStreamTypes[K], + // ): void => { + // this.publish( + // `messagingStream:${groupId}`, + // type, + // typeof value === "undefined" ? null : value, + // ); + // }; /* ported to backend-rs */ // public publishMessagingIndexStream = < @@ -225,7 +226,6 @@ export const publishNotesStream = publisher.publishNotesStream; export const publishUserListStream = publisher.publishUserListStream; // export const publishAntennaStream = publisher.publishAntennaStream; // export const publishMessagingStream = publisher.publishMessagingStream; -export const publishGroupMessagingStream = - publisher.publishGroupMessagingStream; +// export const publishGroupMessagingStream = publisher.publishGroupMessagingStream; // export const publishMessagingIndexStream = publisher.publishMessagingIndexStream; // export const publishAdminStream = publisher.publishAdminStream;