MCPcopy Index your code
hub / github.com/AnswerOverflow/AnswerOverflow / fastUpsertManyMessages

Function fastUpsertManyMessages

packages/core/src/message-node.ts:169–243  ·  view source on GitHub ↗
(data: BaseMessageWithRelations[])

Source from the content-addressed store, hash-verified

167}
168
169export async function fastUpsertManyMessages(data: BaseMessageWithRelations[]) {
170 const attachments = new Set<typeof dbAttachments.$inferInsert>();
171 const emojis = new Set<typeof dbEmojis.$inferInsert>();
172 const reactions = new Set<typeof dbReactions.$inferInsert>();
173 const msgs = new Set<typeof dbMessages.$inferInsert>();
174 for (const msg of data) {
175 const { attachments: a, reactions: r, embeds: e, ...m } = msg;
176 msgs.add({
177 ...messageSchema.parse(m),
178 embeds: e,
179 });
180 if (a) {
181 for (const attachment of a) {
182 attachments.add(attachmentSchema.parse(attachment));
183 }
184 }
185 if (r) {
186 for (const reaction of r) {
187 if (!reaction.emoji.id || !reaction.emoji.name) continue;
188 emojis.add(emojiSchema.parse(reaction.emoji));
189 reactions.add(reactionSchema.parse(reaction));
190 }
191 }
192 }
193
194 if (msgs.size > 0)
195 await db
196 .insert(dbMessages)
197 .values(Array.from(msgs))
198 .onDuplicateKeyUpdate({ set: { id: sql.raw('id') } });
199 if (attachments.size > 0) {
200 void db
201 .select()
202 .from(dbAttachments)
203 .where(
204 inArray(
205 dbAttachments.id,
206 [...attachments].map((a) => a.id),
207 ),
208 )
209 .then(async (existingAttachments) => {
210 const existingAttachmentIds = new Set(
211 existingAttachments.map((a) => a.id),
212 );
213 for await (const attachment of attachments) {
214 if (!existingAttachmentIds.has(attachment.id)) {
215 void uploadFileFromUrl({
216 url: attachment.url,
217 id: attachment.id,
218 contentType: attachment.contentType ?? undefined,
219 filename: attachment.filename,
220 }).then(async (uploaded) => {
221 if (!uploaded) return;
222 await db.insert(dbAttachments).values(
223 attachmentSchema.parse({
224 ...attachment,
225 proxyUrl: `https://cdn.answeroverflow.com/${attachment.id}/${attachment.filename}`,
226 }),

Callers 1

upsertManyMessagesFunction · 0.85

Calls 3

uploadFileFromUrlFunction · 0.90
addMethod · 0.80
parseMethod · 0.45

Tested by

no test coverage detected