This repository was archived by the owner on Apr 3, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost.ts
More file actions
207 lines (193 loc) · 5.22 KB
/
post.ts
File metadata and controls
207 lines (193 loc) · 5.22 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import { Project } from "./project.js";
/**
* Whether a post is a draft or published.
*/
enum PostState {
DRAFT = 0,
PUBLISHED = 1,
}
/**
* A class representing a post on Cohost.
*
* This class does not contain all the data that a post can have. See the {@link TimelinePost} class for a more complete representation of a post.
*
* This class is not used for creating posts. Instead, use the PostBuilder class to create a new post.
*/
class Post {
projectHandle: string;
postId: number;
content: {
postState: PostState;
headline: string;
adultContent: boolean;
blocks: object[];
cws: string[];
tags: string[];
};
/* @hidden */
constructor(
projectHandle: string,
postId: number,
content: {
postState: PostState;
headline: string;
adultContent: boolean;
blocks: object[];
cws: string[];
tags: string[];
},
) {
this.projectHandle = projectHandle;
this.postId = postId;
this.content = content;
}
}
/**
* A class representing a post on Cohost, with all the data that a post can have.
*
* This class is used when retrieving posts from the Cohost API, rather than the base {@link Post} class.
*/
class TimelinePost extends Post {
publishedAt: string;
filename: string;
numComments: number;
pinned: boolean;
commentsLocked: boolean;
sharesLocked: boolean;
plainTextBody: string;
postingProject: Project;
singlePostPageUrl: string;
isLiked: boolean;
responseToAskId: number | null;
hasCohostPlus: boolean;
/* @hidden */
constructor(
projectHandle: string,
postId: number,
state: PostState,
headline: string,
effectiveAdultContent: boolean,
blocks: object[],
cws: string[],
tags: string[],
publishedAt: string,
filename: string,
numComments: number,
pinned: boolean,
commentsLocked: boolean,
sharesLocked: boolean,
plainTextBody: string,
postingProject: Project,
singlePostPageUrl: string,
isLiked: boolean,
responseToAskId: number | null,
hasCohostPlus: boolean,
) {
super(projectHandle, postId, {
postState: state,
headline,
adultContent: effectiveAdultContent,
blocks,
cws,
tags,
});
this.publishedAt = publishedAt;
this.filename = filename;
this.numComments = numComments;
this.pinned = pinned;
this.commentsLocked = commentsLocked;
this.sharesLocked = sharesLocked;
this.plainTextBody = plainTextBody;
this.postingProject = postingProject;
this.singlePostPageUrl = singlePostPageUrl;
this.isLiked = isLiked;
this.responseToAskId = responseToAskId;
this.hasCohostPlus = hasCohostPlus;
}
}
/**
* A class for creating Post objects.
*
* Example usage:
* ```ts
* let postBuilder = new PostBuilder("Hello, world!");
* postBuilder.addMarkdownBlock("This is a test post from cohost-api.");
* postBuilder.addTag("cohost-api");
* let finishedPost = postBuilder.build();
* ```
*/
class PostBuilder {
/* @hidden */
private headline: string;
/* @hidden */
private adultContent: boolean;
/* @hidden */
private blocks: object[] = [];
/* @hidden */
private cws: string[] = [];
/* @hidden */
private tags: string[] = [];
/**
* Creates a new PostBuilder, used for easily constructing a Post object.
* @param headline The headline for the post. Appears at the top of a post. Defaults to an empty string.
* @param adultContent Whether the post contains adult content. Defaults to false.
*/
constructor(headline: string = "", adultContent: boolean = false) {
this.headline = headline;
this.adultContent = adultContent;
}
/**
* Adds a block of content to the post. Only use this if you are familiar with the types of content blocks that Cohost supports. Otherwise, use `addMarkdownBlock()`.
* @param type The type of content block to add.
* @param block The content of the block.
* @returns The PostBuilder object, for chaining.
*/
addBlock(type: string, block: object) {
let finalBlock: any = { type };
finalBlock[type] = block;
this.blocks.push(finalBlock);
return this;
}
/**
* Adds a block of markdown text to the post.
* @param content The markdown string to add to the post.
* @returns The PostBuilder object, for chaining.
*/
addMarkdownBlock(content: string) {
this.blocks.push({ type: "markdown", markdown: { content } });
return this;
}
/**
* Adds a content warning to the post.
* @param cw The content warning to add to the post.
* @returns The PostBuilder object, for chaining.
*/
addCw(cw: string) {
this.cws.push(cw);
return this;
}
/**
* Adds a #tag to the post.
* @param tag The #tag to add to the post. You do not need to include the # symbol.
* @returns The PostBuilder object, for chaining.
*/
addTag(tag: string) {
this.tags.push(tag);
return this;
}
/**
* Builds the Post object from the data in the PostBuilder.
* @returns The Post object.
*/
build() {
return new Post("", 0, {
postState: PostState.DRAFT,
headline: this.headline,
adultContent: this.adultContent,
blocks: this.blocks,
cws: this.cws,
tags: this.tags,
});
}
}
export { Post, TimelinePost, PostBuilder, PostState };