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 pathproject.ts
More file actions
372 lines (344 loc) · 9.92 KB
/
project.ts
File metadata and controls
372 lines (344 loc) · 9.92 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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import mime from "mime";
import imageSize = require("image-size");
import fs = require("fs");
import path = require("path");
import { Post, TimelinePost, PostState } from "./post.js";
import { Client } from "./client.js";
enum Privacy {
PUBLIC = "public",
PRIVATE = "private",
}
enum SortOrder {
RECENTLY_POSTED = "recently-posted",
FOLLOWED_ASC = "followed-asc",
FOLLOWED_DESC = "followed-desc",
ALPHA_ASC = "alpha-asc",
ALPHA_DESC = "alpha-desc",
}
/**
* A class representing a project on Cohost. A user can have multiple projects.
*
* Do not instantiate this class directly. Projects can only be obtained from the API. cohost-api does not yet support creating new projects.
*/
class Project {
/* @hidden */
protected client: Client;
/* The unique ID of the project. */
id: number;
/* The handle of the project, without the @. */
handle: string;
/* The display name, which shows up on the timeline. */
displayName: string;
/* The project subheading. */
dek: string;
/* The description/bio of the project. */
description: string;
avatarURL: string;
headerURL: string;
headerPreviewURL: string;
privacy: Privacy;
url: string;
pronouns: string;
flags: string[];
avatarShape: string;
loggedOutPostVisibility: Privacy;
frequentlyUsedTags: string[];
atomFeedURL: string;
/* @hidden */
constructor(
client: Client,
{
projectId,
handle,
displayName,
dek,
description,
avatarURL,
headerURL,
headerPreviewURL,
privacy,
url,
pronouns,
flags,
avatarShape,
loggedOutPostVisibility,
frequentlyUsedTags,
}: {
projectId: number;
handle: string;
displayName: string;
dek: string;
description: string;
avatarURL: string;
headerURL: string;
headerPreviewURL: string;
privacy: Privacy;
url: string;
pronouns: string;
flags: string[];
avatarShape: string;
loggedOutPostVisibility: Privacy;
frequentlyUsedTags: string[];
},
) {
this.client = client;
this.id = projectId;
this.handle = handle;
this.displayName = displayName;
this.dek = dek;
this.description = description;
this.avatarURL = avatarURL;
this.headerURL = headerURL;
this.headerPreviewURL = headerPreviewURL;
this.privacy = privacy;
this.url = url;
this.pronouns = pronouns;
this.flags = flags;
this.avatarShape = avatarShape;
this.loggedOutPostVisibility = loggedOutPostVisibility;
this.frequentlyUsedTags = frequentlyUsedTags;
this.atomFeedURL = `https://cohost.org/@${handle}/rss/public.atom`;
}
/**
* Gets a page of posts from the project. The Cohost API returns 20 posts per page.
* @param page The page of posts to get. Defaults to 0.
* @param options Options for filtering the returned posts.
* @returns An array of TimelinePosts from the project.
*/
async getPosts(
page: number = 0,
options: {
pinnedPostsAtTop: boolean;
hideReplies: boolean;
hideShares: boolean;
hideAsks: boolean;
} = {
pinnedPostsAtTop: true,
hideReplies: false,
hideShares: false,
hideAsks: false,
},
) {
let response = await this.client.trpc.posts.profilePosts.query({
projectHandle: this.handle,
page,
options,
});
return response.posts.map((post: any) => {
return new TimelinePost(
this.handle,
post.postId,
post.postState,
post.headline,
post.effectiveAdultContent,
post.blocks,
post.cws,
post.tags,
post.publishedAt,
post.filename,
post.numComments,
post.pinned,
post.commentsLocked,
post.sharesLocked,
post.plainTextBody,
this,
post.singlePostPageUrl,
post.isLiked,
post.responseToAskId,
post.hasCohostPlus,
);
});
}
}
/**
* A class representing a project on Cohost that the user has editing privileges for.
*/
class EditedProject extends Project {
/**
* Publishes a post to the project.
* @param post The post to publish.
* @returns The post with its postId set.
*/
async createPost(post: Post) {
post.projectHandle = this.handle;
post.content.postState = PostState.PUBLISHED;
post.postId = (await this.client.trpc.posts.create.mutate(post)).postId;
return post;
}
/**
* Creates a draft of a post in the project.
* @param post The post to send to the project's drafts.
* @returns The post with its postId set.
*/
async createDraft(post: Post) {
post.projectHandle = this.handle;
post.content.postState = PostState.DRAFT;
post.postId = (await this.client.trpc.posts.create.mutate(post)).postId;
return post;
}
/**
* Deletes a post from the project.
* @param post The post or ID of the post to delete.
*/
async deletePost(post: Post | number) {
let postId = post instanceof Post ? post.postId : post;
await this.client.trpc.posts.delete.mutate({
projectHandle: this.handle,
postId,
});
}
/**
* Updates a post in the project.
* @param postToUpdate The post or ID of the post to update.
* @param newPost The new post data.
*/
async updatePost(postToUpdate: Post | number, newPost: Post) {
let postId =
postToUpdate instanceof Post ? postToUpdate.postId : postToUpdate;
await this.client.trpc.posts.update.mutate({
projectHandle: this.handle,
postId,
content: newPost.content,
cws: newPost.content.cws,
tags: newPost.content.tags,
});
}
/**
* Publishes a post from the project's drafts.
*
* In the future, this function will also accept a number as the first argument, which will be the ID of the draft post to publish.
* @param draftPost The draft post to publish.
*/
async publishDraft(draftPost: Post) {
await this.client.trpc.posts.update.mutate({
projectHandle: this.handle,
postId: draftPost.postId,
content: draftPost.content,
cws: draftPost.content.cws,
tags: draftPost.content.tags,
});
}
/**
* Adds an attachment to a post. This must be done after the post is created. This is a limitation of the Cohost API.
* @param post The post to add the attachment to.
* @param filepath The path to the file to upload.
* @returns The ID of the attachment.
*/
async addAttachment(post: Post, filepath: string): Promise<number>;
/**
* Adds an attachment to a post. This must be done after the post is created. This is a limitation of the Cohost API.
* @param post The post to add the attachment to.
* @param filename The name of the file to upload.
* @param mimeType The MIME type of the file.
* @param attachment A Buffer containing the file to upload.
* @param width The width of the image.
* @param height The height of the image.
* @returns The ID of the attachment.
*/
async addAttachment(
post: Post,
filename: string,
mimeType: string,
attachment: Buffer,
width: number,
height: number,
): Promise<number>;
async addAttachment(
post: Post,
filepathOrName: string,
mimeType?: string,
attachment?: Buffer,
width?: number,
height?: number,
): Promise<number> {
if (!post.postId) {
throw new Error(
"Post must first be published or drafted before an attachment can be added.",
);
}
if (!mimeType || !attachment || !width || !height) {
attachment = fs.readFileSync(filepathOrName);
let type = mimeType || mime.getType(filepathOrName);
if (!type) {
throw new Error("Could not determine MIME type of file.");
}
mimeType = type;
filepathOrName = path.basename(filepathOrName);
let dimensions = imageSize.imageSize(attachment);
width = dimensions.width;
height = dimensions.height;
}
let response = await this.client.trpc.posts.attachment.start.mutate({
projectHandle: this.handle,
postId: post.postId,
filename: filepathOrName,
contentType: mimeType,
contentLength: attachment.length,
width,
height,
});
// Upload to S3 bucket
const formData = new FormData();
for (let key in response.requiredFields) {
formData.append(key, response.requiredFields[key]);
}
formData.append("file", new Blob([attachment]), filepathOrName);
await fetch(response.url, {
method: "POST",
body: formData,
});
let finishResponse = await this.client.trpc.posts.attachment.finish.mutate({
projectHandle: this.handle,
postId: post.postId,
attachmentId: response.attachmentId,
});
await this.client.trpc.posts.update.mutate({
projectHandle: this.handle,
postId: post.postId,
content: {
...post.content,
blocks: [
...post.content.blocks,
{
type: "attachment",
attachment: {
attachmentId: finishResponse.attachmentId,
altText: "",
previewURL: finishResponse.url,
fileURL: finishResponse.url,
kind: "image",
width,
height,
},
},
],
},
cws: post.content.cws,
tags: post.content.tags,
});
return response.attachmentId;
}
/**
* Gives a like from this project to a post.
* @param post The post to like.
*/
async likePost(post: Post | number) {
let postId = post instanceof Post ? post.postId : post;
await this.client.trpc.relationships.like.mutate({
fromProjectId: this.id,
toPostId: postId,
});
}
/**
* Removes a like from this project to a post.
* @param post The post to unlike.
*/
async unlikePost(post: Post | number) {
let postId = post instanceof Post ? post.postId : post;
await this.client.trpc.relationships.unlike.mutate({
fromProjectId: this.id,
toPostId: postId,
});
}
}
export { EditedProject, Project, Privacy, SortOrder };