this repo has no description
atproto bluesky typescript express
at main 104 lines 2.8 kB view raw
1import { AtpAgent } from "@atproto/api"; 2 3export async function getTheme(agent: AtpAgent, did: string) { 4 try { 5 const response = await agent.com.atproto.repo.getRecord({ 6 repo: did, 7 collection: "lol.skittr.actor.theme", 8 rkey: "self", 9 }); 10 return response.data; 11 } catch (error: any) { 12 if (error.error === "RecordNotFound" || error.error === "InvalidRequest") { 13 // dummy record 14 return { 15 value: { 16 $type: "lol.skittr.actor.theme", 17 bg_color: "#9ae4e8", 18 text_color: "#000000", 19 link_color: "#0000ff", 20 side_color: "#e0ff92", 21 side_border: "#87bc44", 22 }, 23 }; 24 } 25 throw error; 26 } 27} 28 29export async function getBackground(agent: AtpAgent, did: string, cid: string) { 30 try { 31 const response = await agent.com.atproto.sync.getBlob({ 32 did: did, 33 cid: cid, 34 }); 35 return response; 36 } catch (error: any) { 37 throw error; 38 } 39} 40 41export async function getBackgroundBase64(agent: AtpAgent, did: string, cid: string) { 42 const blob = await getBackground(agent, did, cid); 43 const mimetype = blob.headers["content-type"] ?? "application/octet-stream"; 44 const base64 = Buffer.from(blob.data).toString("base64"); 45 return `data:${mimetype};base64,${base64}`; 46} 47 48// this code is incomplete 49export async function loadTheme(agent: AtpAgent, did: string) { 50 const theme = await getTheme(agent, did); 51 const cid = (theme as any).value?.background?.ref?.$link; 52 let bg = null; 53 54 if (cid) { 55 bg = await getBackgroundBase64(agent, did, cid); 56 } else { 57 bg = "../../../img/bg.gif"; 58 } 59 60 return { theme, bg }; 61} 62 63export async function putTheme(agent: AtpAgent, did: string, theme: any, bg?: any) { 64 const { bg_color, text_color, link_color, side_color, side_border, repeat = false } = theme; 65 const bgBuffer = Buffer.from(bg, "base64"); 66 let output = {}; 67 68 if (bg) { 69 const blob = await agent.uploadBlob(bgBuffer); 70 71 output = { 72 $type: "lol.skittr.actor.theme", 73 bg_color: bg_color, 74 text_color: text_color, 75 link_color: link_color, 76 side_color: side_color, 77 side_border: side_border, 78 background: { 79 $type: "blob", 80 ref: blob.data.blob.ref, 81 mimeType: blob.data.blob.mimeType, 82 size: blob.data.blob.size, 83 }, 84 repeat: repeat, 85 }; 86 } else { 87 output = { 88 $type: "lol.skittr.actor.theme", 89 bg_color: bg_color, 90 text_color: text_color, 91 link_color: link_color, 92 side_color: side_color, 93 side_border: side_border, 94 }; 95 } 96 97 const response = await agent.com.atproto.repo.putRecord({ 98 repo: did, 99 collection: "lol.skittr.actor.theme", 100 rkey: "self", 101 record: output, 102 }); 103 return response.data; 104}