MCPcopy Index your code
hub / github.com/Linen-dev/linen.dev / addMessage

Function addMessage

apps/web/services/slack/webhooks/processMessageEvents.ts:100–216  ·  view source on GitHub ↗
(
  channel: channels & {
    account: (accounts & { slackAuthorizations: slackAuthorizations[] }) | null;
  },
  event: SlackMessageEvent,
  logger: Logger
)

Source from the content-addressed store, hash-verified

98}
99
100async 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,

Callers 1

processMessageEventFunction · 0.85

Calls 12

findOrCreateThreadFunction · 0.90
parseSlackSentAtFunction · 0.90
slugifyFunction · 0.90
updateSlackThreadFunction · 0.90
tsToSentAtFunction · 0.90
processAttachmentsFunction · 0.90
serializeThreadFunction · 0.90
eventNewThreadFunction · 0.90
serializeMessageFunction · 0.90
eventNewMessageFunction · 0.90
mapMethod · 0.80

Tested by

no test coverage detected