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 pathuser.ts
More file actions
101 lines (90 loc) · 3.31 KB
/
user.ts
File metadata and controls
101 lines (90 loc) · 3.31 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
import { EditedProject, Project, SortOrder } from "./project.js";
import { Post } from "./post.js";
import { Client } from "./client.js";
/**
* The currently logged in user. Do not instantiate this class; instead, use the `login()` method of the Client class to log in.
*
* This class is not used for any other users because the Cohost API does not provide a way to get other users; it only provides other users' projects.
*/
class User {
/* @hidden */
private client: Client;
/* The projects that this user is able to edit. */
projects: EditedProject[] = [];
/* The unique ID of this user. */
id: number;
/* The email address of this user. */
email: string;
/* @hidden */
constructor(client: Client, id: number, email: string) {
this.client = client;
this.id = id;
this.email = email;
}
/**
* Switches the currently active project to the given project. This is relevant for API calls related to notifications and getting the "liked" state of posts.
* @param project The project to switch to.
*/
switchProject(project: EditedProject) {
this.client.trpc.projects.switchProject.mutate({ projectId: project.id });
}
/**
* Checks if the current project has liked a post. Use `switchProject()` to change the current project.
* @param post The post to check if the project has liked.
* @returns Whether the project has liked the post.
*/
liked(post: Post | number) {
if (typeof post === "number") {
return this.client.trpc.posts.isLiked.query({ postId: post });
} else {
return this.client.trpc.posts.isLiked.query({ postId: post.postId });
}
}
/**
* Gets the projects that follow the current project. Use `switchProject()` to change the current project.
*
* This method is paginated, and the default offset is 0 and the default limit is 10.
* @param offset How many projects to skip.
* @param limit The maximum number of projects to get.
* @returns The followers of this project.
*/
async getFollowers(
offset: number = 0,
limit: number = 10,
): Promise<Project[]> {
let response = await this.client.fetch(
`https://cohost.org/api/v1/projects/followers?offset=${offset}&limit=${limit}`,
);
let followers = (await response.json()).projects ?? [];
return followers.map((project: any) => {
return new Project(this.client, project);
});
}
/**
* Gets the projects that the current project follows. Use `switchProject()` to change the current project.
*
* @param sortOrder What order to sort the projects in.
* @param offset How many projects to skip.
* @param limit The maximum number of projects to get.
* @param beforeTimestamp Not sure yet. Likely has to do with the last time the project posted.
* @returns The projects that this project follows.
*/
async getFollowing(
sortOrder: SortOrder,
offset: number = 0,
limit: number = 10,
beforeTimestamp: number = Date.now(),
): Promise<Project[]> {
// Why is this particular API so redundant? query.query? project.project?
let projects = await this.client.trpc.projects.followedFeed.query.query({
sortOrder,
cursor: offset,
limit,
beforeTimestamp,
});
return projects.projects.map((project: any) => {
return new Project(this.client, project.project);
});
}
}
export { User };