From f94d0da04728872b981230b270a447ba7d1b1fd3 Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Wed, 11 Feb 2026 10:51:04 +0400 Subject: [PATCH] add indexed col to documents and update inngest --- app/api/inngest/client.ts | 4 +-- ...sky_likes.ts => sync_document_metadata.ts} | 30 +++++++++++------ app/api/inngest/route.tsx | 4 +-- appview/index.ts | 32 ++++++++----------- supabase/database.types.ts | 8 +++++ ...0_add_indexed_column_and_ranking_index.sql | 6 ++++ 6 files changed, 52 insertions(+), 32 deletions(-) rename app/api/inngest/functions/{sync_bsky_likes.ts => sync_document_metadata.ts} (62%) create mode 100644 supabase/migrations/20260210100000_add_indexed_column_and_ranking_index.sql diff --git a/app/api/inngest/client.ts b/app/api/inngest/client.ts index f648b39f..ffce505c 100644 --- a/app/api/inngest/client.ts +++ b/app/api/inngest/client.ts @@ -51,10 +51,10 @@ export type Events = { documentUris?: string[]; }; }; - "appview/sync-bsky-likes": { + "appview/sync-document-metadata": { data: { document_uri: string; - bsky_post_uri: string; + bsky_post_uri?: string; }; }; "user/write-records-to-pds": { diff --git a/app/api/inngest/functions/sync_bsky_likes.ts b/app/api/inngest/functions/sync_document_metadata.ts similarity index 62% rename from app/api/inngest/functions/sync_bsky_likes.ts rename to app/api/inngest/functions/sync_document_metadata.ts index 69dfe3c6..d2836a2a 100644 --- a/app/api/inngest/functions/sync_bsky_likes.ts +++ b/app/api/inngest/functions/sync_document_metadata.ts @@ -5,26 +5,36 @@ import { idResolver } from "app/(home-pages)/reader/idResolver"; const TOTAL_ITERATIONS = 144; // 36 hours at 15-minute intervals -export const sync_bsky_likes = inngest.createFunction( +export const sync_document_metadata = inngest.createFunction( { - id: "sync_bsky_likes", - idempotency: "event.data.bsky_post_uri", + id: "sync_document_metadata", + idempotency: "event.data.document_uri", }, - { event: "appview/sync-bsky-likes" }, + { event: "appview/sync-document-metadata" }, async ({ event, step }) => { const { document_uri, bsky_post_uri } = event.data; - const isBridgy = await step.run("check-bridgy", async () => { - const did = new AtUri(bsky_post_uri).host; + const did = new AtUri(document_uri).host; + + const handleResult = await step.run("resolve-handle", async () => { const doc = await idResolver.did.resolve(did); const handle = doc?.alsoKnownAs ?.find((a) => a.startsWith("at://")) ?.replace("at://", ""); - return handle?.includes("brid.gy") ?? false; + return { handle: handle ?? null, isBridgy: handle?.includes("brid.gy") ?? false }; }); - if (isBridgy) { - return { skipped: true, reason: "brid.gy post" }; + if (handleResult.isBridgy) { + await step.run("set-unindexed", async () => { + await supabaseServerClient + .from("documents") + .update({ indexed: false }) + .eq("uri", document_uri); + }); + } + + if (!bsky_post_uri || handleResult.isBridgy) { + return { handle: handleResult.handle }; } const agent = new AtpAgent({ service: "https://public.api.bsky.app" }); @@ -50,6 +60,6 @@ export const sync_bsky_likes = inngest.createFunction( likeCount = await step.run(`sync-${i}`, fetchAndUpdate); } - return { likeCount }; + return { likeCount, handle: handleResult.handle }; }, ); diff --git a/app/api/inngest/route.tsx b/app/api/inngest/route.tsx index ea0f2f9e..a74f1d98 100644 --- a/app/api/inngest/route.tsx +++ b/app/api/inngest/route.tsx @@ -13,7 +13,7 @@ import { check_oauth_session, } from "./functions/cleanup_expired_oauth_sessions"; import { write_records_to_pds } from "./functions/write_records_to_pds"; -import { sync_bsky_likes } from "./functions/sync_bsky_likes"; +import { sync_document_metadata } from "./functions/sync_document_metadata"; export const { GET, POST, PUT } = serve({ client: inngest, @@ -29,6 +29,6 @@ export const { GET, POST, PUT } = serve({ cleanup_expired_oauth_sessions, check_oauth_session, write_records_to_pds, - sync_bsky_likes, + sync_document_metadata, ], }); diff --git a/appview/index.ts b/appview/index.ts index 7319f549..31ead5fb 100644 --- a/appview/index.ts +++ b/appview/index.ts @@ -109,15 +109,13 @@ async function handleEvent(evt: Event) { data: record.value as Json, }); if (docResult.error) console.log(docResult.error); - if (record.value.postRef?.uri) { - await inngest.send({ - name: "appview/sync-bsky-likes", - data: { - document_uri: evt.uri.toString(), - bsky_post_uri: record.value.postRef.uri, - }, - }); - } + await inngest.send({ + name: "appview/sync-document-metadata", + data: { + document_uri: evt.uri.toString(), + bsky_post_uri: record.value.postRef?.uri, + }, + }); if (record.value.publication) { let publicationURI = new AtUri(record.value.publication); @@ -278,15 +276,13 @@ async function handleEvent(evt: Event) { data: record.value as Json, }); if (docResult.error) console.log(docResult.error); - if (record.value.bskyPostRef?.uri) { - await inngest.send({ - name: "appview/sync-bsky-likes", - data: { - document_uri: evt.uri.toString(), - bsky_post_uri: record.value.bskyPostRef.uri, - }, - }); - } + await inngest.send({ + name: "appview/sync-document-metadata", + data: { + document_uri: evt.uri.toString(), + bsky_post_uri: record.value.bskyPostRef?.uri, + }, + }); // site.standard.document uses "site" field to reference the publication // For documents in publications, site is an AT-URI (at://did:plc:xxx/site.standard.publication/rkey) diff --git a/supabase/database.types.ts b/supabase/database.types.ts index 1eb727c2..c4f53bde 100644 --- a/supabase/database.types.ts +++ b/supabase/database.types.ts @@ -337,20 +337,28 @@ export type Database = { Row: { bsky_like_count: number data: Json + indexed: boolean indexed_at: string + recommend_count: number sort_date: string uri: string } Insert: { bsky_like_count?: number data: Json + indexed?: boolean indexed_at?: string + recommend_count?: number + sort_date?: string uri: string } Update: { bsky_like_count?: number data?: Json + indexed?: boolean indexed_at?: string + recommend_count?: number + sort_date?: string uri?: string } Relationships: [] diff --git a/supabase/migrations/20260210100000_add_indexed_column_and_ranking_index.sql b/supabase/migrations/20260210100000_add_indexed_column_and_ranking_index.sql new file mode 100644 index 00000000..562f3b18 --- /dev/null +++ b/supabase/migrations/20260210100000_add_indexed_column_and_ranking_index.sql @@ -0,0 +1,6 @@ +ALTER TABLE documents ADD COLUMN indexed boolean NOT NULL DEFAULT true; + +CREATE INDEX idx_documents_ranking +ON documents (sort_date DESC) +INCLUDE (uri, bsky_like_count, recommend_count) +WHERE indexed = true; -- 2.51.2