your personal website on atproto - mirror blento.app
at fix-cached-posts 64 lines 2.7 kB view raw
1import type { CardDefinition } from '../../types'; 2import UpdatedBlentosCard from './UpdatedBlentosCard.svelte'; 3import type { Did } from '@atcute/lexicons'; 4import { getBlentoOrBskyProfile } from '$lib/atproto/methods'; 5 6type ProfileWithBlentoFlag = Awaited<ReturnType<typeof getBlentoOrBskyProfile>>; 7 8export const UpdatedBlentosCardDefitition = { 9 type: 'updatedBlentos', 10 contentComponent: UpdatedBlentosCard, 11 keywords: ['feed', 'updates', 'recent', 'activity'], 12 loadData: async (items, { cache }) => { 13 try { 14 const response = await fetch( 15 'https://ufos-api.microcosm.blue/records?collection=app.blento.card' 16 ); 17 const recentRecords = await response.json(); 18 const existingUsers = await cache?.get('meta', 'updatedBlentos'); 19 const existingUsersArray: ProfileWithBlentoFlag[] = existingUsers 20 ? JSON.parse(existingUsers) 21 : []; 22 23 const uniqueDids = new Set<Did>(recentRecords.map((v: { did: string }) => v.did as Did)); 24 25 const profiles: Promise<ProfileWithBlentoFlag | undefined>[] = []; 26 27 for (const did of Array.from(uniqueDids)) { 28 profiles.push(getBlentoOrBskyProfile({ did })); 29 } 30 31 for (let i = existingUsersArray.length - 1; i >= 0; i--) { 32 // if handle is handle.invalid, remove from existing users and add to profiles to refresh 33 if ( 34 (existingUsersArray[i].handle === 'handle.invalid' || 35 (!existingUsersArray[i].avatar && !existingUsersArray[i].hasBlento)) && 36 !uniqueDids.has(existingUsersArray[i].did) 37 ) { 38 const removed = existingUsersArray.splice(i, 1)[0]; 39 profiles.push(getBlentoOrBskyProfile({ did: removed.did })); 40 // if in unique dids, remove from older existing users and keep the newer one 41 // so updated profiles go first 42 } else if (uniqueDids.has(existingUsersArray[i].did)) { 43 existingUsersArray.splice(i, 1); 44 } 45 } 46 47 let result = [...(await Promise.all(profiles)), ...existingUsersArray]; 48 49 result = result.filter( 50 (v) => v && v.handle !== 'handle.invalid' && !v.handle.endsWith('.pds.rip') 51 ); 52 53 await cache?.put('meta', 'updatedBlentos', JSON.stringify(result)); 54 55 return JSON.parse(JSON.stringify(result.slice(0, 20))); 56 } catch (error) { 57 console.error('error fetching updated blentos', error); 58 return []; 59 } 60 } 61 // name: 'Updated Blentos', 62 // groups: ['Social'], 63 // icon: `<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" class="size-4"><path stroke-linecap="round" stroke-linejoin="round" d="M12 6v6h4.5m4.5 0a9 9 0 1 1-18 0 9 9 0 0 1 18 0ZM12 6c-1.602 0-3.155.474-4.434 1.357L18 16.791A8.959 8.959 0 0 0 21 12h-4.5Z" /></svg>` 64} as CardDefinition & { type: 'updatedBlentos' };