diff --git a/app/api/inngest/functions/index_post_mention.ts b/app/api/inngest/functions/index_post_mention.ts index 591874fe..726c4c3a 100644 --- a/app/api/inngest/functions/index_post_mention.ts +++ b/app/api/inngest/functions/index_post_mention.ts @@ -5,6 +5,7 @@ import { Json } from "supabase/database.types"; import { ids } from "lexicons/api/lexicons"; import { Notification, pingIdentityToUpdateNotification } from "src/notifications"; import { v7 } from "uuid"; +import { idResolver } from "app/(home-pages)/reader/idResolver"; export const index_post_mention = inngest.createFunction( { id: "index_post_mention" }, @@ -13,17 +14,52 @@ export const index_post_mention = inngest.createFunction( let url = new URL(event.data.document_link); let path = url.pathname.split("/").filter(Boolean); - let { data: pub, error } = await supabaseServerClient - .from("publications") - .select("*") - .eq("record->>base_path", url.host) - .single(); + // Check if this is a standalone document URL (/p/didOrHandle/rkey/...) + const isStandaloneDoc = path[0] === "p" && path.length >= 3; - if (!pub) { - return { - message: `No publication found for ${url.host}/${path[0]}`, - error, - }; + let documentUri: string; + let authorDid: string; + + if (isStandaloneDoc) { + // Standalone doc: /p/didOrHandle/rkey/l-quote/... + const didOrHandle = decodeURIComponent(path[1]); + const rkey = path[2]; + + // Resolve handle to DID if necessary + let did = didOrHandle; + if (!didOrHandle.startsWith("did:")) { + const resolved = await step.run("resolve-handle", async () => { + return idResolver.handle.resolve(didOrHandle); + }); + if (!resolved) { + return { message: `Could not resolve handle: ${didOrHandle}` }; + } + did = resolved; + } + + documentUri = AtUri.make(did, ids.PubLeafletDocument, rkey).toString(); + authorDid = did; + } else { + // Publication post: look up by custom domain + let { data: pub, error } = await supabaseServerClient + .from("publications") + .select("*") + .eq("record->>base_path", url.host) + .single(); + + if (!pub) { + return { + message: `No publication found for ${url.host}/${path[0]}`, + error, + }; + } + + documentUri = AtUri.make( + pub.identity_did, + ids.PubLeafletDocument, + path[0], + ).toString(); + authorDid = pub.identity_did; } let bsky_post = await step.run("get-bsky-post-data", async () => { @@ -39,12 +75,6 @@ export const index_post_mention = inngest.createFunction( return { message: `No post found for ${event.data.post_uri}` }; } - const documentUri = AtUri.make( - pub.identity_did, - ids.PubLeafletDocument, - path[0], - ).toString(); - await step.run("index-bsky-post", async () => { await supabaseServerClient.from("bsky_posts").upsert({ uri: bsky_post.uri, @@ -60,12 +90,12 @@ export const index_post_mention = inngest.createFunction( 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) { + if (bsky_post.author.did !== authorDid) { // 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("recipient", authorDid) .eq("data->>type", "quote") .eq("data->>bsky_post_uri", bsky_post.uri) .eq("data->>document_uri", documentUri) @@ -74,7 +104,7 @@ export const index_post_mention = inngest.createFunction( if (!existingNotification) { const notification: Notification = { id: v7(), - recipient: pub.identity_did, + recipient: authorDid, data: { type: "quote", bsky_post_uri: bsky_post.uri, @@ -82,7 +112,7 @@ export const index_post_mention = inngest.createFunction( }, }; await supabaseServerClient.from("notifications").insert(notification); - await pingIdentityToUpdateNotification(pub.identity_did); + await pingIdentityToUpdateNotification(authorDid); } } });