diff --git a/app/api/inngest/functions/index_post_mention.ts b/app/api/inngest/functions/index_post_mention.ts --- a/app/api/inngest/functions/index_post_mention.ts +++ b/app/api/inngest/functions/index_post_mention.ts @@ -46,12 +46,12 @@ ).toString(); await step.run("index-bsky-post", async () => { - await supabaseServerClient.from("bsky_posts").insert({ + await supabaseServerClient.from("bsky_posts").upsert({ uri: bsky_post.uri, cid: bsky_post.cid, post_view: bsky_post as Json, }); - await supabaseServerClient.from("document_mentions_in_bsky").insert({ + await supabaseServerClient.from("document_mentions_in_bsky").upsert({ uri: bsky_post.uri, document: documentUri, link: event.data.document_link, @@ -61,17 +61,29 @@ await step.run("create-notification", async () => { // Only create notification if the quote is from someone other than the author if (bsky_post.author.did !== pub.identity_did) { - const notification: Notification = { - id: v7(), - recipient: pub.identity_did, - data: { - type: "quote", - bsky_post_uri: bsky_post.uri, - document_uri: documentUri, - }, - }; - await supabaseServerClient.from("notifications").insert(notification); - await pingIdentityToUpdateNotification(pub.identity_did); + // Check if a notification already exists for this post and recipient + const { data: existingNotification } = await supabaseServerClient + .from("notifications") + .select("id") + .eq("recipient", pub.identity_did) + .eq("data->>type", "quote") + .eq("data->>bsky_post_uri", bsky_post.uri) + .eq("data->>document_uri", documentUri) + .single(); + + if (!existingNotification) { + const notification: Notification = { + id: v7(), + recipient: pub.identity_did, + data: { + type: "quote", + bsky_post_uri: bsky_post.uri, + document_uri: documentUri, + }, + }; + await supabaseServerClient.from("notifications").insert(notification); + await pingIdentityToUpdateNotification(pub.identity_did); + } } }); },