({
body,
files,
accountId,
channelId,
threadId,
imitationId,
userId,
externalMessageId,
mentions,
messageFormat = MessageFormat.LINEN,
}: {
body: string;
files?: UploadedFile[];
accountId: string;
channelId: string;
threadId: string;
imitationId?: string;
userId: string;
externalMessageId?: string;
mentions?: { id: string }[];
messageFormat?: MessageFormat;
})
| 19 | |
| 20 | export default class MessagesService { |
| 21 | static async create({ |
| 22 | body, |
| 23 | files, |
| 24 | accountId, |
| 25 | channelId, |
| 26 | threadId, |
| 27 | imitationId, |
| 28 | userId, |
| 29 | externalMessageId, |
| 30 | mentions, |
| 31 | messageFormat = MessageFormat.LINEN, |
| 32 | }: { |
| 33 | body: string; |
| 34 | files?: UploadedFile[]; |
| 35 | accountId: string; |
| 36 | channelId: string; |
| 37 | threadId: string; |
| 38 | imitationId?: string; |
| 39 | userId: string; |
| 40 | externalMessageId?: string; |
| 41 | mentions?: { id: string }[]; |
| 42 | messageFormat?: MessageFormat; |
| 43 | }) { |
| 44 | const account = await prisma.accounts.findFirst({ |
| 45 | where: { id: accountId }, |
| 46 | }); |
| 47 | if (!account) { |
| 48 | throw new Error("can't find the account"); |
| 49 | } |
| 50 | if (account.chat === ChatType.NONE) { |
| 51 | throw new Error('chat is disabled'); |
| 52 | } |
| 53 | const channel = await prisma.channels.findFirst({ |
| 54 | where: { id: channelId, accountId }, |
| 55 | }); |
| 56 | |
| 57 | if (!channel || !channel.accountId || channel.accountId !== accountId) { |
| 58 | throw new Error("can't find the channel"); |
| 59 | } |
| 60 | |
| 61 | const sentAt = new Date(); |
| 62 | |
| 63 | const tree = parse.linen(body); |
| 64 | const mentionNodes = mentions || find.mentions(tree); |
| 65 | const userIds = unique<string>( |
| 66 | mentionNodes.map(({ id }: { id: string }) => id) |
| 67 | ); |
| 68 | const messages = { |
| 69 | create: { |
| 70 | body, |
| 71 | channel: { connect: { id: channelId } }, |
| 72 | sentAt, |
| 73 | author: { connect: { id: userId } }, |
| 74 | mentions: { |
| 75 | create: userIds.map((id: string) => ({ usersId: id })), |
| 76 | }, |
| 77 | externalMessageId, |
| 78 | ...(files?.length && { |
no test coverage detected