this repo has no description

Posts constructor

ari.express 4f98a911 a29cdc09

verified
Changed files
+70 -9
+70 -9
main.ts
··· 1 1 import { simpleFetchHandler, XRPC } from "@atcute/client"; 2 2 import "@atcute/bluesky/lexicons"; 3 + import { ComAtprotoRepoListRecords } from "@atcute/client/lexicons"; 4 + import { AppBskyFeedPost } from "@atcute/client/lexicons"; 5 + import { AppBskyActorDefs } from "@atcute/client/lexicons"; 3 6 4 7 interface AccountMetadata { 5 8 did: string; 6 9 displayName: string; 7 10 avatarCid: string | null; 8 11 } 9 - interface Post { 10 - text: string, 11 - timestamp: number, 12 - quoting: string | null, 13 - replying: string | null, 14 - imagesLinks: string[] | null, 15 - videosLinks: string[] | null, 12 + class Post { 13 + text: string; 14 + timestamp: number; 15 + quotingDid: string | null; 16 + replyingDid: string | null; 17 + imagesLinksCid: string[] | null; 18 + videosLinkCid: string | null; 19 + constructor(record : ComAtprotoRepoListRecords.Record) { 20 + const post = record.value as AppBskyFeedPost.Record; 21 + this.text = post.text; 22 + this.timestamp = Date.parse(post.createdAt); 23 + if (post.reply) { 24 + this.replyingDid = didFromATuri(post.reply.parent.uri).repo; 25 + } else { 26 + this.replyingDid = null; 27 + } 28 + this.quotingDid = null; 29 + this.imagesLinksCid = null; 30 + this.videosLinkCid = null; 31 + switch (post.embed?.$type) { 32 + case "app.bsky.embed.images": 33 + this.imagesLinksCid = post.embed.images.map ((imageRecord) => imageRecord.image.ref.$link); 34 + break; 35 + case "app.bsky.embed.video": 36 + this.videosLinkCid = post.embed.video.ref.$link; 37 + break; 38 + case "app.bsky.embed.record": 39 + this.quotingDid = didFromATuri(post.embed.record.uri).repo; 40 + break; 41 + case "app.bsky.embed.recordWithMedia": 42 + this.quotingDid = didFromATuri(post.embed.record.record.uri).repo; 43 + switch (post.embed.media.$type) { 44 + case "app.bsky.embed.images": 45 + this.imagesLinksCid = post.embed.media.images.map ((imageRecord) => imageRecord.image.ref.$link); 46 + break; 47 + case "app.bsky.embed.video": 48 + this.videosLinkCid = post.embed.media.video.ref.$link; 49 + break; 50 + } 51 + break; 52 + } 53 + } 54 + } 55 + 56 + const didFromATuri = (aturi : string) => { 57 + const parts = aturi.split('/'); 58 + return { 59 + repo: parts[2], 60 + collection: parts[3], 61 + rkey: parts[4] 62 + }; 16 63 } 17 64 18 65 const rpc = new XRPC({ ··· 29 76 }; 30 77 const getAccountMetadata = async (did: `did:${string}:${string}`) => { 31 78 // gonna assume self exists in the app.bsky.actor.profile 32 - const { data: { value } } = await rpc.get("com.atproto.repo.getRecord", { 79 + const { data } = await rpc.get("com.atproto.repo.getRecord", { 33 80 params: { 34 81 repo: did, 35 82 collection: "app.bsky.actor.profile", 36 83 rkey: "self", 37 84 }, 38 85 }); 86 + const value = data.value as AppBskyActorDefs.ProfileView; 39 87 const account: AccountMetadata = { 40 88 did: did, 41 - displayName: value.displayName, 89 + displayName: value.displayName || "", 42 90 avatarCid: null, 43 91 }; 44 92 if (value.avatar) { ··· 56 104 ); 57 105 return metadata; 58 106 }; 107 + 108 + const fetchPosts = async (did: string) => { 109 + const { data } = await rpc.get("com.atproto.repo.listRecords", { 110 + params: { 111 + repo: did, 112 + collection: "app.bsky.feed.post", 113 + limit: 5 114 + } 115 + }); 116 + return data.records as ComAtprotoRepoListRecords.Record[]; 117 + } 118 + // console.log((await fetchPosts("did:web:astrra.space")).map((record : any) => new Post(record))) 119 + console.log(await getAccountMetadata("did:web:astrra.space"));