+70
-9
main.ts
+70
-9
main.ts
···
1
import { simpleFetchHandler, XRPC } from "@atcute/client";
2
import "@atcute/bluesky/lexicons";
3
4
interface AccountMetadata {
5
did: string;
6
displayName: string;
7
avatarCid: string | null;
8
}
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,
16
}
17
18
const rpc = new XRPC({
···
29
};
30
const getAccountMetadata = async (did: `did:${string}:${string}`) => {
31
// gonna assume self exists in the app.bsky.actor.profile
32
-
const { data: { value } } = await rpc.get("com.atproto.repo.getRecord", {
33
params: {
34
repo: did,
35
collection: "app.bsky.actor.profile",
36
rkey: "self",
37
},
38
});
39
const account: AccountMetadata = {
40
did: did,
41
-
displayName: value.displayName,
42
avatarCid: null,
43
};
44
if (value.avatar) {
···
56
);
57
return metadata;
58
};
···
1
import { simpleFetchHandler, XRPC } from "@atcute/client";
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";
6
7
interface AccountMetadata {
8
did: string;
9
displayName: string;
10
avatarCid: string | null;
11
}
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
+
};
63
}
64
65
const rpc = new XRPC({
···
76
};
77
const getAccountMetadata = async (did: `did:${string}:${string}`) => {
78
// gonna assume self exists in the app.bsky.actor.profile
79
+
const { data } = await rpc.get("com.atproto.repo.getRecord", {
80
params: {
81
repo: did,
82
collection: "app.bsky.actor.profile",
83
rkey: "self",
84
},
85
});
86
+
const value = data.value as AppBskyActorDefs.ProfileView;
87
const account: AccountMetadata = {
88
did: did,
89
+
displayName: value.displayName || "",
90
avatarCid: null,
91
};
92
if (value.avatar) {
···
104
);
105
return metadata;
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"));