summary refs log tree commit diff
path: root/src/util/entities/User.ts
blob: 79ea6c3cd31ac3fb01f3d15037282308cf06a9ca (plain) (blame)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
import {
	BeforeInsert,
	BeforeUpdate,
	Column,
	Entity,
	FindOneOptions,
	JoinColumn,
	OneToMany,
	OneToOne,
} from "typeorm";
import { BaseClass } from "./BaseClass";
import { BitField } from "../util/BitField";
import { Relationship } from "./Relationship";
import { ConnectedAccount } from "./ConnectedAccount";
import { Member } from "./Member";
import { UserSettings } from "./UserSettings";
import { Session } from "./Session";
import {
	Config,
	FieldErrors,
	Snowflake,
	trimSpecial,
	adjustEmail,
} from "..";

export enum PublicUserEnum {
	username,
	discriminator,
	id,
	public_flags,
	avatar,
	accent_color,
	banner,
	bio,
	bot,
	premium_since,
}
export type PublicUserKeys = keyof typeof PublicUserEnum;

export enum PrivateUserEnum {
	flags,
	mfa_enabled,
	email,
	phone,
	verified,
	nsfw_allowed,
	premium,
	premium_type,
	purchased_flags,
	premium_usage_flags,
	disabled,
	// settings,	// now a relation
	// locale
}
export type PrivateUserKeys = keyof typeof PrivateUserEnum | PublicUserKeys;

export const PublicUserProjection = Object.values(PublicUserEnum).filter(
	(x) => typeof x === "string",
) as PublicUserKeys[];
export const PrivateUserProjection = [
	...PublicUserProjection,
	...Object.values(PrivateUserEnum).filter((x) => typeof x === "string"),
] as PrivateUserKeys[];

// Private user data that should never get sent to the client
export type PublicUser = Pick<User, PublicUserKeys>;

export interface UserPublic extends Pick<User, PublicUserKeys> { }

export interface UserPrivate extends Pick<User, PrivateUserKeys> {
	locale: string;
}

@Entity("users")
export class User extends BaseClass {
	@Column()
	username: string; // username max length 32, min 2 (should be configurable)

	@Column()
	discriminator: string; // opaque string: 4 digits on discord.com

	@Column({ nullable: true })
	avatar?: string; // hash of the user avatar

	@Column({ nullable: true })
	accent_color?: number; // banner color of user

	@Column({ nullable: true })
	banner?: string; // hash of the user banner

	@Column({ nullable: true, select: false })
	phone?: string; // phone number of the user

	@Column({ select: false })
	desktop: boolean = false; // if the user has desktop app installed

	@Column({ select: false })
	mobile: boolean = false; // if the user has mobile app installed

	@Column()
	premium: boolean = false; // if user bought individual premium

	@Column()
	premium_type: number = 0; // individual premium level

	@Column()
	bot: boolean = false; // if user is bot

	@Column()
	bio: string = ""; // short description of the user (max 190 chars -> should be configurable)

	@Column()
	system: boolean = false; // shouldn't be used, the api sends this field type true, if the generated message comes from a system generated author

	@Column({ select: false })
	nsfw_allowed: boolean = true; // if the user can do age-restricted actions (NSFW channels/guilds/commands) // TODO: depending on age

	@Column({ select: false })
	mfa_enabled: boolean = false; // if multi factor authentication is enabled

	@Column({ select: false, nullable: true })
	totp_secret?: string = "";

	@Column({ nullable: true, select: false })
	totp_last_ticket?: string = "";

	@Column()
	created_at: Date = new Date(); // registration date

	@Column({ nullable: true })
	premium_since: Date = new Date(); // premium date

	@Column({ select: false })
	verified: boolean = true;	// email is verified

	@Column()
	disabled: boolean = false; // if the account is disabled

	@Column()
	deleted: boolean = false; // if the user was deleted

	@Column({ nullable: true, select: false })
	email?: string; // email of the user

	@Column()
	flags: string = "0"; // UserFlags // TODO: generate

	@Column()
	public_flags: number = 0;

	@Column()
	purchased_flags: number = 0;

	@Column()
	premium_usage_flags: number = 0;

	@Column({ type: "bigint" })
	rights: string; // Rights

	@OneToMany(() => Session, (session: Session) => session.user)
	sessions: Session[];

	@JoinColumn({ name: "relationship_ids" })
	@OneToMany(
		() => Relationship,
		(relationship: Relationship) => relationship.from,
		{
			cascade: true,
			orphanedRowAction: "delete",
		},
	)
	relationships: Relationship[];

	@JoinColumn({ name: "connected_account_ids" })
	@OneToMany(
		() => ConnectedAccount,
		(account: ConnectedAccount) => account.user,
		{
			cascade: true,
			orphanedRowAction: "delete",
		},
	)
	connected_accounts: ConnectedAccount[];

	@Column({ type: "simple-json", select: false })
	data: {
		valid_tokens_since: Date; // all tokens with a previous issue date are invalid
		hash?: string; // hash of the password, salt is saved in password (bcrypt)
	};

	@Column({ type: "simple-array", select: false })
	fingerprints: string[] = []; // array of fingerprints -> used to prevent multiple accounts

	@OneToOne(() => UserSettings, {
		cascade: true,
		orphanedRowAction: "delete",
		eager: false
	})
	@JoinColumn()
	settings: UserSettings;

	// workaround to prevent fossord-unaware clients from deleting settings not used by them
	@Column({ type: "simple-json", select: false })
	extended_settings: string = "{}";

	// TODO: I don't like this method?
	validate() {
		if (this.email) {
			this.email = adjustEmail(this.email);
			if (!this.email)
				throw FieldErrors({
					email: { message: "Invalid email", code: "EMAIL_INVALID" },
				});
			if (!this.email.match(/([a-z\d.-]{3,})@([a-z\d.-]+).([a-z]{2,})/g))
				throw FieldErrors({
					email: { message: "Invalid email", code: "EMAIL_INVALID" },
				});
		}

		if (this.discriminator) {
			const discrim = Number(this.discriminator);
			if (this.discriminator.length > 4)
				throw FieldErrors({
					discriminator: {
						message: "Discriminator cannot be more than 4 digits.",
						code: "DISCRIMINATOR_INVALID",
					},
				});
			if (isNaN(discrim))
				throw FieldErrors({
					discriminator: {
						message: "Discriminator must be a number.",
						code: "DISCRIMINATOR_INVALID",
					},
				});
			if (discrim <= 0 || discrim >= 10000)
				throw FieldErrors({
					discriminator: {
						message: "Discriminator must be a number.",
						code: "DISCRIMINATOR_INVALID",
					},
				});
			this.discriminator = discrim.toString().padStart(4, "0");
		}
	}

	toPublicUser() {
		const user: any = {};
		PublicUserProjection.forEach((x) => {
			user[x] = this[x];
		});
		return user as PublicUser;
	}

	static async getPublicUser(user_id: string, opts?: FindOneOptions<User>) {
		return await User.findOneOrFail({
			where: { id: user_id },
			...opts,
			//@ts-ignore
			select: [...PublicUserProjection, ...(opts?.select || [])], // TODO: fix
		});
	}

	public static async generateDiscriminator(username: string): Promise<string | undefined> {
		if (Config.get().register.incrementingDiscriminators) {
			// discriminator will be incrementally generated

			// First we need to figure out the currently highest discrimnator for the given username and then increment it
			const users = await User.find({
				where: { username },
				select: ["discriminator"],
			});
			const highestDiscriminator = Math.max(
				0,
				...users.map((u) => Number(u.discriminator)),
			);

			const discriminator = highestDiscriminator + 1;
			if (discriminator >= 10000) {
				return undefined;
			}

			return discriminator.toString().padStart(4, "0");
		} else {
			// discriminator will be randomly generated

			// randomly generates a discriminator between 1 and 9999 and checks max five times if it already exists
			// TODO: is there any better way to generate a random discriminator only once, without checking if it already exists in the database?
			for (let tries = 0; tries < 5; tries++) {
				const discriminator = Math.randomIntBetween(1, 9999)
					.toString()
					.padStart(4, "0");
				const exists = await User.findOne({
					where: { discriminator, username: username },
					select: ["id"],
				});
				if (!exists) return discriminator;
			}

			return undefined;
		}
	}

	static async register({
		email,
		username,
		password,
		date_of_birth,
		req,
	}: {
		username: string;
		password?: string;
		email?: string;
		date_of_birth?: Date; // "2000-04-03"
		req?: any;
	}) {
		// trim special uf8 control characters -> Backspace, Newline, ...
		username = trimSpecial(username);

		const discriminator = await User.generateDiscriminator(username);
		if (!discriminator) {
			// We've failed to generate a valid and unused discriminator
			throw FieldErrors({
				username: {
					code: "USERNAME_TOO_MANY_USERS",
					message: req.t("auth:register.USERNAME_TOO_MANY_USERS"),
				},
			});
		}

		// TODO: save date_of_birth
		// appearently discord doesn't save the date of birth and just calculate if nsfw is allowed
		// if nsfw_allowed is null/undefined it'll require date_of_birth to set it to true/false
		const language =
			req.language === "en" ? "en-US" : req.language || "en-US";

		const user = User.create({
			username: username,
			discriminator,
			id: Snowflake.generate(),
			email: email,
			rights: Config.get().register.defaultRights,
			data: {
				hash: password,
				valid_tokens_since: new Date(),
			},
			extended_settings: "{}",
			premium_type: Config.get().defaults.user.premium_type,
			premium: Config.get().defaults.user.premium,
			verified: Config.get().defaults.user.verified,
			settings: { ...new UserSettings(), locale: language }
		});

		user.validate();
		await user.save();
		await user.settings.save();

		setImmediate(async () => {
			if (Config.get().guild.autoJoin.enabled) {
				for (const guild of Config.get().guild.autoJoin.guilds || []) {
					await Member.addToGuild(user.id, guild).catch((e) => { });
				}
			}
		});

		return user;
	}
}

export const CUSTOM_USER_FLAG_OFFSET = BigInt(1) << BigInt(32);

export class UserFlags extends BitField {
	static FLAGS = {
		DISCORD_EMPLOYEE: BigInt(1) << BigInt(0),
		PARTNERED_SERVER_OWNER: BigInt(1) << BigInt(1),
		HYPESQUAD_EVENTS: BigInt(1) << BigInt(2),
		BUGHUNTER_LEVEL_1: BigInt(1) << BigInt(3),
		MFA_SMS: BigInt(1) << BigInt(4),
		PREMIUM_PROMO_DISMISSED: BigInt(1) << BigInt(5),
		HOUSE_BRAVERY: BigInt(1) << BigInt(6),
		HOUSE_BRILLIANCE: BigInt(1) << BigInt(7),
		HOUSE_BALANCE: BigInt(1) << BigInt(8),
		EARLY_SUPPORTER: BigInt(1) << BigInt(9),
		TEAM_USER: BigInt(1) << BigInt(10),
		TRUST_AND_SAFETY: BigInt(1) << BigInt(11),
		SYSTEM: BigInt(1) << BigInt(12),
		HAS_UNREAD_URGENT_MESSAGES: BigInt(1) << BigInt(13),
		BUGHUNTER_LEVEL_2: BigInt(1) << BigInt(14),
		UNDERAGE_DELETED: BigInt(1) << BigInt(15),
		VERIFIED_BOT: BigInt(1) << BigInt(16),
		EARLY_VERIFIED_BOT_DEVELOPER: BigInt(1) << BigInt(17),
		CERTIFIED_MODERATOR: BigInt(1) << BigInt(18),
		BOT_HTTP_INTERACTIONS: BigInt(1) << BigInt(19),
	};
}