tangled
alpha
login
or
join now
witchcraft.systems
/
pds-dash
15
fork
atom
this repo has no description
15
fork
atom
overview
issues
pulls
pipelines
Posts constructor
ari.express
10 months ago
4f98a911
a29cdc09
verified
This commit was signed with the committer's
known signature
.
ari.express
SSH Key Fingerprint:
SHA256:j4xpQafvRcIH4rwZqM5aREIogWsCjyYohia7vH0+uZY=
+70
-9
1 changed file
expand all
collapse all
unified
split
main.ts
+70
-9
main.ts
···
1
1
import { simpleFetchHandler, XRPC } from "@atcute/client";
2
2
import "@atcute/bluesky/lexicons";
3
3
+
import { ComAtprotoRepoListRecords } from "@atcute/client/lexicons";
4
4
+
import { AppBskyFeedPost } from "@atcute/client/lexicons";
5
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
9
-
interface Post {
10
10
-
text: string,
11
11
-
timestamp: number,
12
12
-
quoting: string | null,
13
13
-
replying: string | null,
14
14
-
imagesLinks: string[] | null,
15
15
-
videosLinks: string[] | null,
12
12
+
class Post {
13
13
+
text: string;
14
14
+
timestamp: number;
15
15
+
quotingDid: string | null;
16
16
+
replyingDid: string | null;
17
17
+
imagesLinksCid: string[] | null;
18
18
+
videosLinkCid: string | null;
19
19
+
constructor(record : ComAtprotoRepoListRecords.Record) {
20
20
+
const post = record.value as AppBskyFeedPost.Record;
21
21
+
this.text = post.text;
22
22
+
this.timestamp = Date.parse(post.createdAt);
23
23
+
if (post.reply) {
24
24
+
this.replyingDid = didFromATuri(post.reply.parent.uri).repo;
25
25
+
} else {
26
26
+
this.replyingDid = null;
27
27
+
}
28
28
+
this.quotingDid = null;
29
29
+
this.imagesLinksCid = null;
30
30
+
this.videosLinkCid = null;
31
31
+
switch (post.embed?.$type) {
32
32
+
case "app.bsky.embed.images":
33
33
+
this.imagesLinksCid = post.embed.images.map ((imageRecord) => imageRecord.image.ref.$link);
34
34
+
break;
35
35
+
case "app.bsky.embed.video":
36
36
+
this.videosLinkCid = post.embed.video.ref.$link;
37
37
+
break;
38
38
+
case "app.bsky.embed.record":
39
39
+
this.quotingDid = didFromATuri(post.embed.record.uri).repo;
40
40
+
break;
41
41
+
case "app.bsky.embed.recordWithMedia":
42
42
+
this.quotingDid = didFromATuri(post.embed.record.record.uri).repo;
43
43
+
switch (post.embed.media.$type) {
44
44
+
case "app.bsky.embed.images":
45
45
+
this.imagesLinksCid = post.embed.media.images.map ((imageRecord) => imageRecord.image.ref.$link);
46
46
+
break;
47
47
+
case "app.bsky.embed.video":
48
48
+
this.videosLinkCid = post.embed.media.video.ref.$link;
49
49
+
break;
50
50
+
}
51
51
+
break;
52
52
+
}
53
53
+
}
54
54
+
}
55
55
+
56
56
+
const didFromATuri = (aturi : string) => {
57
57
+
const parts = aturi.split('/');
58
58
+
return {
59
59
+
repo: parts[2],
60
60
+
collection: parts[3],
61
61
+
rkey: parts[4]
62
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
32
-
const { data: { value } } = await rpc.get("com.atproto.repo.getRecord", {
79
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
86
+
const value = data.value as AppBskyActorDefs.ProfileView;
39
87
const account: AccountMetadata = {
40
88
did: did,
41
41
-
displayName: value.displayName,
89
89
+
displayName: value.displayName || "",
42
90
avatarCid: null,
43
91
};
44
92
if (value.avatar) {
···
56
104
);
57
105
return metadata;
58
106
};
107
107
+
108
108
+
const fetchPosts = async (did: string) => {
109
109
+
const { data } = await rpc.get("com.atproto.repo.listRecords", {
110
110
+
params: {
111
111
+
repo: did,
112
112
+
collection: "app.bsky.feed.post",
113
113
+
limit: 5
114
114
+
}
115
115
+
});
116
116
+
return data.records as ComAtprotoRepoListRecords.Record[];
117
117
+
}
118
118
+
// console.log((await fetchPosts("did:web:astrra.space")).map((record : any) => new Post(record)))
119
119
+
console.log(await getAccountMetadata("did:web:astrra.space"));