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