forked from stoatchat/javascript-client-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.ts
More file actions
328 lines (287 loc) · 7.36 KB
/
Copy pathUser.ts
File metadata and controls
328 lines (287 loc) · 7.36 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
import type { User as APIUser, DataEditUser, Presence } from "stoat-api";
import { decodeTime } from "ulid";
import type { UserCollection } from "../collections/UserCollection.js";
import { U32_MAX, UserPermission } from "../permissions/definitions.js";
import type { Channel } from "./Channel.js";
import type { File } from "./File.js";
import { UserProfile } from "./UserProfile.js";
/**
* User Class
*/
export class User {
readonly #collection: UserCollection;
readonly id: string;
/**
* Construct User
* @param collection Collection
* @param id Id
*/
constructor(collection: UserCollection, id: string) {
this.#collection = collection;
this.id = id;
}
/**
* Write to string as a user mention
* @returns Formatted String
*/
toString(): string {
return `<@${this.id}>`;
}
/**
* Whether this object exists
*/
get $exists(): boolean {
return !!this.#collection.getUnderlyingObject(this.id).id;
}
/**
* Time when this user created their account
*/
get createdAt(): Date {
return new Date(decodeTime(this.id));
}
/**
* Username
*/
get username(): string {
return this.#collection.getUnderlyingObject(this.id).username;
}
/**
* Discriminator
*/
get discriminator(): string {
return this.#collection.getUnderlyingObject(this.id).discriminator;
}
/**
* Display Name
*/
get displayName(): string {
return (
this.#collection.getUnderlyingObject(this.id).displayName ??
this.#collection.getUnderlyingObject(this.id).username
);
}
/**
* Avatar
*/
get avatar(): File | undefined {
return this.#collection.getUnderlyingObject(this.id).avatar;
}
/**
* Badges
*/
get badges(): number {
return this.#collection.getUnderlyingObject(this.id).badges;
}
/**
* User Status
*/
get status():
| { text?: string | null; presence?: Presence | null }
| undefined {
// TODO: issue with API, upstream fix required #319
if (!this.online)
return { text: undefined, presence: "Invisible" as const };
return this.#collection.getUnderlyingObject(this.id).status;
}
/**
* Relationship with user
*/
get relationship(): string {
return this.#collection.getUnderlyingObject(this.id).relationship;
}
/**
* Whether the user is online
*/
get online(): boolean {
return this.#collection.getUnderlyingObject(this.id).online;
}
/**
* Whether the user is privileged
*/
get privileged(): boolean {
return this.#collection.getUnderlyingObject(this.id).privileged;
}
/**
* Flags
*/
get flags(): number {
return this.#collection.getUnderlyingObject(this.id).flags;
}
/**
* Bot information
*/
get bot(): { owner: string } | undefined {
return this.#collection.getUnderlyingObject(this.id).bot;
}
/**
* Whether this user is ourselves
*/
get self(): boolean {
return this.#collection.client.user === this;
}
/**
* URL to the user's default avatar
*/
get defaultAvatarURL(): string {
return `${this.#collection.client.options.baseURL}/users/${
this.id
}/default_avatar`;
}
/**
* URL to the user's avatar
*/
get avatarURL(): string {
return this.avatar?.createFileURL() ?? this.defaultAvatarURL;
}
/**
* URL to the user's animated avatar
*/
get animatedAvatarURL(): string {
return this.avatar?.createFileURL(true) ?? this.defaultAvatarURL;
}
/**
* Presence
*/
get presence(): Presence {
return this.online ? (this.status?.presence ?? "Online") : "Invisible";
}
/**
* Generate status message
* @param translate Translation function
* @returns Status message
*/
statusMessage(
translate: (presence: Presence) => string = (a) => a,
): string | undefined {
return this.online
? (this.status?.text ??
(this.presence === "Focus" ? translate("Focus") : undefined))
: undefined;
}
/**
* Permissions against this user
*/
get permission(): number {
let permissions = 0;
switch (this.relationship) {
case "Friend":
case "User":
return U32_MAX;
case "Blocked":
case "BlockedOther":
return UserPermission.Access;
case "Incoming":
case "Outgoing":
permissions = UserPermission.Access;
}
if (
this.#collection.client.channels.find(
(channel) =>
(channel.type === "Group" || channel.type === "DirectMessage") &&
channel.recipientIds.has(this.id),
) ||
this.#collection.client.serverMembers.find(
(member) => member.id.user === this.id,
)
) {
if (this.#collection.client.user?.bot || this.bot) {
permissions |= UserPermission.SendMessage;
}
permissions |= UserPermission.Access | UserPermission.ViewProfile;
}
return permissions;
}
/**
* Edit the user
* @param data Changes
*/
async edit(data: DataEditUser): Promise<void> {
await this.#collection.client.api.patch(
`/users/${
this.id === this.#collection.client.user?.id ? "@me" : this.id
}`,
data,
);
}
/**
* Change the username of the current user
* @param username New username
* @param password Current password
*/
async changeUsername(username: string, password: string): Promise<APIUser> {
return await this.#collection.client.api.patch("/users/@me/username", {
username,
password,
});
}
/**
* Open a DM with a user
* @returns DM Channel
*/
async openDM(): Promise<Channel> {
let dm = [...this.#collection.client.channels.values()].find(
(x) => x.type === "DirectMessage" && x.recipient == this,
);
if (dm) {
if (!dm.active) {
this.#collection.client.channels.updateUnderlyingObject(
dm.id,
"active",
true,
);
}
} else {
const data = await this.#collection.client.api.get(
`/users/${this.id as ""}/dm`,
);
dm = this.#collection.client.channels.getOrCreate(data._id, data)!;
}
return dm;
}
/**
* Send a friend request to a user
*/
async addFriend(): Promise<User> {
const user = await this.#collection.client.api.post(`/users/friend`, {
username: this.username + "#" + this.discriminator,
});
return this.#collection.getOrCreate(user._id, user);
}
/**
* Remove a user from the friend list
*/
async removeFriend(): Promise<void> {
await this.#collection.client.api.delete(`/users/${this.id as ""}/friend`);
}
/**
* Block a user
*/
async blockUser(): Promise<void> {
await this.#collection.client.api.put(`/users/${this.id as ""}/block`);
}
/**
* Unblock a user
*/
async unblockUser(): Promise<void> {
await this.#collection.client.api.delete(`/users/${this.id as ""}/block`);
}
/**
* Fetch the profile of a user
* @returns The profile of the user
*/
async fetchProfile(): Promise<UserProfile> {
return new UserProfile(
this.#collection.client,
await this.#collection.client.api.get(`/users/${this.id as ""}/profile`),
);
}
/**
* Fetch the mutual connections of the current user and a target user
* @returns The mutual connections of the current user and a target user
*/
async fetchMutual(): Promise<{ users: string[]; servers: string[] }> {
return await this.#collection.client.api.get(
`/users/${this.id as ""}/mutual`,
);
}
}