rudeshark.net/packages/backend/src/services/messages/create.ts

149 lines
4.6 KiB
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import type { CacheableUser, User } from "@/models/entities/user.js";
import type { UserGroup } from "@/models/entities/user-group.js";
import type { DriveFile } from "@/models/entities/drive-file.js";
import {
MessagingMessages,
UserGroupJoinings,
Mutings,
Users,
} from "@/models/index.js";
import { genId } from "@/misc/gen-id.js";
import type { MessagingMessage } from "@/models/entities/messaging-message.js";
import {
publishMessagingStream,
publishMessagingIndexStream,
publishMainStream,
publishGroupMessagingStream,
} from "@/services/stream.js";
import { pushNotification } from "@/services/push-notification.js";
import { Not } from "typeorm";
import type { Note } from "@/models/entities/note.js";
import renderNote from "@/remote/activitypub/renderer/note.js";
import renderCreate from "@/remote/activitypub/renderer/create.js";
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
import { deliver } from "@/queue/index.js";
2019-10-28 22:01:14 +01:00
2023-01-13 05:40:33 +01:00
export async function createMessage(
user: { id: User["id"]; host: User["host"] },
recipientUser: CacheableUser | undefined,
recipientGroup: UserGroup | undefined,
text: string | null | undefined,
file: DriveFile | null,
uri?: string,
) {
2021-03-21 13:27:09 +01:00
const message = {
2019-10-28 22:01:14 +01:00
id: genId(),
createdAt: new Date(),
fileId: file ? file.id : null,
recipientId: recipientUser ? recipientUser.id : null,
groupId: recipientGroup ? recipientGroup.id : null,
text: text ? text.trim() : null,
userId: user.id,
isRead: false,
reads: [] as any[],
2021-12-09 15:58:30 +01:00
uri,
2021-03-21 13:27:09 +01:00
} as MessagingMessage;
await MessagingMessages.insert(message);
2019-10-28 22:01:14 +01:00
const messageObj = await MessagingMessages.pack(message);
if (recipientUser) {
if (Users.isLocalUser(user)) {
// 自分のストリーム
2023-01-13 05:40:33 +01:00
publishMessagingStream(
message.userId,
recipientUser.id,
"message",
messageObj,
);
publishMessagingIndexStream(message.userId, "message", messageObj);
publishMainStream(message.userId, "messagingMessage", messageObj);
2019-10-28 22:01:14 +01:00
}
if (Users.isLocalUser(recipientUser)) {
// 相手のストリーム
2023-01-13 05:40:33 +01:00
publishMessagingStream(
recipientUser.id,
message.userId,
"message",
messageObj,
);
publishMessagingIndexStream(recipientUser.id, "message", messageObj);
publishMainStream(recipientUser.id, "messagingMessage", messageObj);
2019-10-28 22:01:14 +01:00
}
} else if (recipientGroup) {
// グループのストリーム
2023-01-13 05:40:33 +01:00
publishGroupMessagingStream(recipientGroup.id, "message", messageObj);
2019-10-28 22:01:14 +01:00
// メンバーのストリーム
2023-01-13 05:40:33 +01:00
const joinings = await UserGroupJoinings.findBy({
userGroupId: recipientGroup.id,
});
2019-10-28 22:01:14 +01:00
for (const joining of joinings) {
2023-01-13 05:40:33 +01:00
publishMessagingIndexStream(joining.userId, "message", messageObj);
publishMainStream(joining.userId, "messagingMessage", messageObj);
2019-10-28 22:01:14 +01:00
}
}
// 2秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する
setTimeout(async () => {
const freshMessage = await MessagingMessages.findOneBy({ id: message.id });
2019-10-28 22:01:14 +01:00
if (freshMessage == null) return; // メッセージが削除されている場合もある
if (recipientUser && Users.isLocalUser(recipientUser)) {
if (freshMessage.isRead) return; // 既読
//#region ただしミュートされているなら発行しない
const mute = await Mutings.findBy({
2019-10-28 22:01:14 +01:00
muterId: recipientUser.id,
});
2023-01-13 05:40:33 +01:00
if (mute.map((m) => m.muteeId).includes(user.id)) return;
2019-10-28 22:01:14 +01:00
//#endregion
2023-01-13 05:40:33 +01:00
publishMainStream(recipientUser.id, "unreadMessagingMessage", messageObj);
pushNotification(recipientUser.id, "unreadMessagingMessage", messageObj);
2019-10-28 22:01:14 +01:00
} else if (recipientGroup) {
2023-01-13 05:40:33 +01:00
const joinings = await UserGroupJoinings.findBy({
userGroupId: recipientGroup.id,
userId: Not(user.id),
});
2019-10-28 22:01:14 +01:00
for (const joining of joinings) {
if (freshMessage.reads.includes(joining.userId)) return; // 既読
2023-01-13 05:40:33 +01:00
publishMainStream(joining.userId, "unreadMessagingMessage", messageObj);
pushNotification(joining.userId, "unreadMessagingMessage", messageObj);
2019-10-28 22:01:14 +01:00
}
}
}, 2000);
2023-01-13 05:40:33 +01:00
if (
recipientUser &&
Users.isLocalUser(user) &&
Users.isRemoteUser(recipientUser)
) {
2019-10-28 22:01:14 +01:00
const note = {
id: message.id,
createdAt: message.createdAt,
2023-01-13 05:40:33 +01:00
fileIds: message.fileId ? [message.fileId] : [],
2019-10-28 22:01:14 +01:00
text: message.text,
userId: message.userId,
2023-01-13 05:40:33 +01:00
visibility: "specified",
mentions: [recipientUser].map((u) => u.id),
mentionedRemoteUsers: JSON.stringify(
[recipientUser].map((u) => ({
uri: u.uri,
username: u.username,
host: u.host,
})),
),
2019-10-28 22:01:14 +01:00
} as Note;
2023-01-13 05:40:33 +01:00
const activity = renderActivity(
renderCreate(await renderNote(note, false, true), note),
);
2019-10-28 22:01:14 +01:00
deliver(user, activity, recipientUser.inbox);
}
return messageObj;
}