tangled
alpha
login
or
join now
leaflet.pub
/
leaflet
289
fork
atom
a tool for shared writing and social publishing
289
fork
atom
overview
issues
28
pulls
pipelines
delete unfollowed accounts in index_follows properly
awarm.space
5 months ago
f915cd82
bec1208d
+41
-7
1 changed file
expand all
collapse all
unified
split
app
api
inngest
functions
index_follows.ts
+41
-7
app/api/inngest/functions/index_follows.ts
···
89
89
const insertBatchSize = 100;
90
90
let insertBatchNumber = 0;
91
91
92
92
-
await step.run("clear existing follows", () => {
93
93
-
return supabaseServerClient
94
94
-
.from("bsky_follows")
95
95
-
.delete()
96
96
-
.eq("identity", event.data.did);
97
97
-
});
98
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
113
+
114
114
+
// Delete follows that are no longer in the fetched list
115
115
+
// For large follow lists, we need to batch this operation
116
116
+
await step.run("delete-unfollowed", async () => {
117
117
+
// Get all current follows from the database
118
118
+
const { data: currentFollows } = await supabaseServerClient
119
119
+
.from("bsky_follows")
120
120
+
.select("follows")
121
121
+
.eq("identity", event.data.did);
122
122
+
123
123
+
if (!currentFollows || currentFollows.length === 0) {
124
124
+
return { deleted: 0 };
125
125
+
}
126
126
+
127
127
+
// Find follows that are in the database but not in the newly fetched list
128
128
+
const currentFollowDids = currentFollows.map((f) => f.follows);
129
129
+
const toDelete = currentFollowDids.filter(
130
130
+
(did) => !existingFollows.includes(did)
131
131
+
);
132
132
+
133
133
+
if (toDelete.length === 0) {
134
134
+
return { deleted: 0 };
135
135
+
}
136
136
+
137
137
+
// Delete in batches to avoid query size limits
138
138
+
const deleteBatchSize = 100;
139
139
+
const deletePromises = [];
140
140
+
for (let i = 0; i < toDelete.length; i += deleteBatchSize) {
141
141
+
const batch = toDelete.slice(i, i + deleteBatchSize);
142
142
+
deletePromises.push(
143
143
+
supabaseServerClient
144
144
+
.from("bsky_follows")
145
145
+
.delete()
146
146
+
.eq("identity", event.data.did)
147
147
+
.in("follows", batch)
148
148
+
);
149
149
+
}
150
150
+
151
151
+
await Promise.all(deletePromises);
152
152
+
return { deleted: toDelete.length };
153
153
+
});
120
154
return {
121
155
done: true,
122
156
};