-
Notifications
You must be signed in to change notification settings - Fork 1
/
PostReference.ts
128 lines (113 loc) · 2.87 KB
/
PostReference.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import type { Bot, BotPostOptions, StrongRef } from "../../bot/Bot.js";
import type { Post } from "./Post.js";
import type { PostPayload, ReplyRef } from "./PostPayload.js";
/**
* Data used to construct a PostReference class.
* @see PostReference
*/
export interface PostReferenceData {
uri: string;
cid: string;
replyRef?: ReplyRef | undefined;
}
/**
* A reference to a post.
*/
export class PostReference implements StrongRef {
/** The post's AT URI. */
uri: string;
/** The post's CID. */
cid: string;
/** A reference to the post's parent and root post. */
replyRef?: ReplyRef;
/**
* @param data Data used to construct the reference.
* @param bot The active Bot instance.
*/
constructor({ uri, cid, replyRef }: PostReferenceData, protected bot: Bot) {
this.uri = uri;
this.cid = cid;
if (replyRef) {
this.replyRef = { parent: { uri: replyRef.parent.uri, cid: replyRef.parent.cid } };
if (replyRef.root) {
this.replyRef.root = { uri: replyRef.root.uri, cid: replyRef.root.cid };
}
}
}
/**
* Fetch the full referenced post.
*/
async fetch(): Promise<Post> {
return this.bot.getPost(this.uri);
}
/**
* Reply to the post.
* @param payload The post payload.
* @param options Optional configuration.
* @returns A reference to the created post.
*/
async reply(payload: PostPayload, options: BotPostOptions = {}): Promise<PostReference> {
return this.bot.post({
...payload,
replyRef: {
parent: { uri: this.uri, cid: this.cid },
root: this.replyRef?.root ?? { uri: this.uri, cid: this.cid },
},
}, options);
}
/**
* Create a new post with this post quoted.
* @param payload The post payload.
* @param options Optional configuration.
* @returns A reference to the created post.
*/
async quote(payload: PostPayload, options: BotPostOptions = {}): Promise<PostReference> {
return this.bot.post({ ...payload, quoted: this }, options);
}
/**
* Like the post.
*/
async like() {
return this.bot.like(this);
}
/**
* Unlike the post.
*/
async unlike() {
return this.bot.unlike(this.uri);
}
/**
* Repost the post.
*/
async repost() {
return this.bot.repost(this);
}
/**
* If this post has been reposted, delete the repost.
*/
async deleteRepost() {
return this.bot.deleteRepost(this.uri);
}
/**
* Delete the post.
*/
async delete() {
return this.bot.deletePost(this.uri);
}
/**
* Apply labels to the post.
* @param labels The labels to apply.
* @param comment An optional comment.
*/
async label(labels: Array<string>, comment?: string) {
return this.bot.label({ reference: this, labels, comment });
}
/**
* Negate labels previously applied to the post.
* @param labels The labels to negate.
* @param comment An optional comment.
*/
async negateLabels(labels: Array<string>, comment?: string) {
return this.bot.negateLabels({ reference: this, labels, comment });
}
}