From 052cbaa4df3a990f62ade2303fba04065b3b1272 Mon Sep 17 00:00:00 2001 From: Jared Pereira Date: Mon, 28 Jul 2025 21:29:18 -0400 Subject: [PATCH] batch profile updates --- app/api/inngest/client.ts | 6 +++ .../functions/batched_update_profiles.ts | 37 +++++++++++++++++++ app/api/inngest/route.tsx | 3 +- appview/index.ts | 11 ++++-- 4 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 app/api/inngest/functions/batched_update_profiles.ts diff --git a/app/api/inngest/client.ts b/app/api/inngest/client.ts index b062a584..21e16d3e 100644 --- a/app/api/inngest/client.ts +++ b/app/api/inngest/client.ts @@ -3,6 +3,12 @@ import { Inngest } from "inngest"; import { EventSchemas } from "inngest"; export type Events = { + "appview/profile-update": { + data: { + record: any; + did: string; + }; + }; "appview/index-bsky-post-mention": { data: { post_uri: string; diff --git a/app/api/inngest/functions/batched_update_profiles.ts b/app/api/inngest/functions/batched_update_profiles.ts new file mode 100644 index 00000000..b0b11e30 --- /dev/null +++ b/app/api/inngest/functions/batched_update_profiles.ts @@ -0,0 +1,37 @@ +import { supabaseServerClient } from "supabase/serverClient"; +import { inngest } from "../client"; + +export const batched_update_profiles = inngest.createFunction( + { + id: "batched_update_profiles", + batchEvents: { + maxSize: 100, + timeout: "10s", + }, + }, + { event: "appview/profile-update" }, + async ({ events, step }) => { + let existingProfiles = await supabaseServerClient + .from("bsky_profiles") + .select("did") + .in( + "did", + events.map((event) => event.data.did), + ); + if (!existingProfiles.data) return { error: existingProfiles.error }; + + const profileUpdates = events.map((event) => ({ + did: event.data.did, + record: event.data.record, + })); + + let { error } = await supabaseServerClient + .from("bsky_profiles") + .upsert(profileUpdates); + return { + done: true, + profiles_updated: existingProfiles.data.length, + error, + }; + }, +); diff --git a/app/api/inngest/route.tsx b/app/api/inngest/route.tsx index 8ae9821a..826429bc 100644 --- a/app/api/inngest/route.tsx +++ b/app/api/inngest/route.tsx @@ -2,9 +2,10 @@ import { serve } from "inngest/next"; import { inngest } from "app/api/inngest/client"; import { index_post_mention } from "./functions/index_post_mention"; import { come_online } from "./functions/come_online"; +import { batched_update_profiles } from "./functions/batched_update_profiles"; // Create an API that serves zero functions export const { GET, POST, PUT } = serve({ client: inngest, - functions: [index_post_mention, come_online], + functions: [index_post_mention, come_online, batched_update_profiles], }); diff --git a/appview/index.ts b/appview/index.ts index 5a96a481..734394c9 100644 --- a/appview/index.ts +++ b/appview/index.ts @@ -138,10 +138,13 @@ async function main() { if (evt.collection === ids.AppBskyActorProfile) { //only listen to updates because we should fetch it for the first time when they subscribe! if (evt.event === "update") { - await supabaseServerClient - .from("bsky_profiles") - .update({ record: evt.record as Json }) - .eq("did", evt.did); + await inngest.send({ + name: "appview/profile-update", + data: { + did: evt.did, + record: evt.record, + }, + }); } } if (evt.collection === "app.bsky.feed.post") { -- 2.51.2