···
291
291
// Loading a client for a stored community
292
292
// -------------------------------------------------------------------------
293
293
294
294
+
/**
295
295
+
* Re-read the community's on-network profile (via the public appview) +
296
296
+
* `garden.atmo.community/self` record and write the fresh values into
297
297
+
* the D1 cache. Used by the cron tick AND by the editCommunity remote so
298
298
+
* an edit takes effect immediately instead of waiting up to a minute for
299
299
+
* the next cron. Fails soft — errors just leave the cache stale until
300
300
+
* the next cron tick catches up.
301
301
+
*/
302
302
+
export async function refreshCommunityCache(
303
303
+
env: App.Platform['env'],
304
304
+
row: CommunityRow
305
305
+
): Promise<void> {
306
306
+
const appview = new Client({
307
307
+
handler: simpleFetchHandler({ service: PUBLIC_APPVIEW })
308
308
+
});
309
309
+
try {
310
310
+
const [profile, config] = await Promise.all([
311
311
+
appview.get('app.bsky.actor.getProfile', { params: { actor: row.did } }),
312
312
+
fetchCommunityConfig(row.pds, row.did)
313
313
+
]);
314
314
+
if (profile.ok) {
315
315
+
await updateCommunityProfile(env.DB, row.did, {
316
316
+
display_name: profile.data.displayName ?? null,
317
317
+
avatar: profile.data.avatar ?? null,
318
318
+
description: stripCommunityLink(profile.data.description),
319
319
+
accent_color: config.accentColor,
320
320
+
followers_count: profile.data.followersCount ?? 0
321
321
+
});
322
322
+
}
323
323
+
} catch (e) {
324
324
+
console.error('[refreshCommunityCache] failed', e);
325
325
+
}
326
326
+
}
327
327
+
294
328
async function loadClient(
295
329
env: App.Platform['env'],
296
330
row: CommunityRow
···
1258
1292
const errors: string[] = [];
1259
1293
const communities = await listCommunities(db);
1260
1294
1261
1261
-
const appview = new Client({
1262
1262
-
handler: simpleFetchHandler({ service: PUBLIC_APPVIEW })
1263
1263
-
});
1264
1264
-
1265
1295
let postsCreated = 0;
1266
1296
for (const row of communities) {
1267
1297
try {
···
1272
1302
}
1273
1303
1274
1304
// Best-effort: refresh cached profile metadata (avatar, display name,
1275
1275
-
// desc, follower count) from the appview, and the accent color from
1276
1276
-
// the community's `garden.atmo.community/self` record on the PDS.
1277
1277
-
// Both requests run in parallel since they hit different services.
1278
1278
-
// `followersCount` already comes back on every getProfile call, so
1279
1279
-
// caching it is free — no separate refresh schedule needed.
1280
1280
-
try {
1281
1281
-
const [profile, config] = await Promise.all([
1282
1282
-
appview.get('app.bsky.actor.getProfile', { params: { actor: row.did } }),
1283
1283
-
fetchCommunityConfig(row.pds, row.did)
1284
1284
-
]);
1285
1285
-
if (profile.ok) {
1286
1286
-
await updateCommunityProfile(db, row.did, {
1287
1287
-
display_name: profile.data.displayName ?? null,
1288
1288
-
avatar: profile.data.avatar ?? null,
1289
1289
-
description: stripCommunityLink(profile.data.description),
1290
1290
-
accent_color: config.accentColor,
1291
1291
-
followers_count: profile.data.followersCount ?? 0
1292
1292
-
});
1293
1293
-
}
1294
1294
-
} catch {
1295
1295
-
// non-fatal
1296
1296
-
}
1305
1305
+
// desc, follower count) from the appview + accent color from the
1306
1306
+
// community's `garden.atmo.community/self` record.
1307
1307
+
await refreshCommunityCache(env, row);
1297
1308
}
1298
1309
1299
1310
// Drain the jetstream for `garden.atmo.submission` records created via the
···
15
15
registerCommunity,
16
16
updateCommunity,
17
17
fetchCommunityConfig,
18
18
-
checkCanSubmit
18
18
+
checkCanSubmit,
19
19
+
refreshCommunityCache
19
20
} from '../bot';
20
21
import { parseListUri } from '../list-uri';
21
22
import {
···
325
326
console.error('[editCommunity]', e);
326
327
error(400, `Update failed: ${msg}`);
327
328
}
329
329
+
330
330
+
// Force-refresh the D1 cache so the caller's next page load already
331
331
+
// shows the new avatar/description/accent color, instead of waiting
332
332
+
// up to a minute for the next cron tick. Soft-failure: if the
333
333
+
// appview hasn't seen the change yet (PDS → relay → appview lag),
334
334
+
// the cache stays briefly stale and the cron picks it up next tick.
335
335
+
await refreshCommunityCache(env, row);
328
336
329
337
return { ok: true, did: row.did, handle: row.handle };
330
338
}