a tool for shared writing and social publishing

delete unfollowed accounts in index_follows properly

+41 -7
+41 -7
app/api/inngest/functions/index_follows.ts
··· 89 89 const insertBatchSize = 100; 90 90 let insertBatchNumber = 0; 91 91 92 - await step.run("clear existing follows", () => { 93 - return supabaseServerClient 94 - .from("bsky_follows") 95 - .delete() 96 - .eq("identity", event.data.did); 97 - }); 98 - 99 92 // Create all insert batches in parallel 100 93 const insertBatches = []; 101 94 for (let i = 0; i < existingFollows.length; i += insertBatchSize) { ··· 117 110 118 111 // Wait for all insert batches to complete 119 112 await Promise.all(insertBatches); 113 + 114 + // Delete follows that are no longer in the fetched list 115 + // For large follow lists, we need to batch this operation 116 + await step.run("delete-unfollowed", async () => { 117 + // Get all current follows from the database 118 + const { data: currentFollows } = await supabaseServerClient 119 + .from("bsky_follows") 120 + .select("follows") 121 + .eq("identity", event.data.did); 122 + 123 + if (!currentFollows || currentFollows.length === 0) { 124 + return { deleted: 0 }; 125 + } 126 + 127 + // Find follows that are in the database but not in the newly fetched list 128 + const currentFollowDids = currentFollows.map((f) => f.follows); 129 + const toDelete = currentFollowDids.filter( 130 + (did) => !existingFollows.includes(did) 131 + ); 132 + 133 + if (toDelete.length === 0) { 134 + return { deleted: 0 }; 135 + } 136 + 137 + // Delete in batches to avoid query size limits 138 + const deleteBatchSize = 100; 139 + const deletePromises = []; 140 + for (let i = 0; i < toDelete.length; i += deleteBatchSize) { 141 + const batch = toDelete.slice(i, i + deleteBatchSize); 142 + deletePromises.push( 143 + supabaseServerClient 144 + .from("bsky_follows") 145 + .delete() 146 + .eq("identity", event.data.did) 147 + .in("follows", batch) 148 + ); 149 + } 150 + 151 + await Promise.all(deletePromises); 152 + return { deleted: toDelete.length }; 153 + }); 120 154 return { 121 155 done: true, 122 156 };