refactor (backend): port publishGroupMessagingStream to backend-rs

This commit is contained in:
naskya 2024-04-27 08:35:01 +09:00
parent d880601d56
commit 38cd4bafde
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
9 changed files with 54 additions and 39 deletions

View file

@ -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

View file

@ -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

View file

@ -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;

View file

@ -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)?),
)
}

View file

@ -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);

View file

@ -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);
}
}
}

View file

@ -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,
});

View file

@ -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);
}
}

View file

@ -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;