my website at ewancroft.uk

feat(atproto): add Kibun status fetcher and type definitions

ewancroft.uk 491fff27 aa72130d

verified
Changed files
+76 -3
src
lib
services
+60 -1
src/lib/services/atproto/fetch.ts
··· 1 1 import { PUBLIC_ATPROTO_DID } from '$env/static/public'; 2 2 import { cache } from './cache'; 3 3 import { withFallback, resolveIdentity } from './agents'; 4 - import type { ProfileData, StatusData, SiteInfoData, LinkData, MusicStatusData } from './types'; 4 + import type { 5 + ProfileData, 6 + StatusData, 7 + SiteInfoData, 8 + LinkData, 9 + MusicStatusData, 10 + KibunStatusData 11 + } from './types'; 5 12 import { buildPdsBlobUrl } from './media'; 6 13 import { findArtwork } from './musicbrainz'; 7 14 ··· 339 346 return null; 340 347 } 341 348 } 349 + 350 + /** 351 + * Fetches Kibun status from social.kibun.status collection 352 + */ 353 + export async function fetchKibunStatus(fetchFn?: typeof fetch): Promise<KibunStatusData | null> { 354 + console.info('[KibunStatus] Fetching kibun status data'); 355 + const cacheKey = `kibun-status:${PUBLIC_ATPROTO_DID}`; 356 + const cached = cache.get<KibunStatusData>(cacheKey); 357 + if (cached) { 358 + console.debug('[KibunStatus] Returning cached kibun status data'); 359 + return cached; 360 + } 361 + 362 + try { 363 + console.info('[KibunStatus] Cache miss, fetching from network'); 364 + 365 + const statusRecords = await withFallback( 366 + PUBLIC_ATPROTO_DID, 367 + async (agent) => { 368 + const response = await agent.com.atproto.repo.listRecords({ 369 + repo: PUBLIC_ATPROTO_DID, 370 + collection: 'social.kibun.status', 371 + limit: 1 372 + }); 373 + return response.data.records; 374 + }, 375 + true, 376 + fetchFn 377 + ); 378 + 379 + if (statusRecords && statusRecords.length > 0) { 380 + const record = statusRecords[0]; 381 + const value = record.value as any; 382 + 383 + const data: KibunStatusData = { 384 + text: value.text, 385 + emoji: value.emoji, 386 + createdAt: value.createdAt, 387 + $type: 'social.kibun.status' 388 + }; 389 + 390 + console.info('[KibunStatus] Successfully fetched kibun status'); 391 + cache.set(cacheKey, data); 392 + return data; 393 + } 394 + 395 + return null; 396 + } catch (error) { 397 + console.error('[KibunStatus] Failed to fetch kibun status from all sources:', error); 398 + return null; 399 + } 400 + }
+9 -2
src/lib/services/atproto/index.ts
··· 29 29 ResolvedIdentity, 30 30 CacheEntry, 31 31 MusicStatusData, 32 - MusicArtist 32 + MusicArtist, 33 + KibunStatusData 33 34 } from './types'; 34 35 35 36 export type { TangledRepo, TangledReposData } from './tangled'; 36 37 37 38 // Export fetch functions 38 - export { fetchProfile, fetchSiteInfo, fetchLinks, fetchMusicStatus } from './fetch'; 39 + export { 40 + fetchProfile, 41 + fetchSiteInfo, 42 + fetchLinks, 43 + fetchMusicStatus, 44 + fetchKibunStatus 45 + } from './fetch'; 39 46 40 47 export { fetchTangledRepos } from './tangled'; 41 48
+7
src/lib/services/atproto/types.ts
··· 216 216 }; 217 217 artworkUrl?: string; // Computed URL for display 218 218 } 219 + 220 + export interface KibunStatusData { 221 + text: string; 222 + emoji: string; 223 + createdAt: string; 224 + $type: 'social.kibun.status'; 225 + }