your personal website on atproto - mirror blento.app
at button 106 lines 2.9 kB view raw
1import { user, listRecords, getCDNImageBlobUrl } from '$lib/atproto'; 2import type { CardDefinition } from '../types'; 3import LivestreamCard from './LivestreamCard.svelte'; 4import LivestreamEmbedCard from './LivestreamEmbedCard.svelte'; 5 6export const LivestreamCardDefitition = { 7 type: 'latestLivestream', 8 contentComponent: LivestreamCard, 9 sidebarButtonText: 'stream.place info', 10 createNew: (card) => { 11 card.w = 4; 12 card.h = 4; 13 card.mobileH = 8; 14 card.mobileW = 8; 15 16 card.cardType = 'latestLivestream'; 17 }, 18 loadData: async (items, { did }) => { 19 const records = await listRecords({ did, collection: 'place.stream.livestream', limit: 3 }); 20 21 let latestLivestream: 22 | { 23 createdAt: string; 24 title: string; 25 thumb?: string; 26 href: string; 27 online?: boolean; 28 } 29 | undefined; 30 31 if (records?.length) { 32 const latest = JSON.parse(JSON.stringify(records?.[0])); 33 34 latestLivestream = { 35 createdAt: latest.value.createdAt, 36 title: latest.value?.title as string, 37 thumb: latest.value?.thumb?.ref?.$link 38 ? getCDNImageBlobUrl({ blob: latest.value.thumb, did }) 39 : undefined, 40 href: latest.value?.canonicalUrl || latest.value.url, 41 online: undefined 42 }; 43 } 44 45 if (latestLivestream) { 46 try { 47 const segmentsResponse = await fetch( 48 'https://stream.place/xrpc/place.stream.live.getSegments?userDID=' + 49 encodeURIComponent(did) 50 ); 51 const segments = await segmentsResponse.json(); 52 53 const lastSegment = segments.segments[0]; 54 const startTime = new Date(lastSegment.record.startTime).getTime(); 55 56 const FIVE_MINUTES = 5 * 60 * 1000; 57 const now = Date.now(); 58 59 latestLivestream.online = now - startTime <= FIVE_MINUTES; 60 } catch (error) { 61 console.error(error); 62 } 63 } 64 65 return latestLivestream; 66 }, 67 68 onUrlHandler: (url, item) => { 69 console.log(url, 'https://stream.place/' + user.profile?.handle); 70 if (url === 'https://stream.place/' + user.profile?.handle) { 71 item.w = 4; 72 item.h = 4; 73 item.mobileH = 8; 74 item.mobileW = 8; 75 item.cardData.href = 'https://stream.place/' + user.profile?.handle; 76 return item; 77 } 78 }, 79 80 canChange: (item) => item.cardData.href === 'https://stream.place/' + user.profile?.handle, 81 82 urlHandlerPriority: 5, 83 84 name: 'stream.place Card' 85} as CardDefinition & { type: 'latestLivestream' }; 86 87export const LivestreamEmbedCardDefitition = { 88 type: 'livestreamEmbed', 89 contentComponent: LivestreamEmbedCard, 90 createNew: (card) => { 91 card.w = 4; 92 card.h = 2; 93 card.mobileW = 8; 94 card.mobileH = 4; 95 96 card.cardData = { 97 href: 'https://stream.place/' + user.profile?.handle, 98 embed: 'https://stream.place/embed/' + user.profile?.handle 99 }; 100 } 101 // canChange: (item) => item.cardData.href === 'https://stream.place/' + client.profile?.handle, 102 103 // change: (item) => { 104 // item.cardData.embed = 'https://stream.place/embed/' + client.profile?.handle; 105 // }, 106} as CardDefinition & { type: 'livestreamEmbed' };