Skip to content

Commit

Permalink
fix: Drop old AI tag on link refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Mar 5, 2024
1 parent b8b0a44 commit 39bf1cc
Showing 1 changed file with 53 additions and 40 deletions.
93 changes: 53 additions & 40 deletions packages/workers/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ZOpenAIRequest, void>) {
Expand Down Expand Up @@ -239,8 +253,7 @@ async function runOpenAI(job: Job<ZOpenAIRequest, void>) {

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", {
Expand Down

0 comments on commit 39bf1cc

Please sign in to comment.