From 39bf1ccd1e5f9bdfd1a3bbd4d52c396069ada2e4 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Tue, 5 Mar 2024 02:37:06 +0000 Subject: [PATCH] fix: Drop old AI tag on link refresh --- packages/workers/openai.ts | 93 ++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/packages/workers/openai.ts b/packages/workers/openai.ts index e82a53b5..1ec22d32 100644 --- a/packages/workers/openai.ts +++ b/packages/workers/openai.ts @@ -163,47 +163,61 @@ async function inferTags( } } -async function createTags(tags: string[], userId: string) { - if (tags.length == 0) { - return []; +async function connectTags( + bookmarkId: string, + newTags: string[], + userId: string, +) { + if (newTags.length == 0) { + return; } - await db - .insert(bookmarkTags) - .values( - tags.map((t) => ({ - name: t, - userId, - })), - ) - .onConflictDoNothing(); - - const res = await db.query.bookmarkTags.findMany({ - where: and( - eq(bookmarkTags.userId, userId), - inArray(bookmarkTags.name, tags), - ), - columns: { - id: true, - }, - }); - return res.map((r) => r.id); -} + await db.transaction(async (tx) => { + // Create tags that didn't exist previously + await tx + .insert(bookmarkTags) + .values( + newTags.map((t) => ({ + name: t, + userId, + })), + ) + .onConflictDoNothing(); -async function connectTags(bookmarkId: string, tagIds: string[]) { - if (tagIds.length == 0) { - return; - } - await db - .insert(tagsOnBookmarks) - .values( - tagIds.map((tagId) => ({ - tagId, - bookmarkId, - attachedBy: "ai" as const, - })), - ) - .onConflictDoNothing(); + const newTagIds = ( + await tx.query.bookmarkTags.findMany({ + where: and( + eq(bookmarkTags.userId, userId), + inArray(bookmarkTags.name, newTags), + ), + columns: { + id: true, + }, + }) + ).map((r) => r.id); + + // Delete old AI tags + await tx + .delete(tagsOnBookmarks) + .where( + and( + eq(tagsOnBookmarks.attachedBy, "ai"), + eq(tagsOnBookmarks.bookmarkId, bookmarkId), + ), + ); + + // Attach new ones + await tx + .insert(tagsOnBookmarks) + .values( + newTagIds.map((tagId) => ({ + tagId, + bookmarkId, + attachedBy: "ai" as const, + })), + ) + .onConflictDoNothing(); + }); } async function runOpenAI(job: Job) { @@ -239,8 +253,7 @@ async function runOpenAI(job: Job) { const tags = await inferTags(jobId, bookmark, openai); - const tagIds = await createTags(tags, bookmark.userId); - await connectTags(bookmarkId, tagIds); + await connectTags(bookmarkId, tags, bookmark.userId); // Update the search index SearchIndexingQueue.add("search_indexing", {