Live video on the AT Protocol
1import { PlaceStreamChatProfile } from "streamplace";
2import {
3 getStreamplaceStoreFromContext,
4 useDID,
5 useStreamplaceStore,
6} from "./streamplace-store";
7import { usePDSAgent } from "./xrpc";
8
9export function useGetChatProfile() {
10 const did = useDID();
11 const pdsAgent = usePDSAgent();
12 const store = getStreamplaceStoreFromContext();
13 const createEmptyChatProfile = useCreateEmptyChatProfile();
14
15 return async () => {
16 if (!did || !pdsAgent) {
17 throw new Error("No DID or PDS agent");
18 }
19 let res;
20 try {
21 res = await pdsAgent.com.atproto.repo.getRecord({
22 repo: did,
23 collection: "place.stream.chat.profile",
24 rkey: "self",
25 });
26 } catch (e) {
27 console.error(
28 "Failed to get chat profile record, attempting creation",
29 e,
30 );
31 }
32 if (!res || !res.success) {
33 try {
34 await createEmptyChatProfile();
35 res = await pdsAgent.com.atproto.repo.getRecord({
36 repo: did,
37 collection: "place.stream.chat.profile",
38 rkey: "self",
39 });
40 } catch (e) {
41 console.error("Failed to create empty chat profile record", e);
42 }
43 }
44
45 if (PlaceStreamChatProfile.isRecord(res.data.value)) {
46 store.setState({ chatProfile: res.data.value });
47 } else {
48 console.log("not a record", res.data.value);
49 }
50 };
51}
52
53export function useCreateEmptyChatProfile() {
54 const did = useDID();
55 const pdsAgent = usePDSAgent();
56
57 return async () => {
58 if (!did || !pdsAgent) {
59 throw new Error("No DID or PDS agent");
60 }
61 const res = await pdsAgent.com.atproto.repo.putRecord({
62 repo: did,
63 collection: "place.stream.chat.profile",
64 rkey: "self",
65 record: {
66 $type: "place.stream.chat.profile",
67 color:
68 DEFAULT_COLORS[Math.floor(Math.random() * DEFAULT_COLORS.length)],
69 },
70 });
71 if (!res.success) {
72 throw new Error("Failed to create empty chat profile record");
73 }
74 };
75}
76
77export function useGetBskyProfile() {
78 const did = useDID();
79 const pdsAgent = usePDSAgent();
80 const store = getStreamplaceStoreFromContext();
81
82 return async () => {
83 if (!did || !pdsAgent) {
84 throw new Error("No DID or PDS agent");
85 }
86 const res = await pdsAgent.app.bsky.actor.getProfile({
87 actor: did,
88 });
89 if (!res.success) {
90 throw new Error("Failed to get chat profile record");
91 }
92
93 store.setState({ handle: res.data.handle });
94 };
95}
96
97export function useChatProfile() {
98 return useStreamplaceStore((x) => x.chatProfile);
99}
100
101const DEFAULT_COLORS: PlaceStreamChatProfile.Color[] = [
102 { red: 244, green: 67, blue: 54 },
103 { red: 233, green: 30, blue: 99 },
104 { red: 156, green: 39, blue: 176 },
105 { red: 103, green: 58, blue: 183 },
106 { red: 63, green: 81, blue: 181 },
107 { red: 33, green: 150, blue: 243 },
108 { red: 3, green: 169, blue: 244 },
109 { red: 0, green: 188, blue: 212 },
110 { red: 0, green: 150, blue: 136 },
111 { red: 76, green: 175, blue: 80 },
112 { red: 139, green: 195, blue: 74 },
113 { red: 205, green: 220, blue: 57 },
114 { red: 255, green: 235, blue: 59 },
115 { red: 255, green: 193, blue: 7 },
116 { red: 255, green: 152, blue: 0 },
117 { red: 255, green: 87, blue: 34 },
118 { red: 121, green: 85, blue: 72 },
119 { red: 158, green: 158, blue: 158 },
120 { red: 96, green: 125, blue: 139 },
121];