forked from
leaflet.pub/leaflet
a tool for shared writing and social publishing
1import { supabaseServerClient } from "supabase/serverClient";
2import { inngest } from "../client";
3import { AtpAgent, AtUri } from "@atproto/api";
4import { Json } from "supabase/database.types";
5import { ids } from "lexicons/api/lexicons";
6
7export const index_post_mention = inngest.createFunction(
8 { id: "index_post_mention" },
9 { event: "appview/index-bsky-post-mention" },
10 async ({ event, step }) => {
11 let url = new URL(event.data.document_link);
12 let path = url.pathname.split("/").filter(Boolean);
13
14 let { data: pub, error } = await supabaseServerClient
15 .from("publications")
16 .select("*")
17 .eq("record->>base_path", url.host)
18 .single();
19
20 if (!pub) {
21 return {
22 message: `No publication found for ${url.host}/${path[0]}`,
23 error,
24 };
25 }
26
27 let bsky_post = await step.run("get-bsky-post-data", async () => {
28 let agent = new AtpAgent({ service: "https://public.api.bsky.app" });
29 let posts = await agent.app.bsky.feed.getPosts({
30 uris: [event.data.post_uri],
31 });
32 if (!posts.data.posts[0]) return null;
33 return posts.data.posts[0];
34 });
35
36 if (!bsky_post) {
37 return { message: `No post found for ${event.data.post_uri}` };
38 }
39
40 await step.run("index-bsky-post", async () => {
41 await supabaseServerClient.from("bsky_posts").insert({
42 uri: bsky_post.uri,
43 cid: bsky_post.cid,
44 post_view: bsky_post as Json,
45 });
46 await supabaseServerClient.from("document_mentions_in_bsky").insert({
47 uri: bsky_post.uri,
48 document: AtUri.make(
49 pub.identity_did,
50 ids.PubLeafletDocument,
51 path[0],
52 ).toString(),
53 link: event.data.document_link,
54 });
55 });
56 },
57);