(
channel: channels & {
account: (accounts & { slackAuthorizations: slackAuthorizations[] }) | null;
},
event: SlackMessageEvent,
logger: Logger
)
| 98 | } |
| 99 | |
| 100 | async function addMessage( |
| 101 | channel: channels & { |
| 102 | account: (accounts & { slackAuthorizations: slackAuthorizations[] }) | null; |
| 103 | }, |
| 104 | event: SlackMessageEvent, |
| 105 | logger: Logger |
| 106 | ) { |
| 107 | const thread_ts = event.thread_ts || event.ts; |
| 108 | const accessToken = channel.account?.slackAuthorizations[0]?.accessToken!; |
| 109 | |
| 110 | const externalUserId = event.bot_id || event.user; |
| 111 | if (!externalUserId) { |
| 112 | return { error: 'missing externalUserId', event }; |
| 113 | } |
| 114 | |
| 115 | const user = await findOrCreateUserFromUserInfo( |
| 116 | externalUserId, |
| 117 | channel.accountId!, |
| 118 | accessToken |
| 119 | ); |
| 120 | |
| 121 | const thread = await findOrCreateThread({ |
| 122 | externalThreadId: thread_ts, |
| 123 | channelId: channel.id, |
| 124 | sentAt: parseSlackSentAt(event.ts), |
| 125 | lastReplyAt: parseSlackSentAt(event.ts), |
| 126 | slug: slugify(event.text), |
| 127 | }); |
| 128 | |
| 129 | if (!!event.thread_ts) { |
| 130 | thread.messageCount += 1; |
| 131 | await updateSlackThread(thread.id, { |
| 132 | messageCount: thread.messageCount, |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | //TODO: create render text object and save that on creation of message |
| 137 | // This way we don't have to fetch mentions on every message render |
| 138 | let mentionUserIds = event.text.match(/<@(.*?)>/g) || []; |
| 139 | let mentionUsersMap = mentionUserIds.map((m) => |
| 140 | m.replace('<@', '').replace('>', '') |
| 141 | ); |
| 142 | const mentionUsers = await Promise.all( |
| 143 | mentionUsersMap.map((userId) => |
| 144 | findOrCreateUserFromUserInfo(userId, channel.accountId!, accessToken) |
| 145 | ) |
| 146 | ); |
| 147 | |
| 148 | const mentionIds = mentionUsers.filter(Boolean).map((x) => x!.id); |
| 149 | |
| 150 | const param: Prisma.messagesUncheckedCreateInput = { |
| 151 | body: event.text, |
| 152 | channelId: channel.id, |
| 153 | sentAt: tsToSentAt(event.ts), |
| 154 | threadId: thread?.id, |
| 155 | externalMessageId: event.ts, |
| 156 | usersId: user?.id, |
| 157 | messageFormat: MessageFormat.SLACK, |
no test coverage detected