-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Expand file tree
/
Copy pathusers.ts
More file actions
390 lines (353 loc) · 11.6 KB
/
users.ts
File metadata and controls
390 lines (353 loc) · 11.6 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
import { z, ZodEffects, ZodOptional, ZodString } from "zod";
import { IdSchema, nameWithSeparators, slug, StringNumberSchema } from "./util";
import { LanguageSchema } from "./languages";
import {
ModeSchema,
Mode2Schema,
PersonalBestsSchema,
DefaultWordsModeSchema,
DefaultTimeModeSchema,
QuoteLengthSchema,
DifficultySchema,
PersonalBestSchema,
} from "./shared";
import { CustomThemeColorsSchema, FunboxNameSchema } from "./configs";
import { doesNotContainDisallowedWords } from "./validation/validation";
import { ConnectionSchema } from "./connections";
const NoneFilterSchema = z.literal("none");
export const ResultFiltersSchema = z.object({
_id: IdSchema,
name: slug().max(16),
pb: z
.object({
no: z.boolean(),
yes: z.boolean(),
})
.strict(),
difficulty: z.record(DifficultySchema, z.boolean()),
mode: z.record(ModeSchema, z.boolean()),
words: z.record(DefaultWordsModeSchema.or(z.literal("custom")), z.boolean()),
time: z.record(DefaultTimeModeSchema.or(z.literal("custom")), z.boolean()),
quoteLength: z.record(QuoteLengthSchema, z.boolean()),
punctuation: z
.object({
on: z.boolean(),
off: z.boolean(),
})
.strict(),
numbers: z
.object({
on: z.boolean(),
off: z.boolean(),
})
.strict(),
date: z
.object({
last_day: z.boolean(),
last_week: z.boolean(),
last_month: z.boolean(),
last_3months: z.boolean(),
all: z.boolean(),
})
.strict(),
tags: z.record(IdSchema.or(NoneFilterSchema), z.boolean()),
language: z.record(LanguageSchema, z.boolean()),
funbox: z.record(FunboxNameSchema.or(NoneFilterSchema), z.boolean()),
});
export type ResultFilters = z.infer<typeof ResultFiltersSchema>;
export const StreakHourOffsetSchema = z.number().min(-11).max(12).step(0.5);
export type StreakHourOffset = z.infer<typeof StreakHourOffsetSchema>;
export const UserStreakSchema = z
.object({
lastResultTimestamp: z.number().int().nonnegative(),
length: z.number().int().nonnegative(),
maxLength: z.number().int().nonnegative(),
hourOffset: StreakHourOffsetSchema.optional(),
})
.strict();
export type UserStreak = z.infer<typeof UserStreakSchema>;
export const TagNameSchema = nameWithSeparators().max(16);
export type TagName = z.infer<typeof TagNameSchema>;
export const UserTagSchema = z
.object({
_id: IdSchema,
name: TagNameSchema,
personalBests: PersonalBestsSchema,
})
.strict();
export type UserTag = z.infer<typeof UserTagSchema>;
function profileDetailsBase(
schema: ZodString,
): ZodEffects<ZodOptional<ZodEffects<ZodString>>> {
return doesNotContainDisallowedWords("word", schema)
.optional()
.transform((value) => (value === null ? undefined : value));
}
export const TwitterProfileSchema = profileDetailsBase(slug().max(20)).or(
z.literal(""),
);
export const GithubProfileSchema = profileDetailsBase(slug().max(39)).or(
z.literal(""),
);
export const WebsiteSchema = profileDetailsBase(
z.string().url().max(200).startsWith("https://"),
).or(z.literal(""));
export const UserProfileDetailsSchema = z
.object({
bio: profileDetailsBase(z.string().max(250)).or(z.literal("")),
keyboard: profileDetailsBase(z.string().max(75)).or(z.literal("")),
socialProfiles: z
.object({
twitter: TwitterProfileSchema,
github: GithubProfileSchema,
website: WebsiteSchema,
})
.strict()
.optional(),
showActivityOnPublicProfile: z.boolean().optional(),
})
.strict();
export type UserProfileDetails = z.infer<typeof UserProfileDetailsSchema>;
export const CustomThemeNameSchema = nameWithSeparators().max(16);
export type CustomThemeName = z.infer<typeof CustomThemeNameSchema>;
export const CustomThemeSchema = z
.object({
_id: IdSchema,
name: CustomThemeNameSchema,
colors: CustomThemeColorsSchema,
})
.strict();
export type CustomTheme = z.infer<typeof CustomThemeSchema>;
export const PremiumInfoSchema = z.object({
startTimestamp: z.number().int().nonnegative(),
expirationTimestamp: z
.number()
.int()
.nonnegative()
.or(z.literal(-1).describe("lifetime premium")),
});
export type PremiumInfo = z.infer<typeof PremiumInfoSchema>;
export const UserQuoteRatingsSchema = z.record(
LanguageSchema,
z.record(
StringNumberSchema.describe("quoteId as string"),
z.number().nonnegative(),
),
);
export type UserQuoteRatings = z.infer<typeof UserQuoteRatingsSchema>;
export const UserLbMemorySchema = z.record(
ModeSchema,
z.record(
Mode2Schema,
z.record(LanguageSchema, z.number().int().nonnegative()),
),
);
export type UserLbMemory = z.infer<typeof UserLbMemorySchema>;
export const RankAndCountSchema = z.object({
rank: z.number().int().nonnegative().optional(),
count: z.number().int().nonnegative(),
});
export type RankAndCount = z.infer<typeof RankAndCountSchema>;
export const AllTimeLbsSchema = z.object({
time: z.record(
Mode2Schema,
z.record(LanguageSchema, RankAndCountSchema.optional()),
),
});
export type AllTimeLbs = z.infer<typeof AllTimeLbsSchema>;
export const BadgeSchema = z
.object({
id: z.number().int().nonnegative(),
selected: z.boolean().optional(),
})
.strict();
export type Badge = z.infer<typeof BadgeSchema>;
export const UserInventorySchema = z
.object({
badges: z.array(BadgeSchema),
})
.strict();
export type UserInventory = z.infer<typeof UserInventorySchema>;
export const QuoteModSchema = z
.boolean()
.describe("Admin for all languages if true")
.or(LanguageSchema.describe("Admin for the given language"));
export type QuoteMod = z.infer<typeof QuoteModSchema>;
export const TestActivitySchema = z
.object({
testsByDays: z
.array(z.number().int().nonnegative().or(z.null()))
.describe(
"Number of tests by day. Last element of the array is on the date `lastDay`. `null` means no tests on that day.",
),
lastDay: z
.number()
.int()
.nonnegative()
.describe("Timestamp of the last day included in the test activity"),
})
.strict();
export type TestActivity = z.infer<typeof TestActivitySchema>;
export const CountByYearAndDaySchema = z.record(
StringNumberSchema.describe("year"),
z.array(
z
.number()
.int()
.nonnegative()
.nullable()
.describe(
"number of tests, position in the array is the day of the year",
),
),
);
export type CountByYearAndDay = z.infer<typeof CountByYearAndDaySchema>;
//Record<language, array with quoteIds as string
export const FavoriteQuotesSchema = z.record(
LanguageSchema,
z.array(StringNumberSchema),
);
export type FavoriteQuotes = z.infer<typeof FavoriteQuotesSchema>;
export const UserEmailSchema = z.string().email();
export const UserNameSchema = doesNotContainDisallowedWords(
"substring",
slug().min(1).max(16),
);
export const UserSchema = z.object({
name: UserNameSchema,
email: UserEmailSchema,
uid: z.string(), //defined by firebase, no validation should be applied
addedAt: z.number().int().nonnegative(),
personalBests: PersonalBestsSchema,
lastResultHashes: z.array(z.string()).optional(),
lastReultHashes: z.array(z.string()).optional(), //legacy typo, kept for backwards compatibility
completedTests: z.number().int().nonnegative().optional(),
startedTests: z.number().int().nonnegative().optional(),
timeTyping: z
.number()
.nonnegative()
.optional()
.describe("time typing in seconds"),
streak: UserStreakSchema.optional(),
xp: z.number().int().nonnegative().optional(),
discordId: z.string().optional(),
discordAvatar: z.string().optional(),
tags: z.array(UserTagSchema).optional(),
profileDetails: UserProfileDetailsSchema.optional(),
customThemes: z.array(CustomThemeSchema).optional(),
premium: PremiumInfoSchema.optional(),
isPremium: z.boolean().optional(),
quoteRatings: UserQuoteRatingsSchema.optional(),
favoriteQuotes: FavoriteQuotesSchema.optional(),
lbMemory: UserLbMemorySchema.optional(),
allTimeLbs: AllTimeLbsSchema,
inventory: UserInventorySchema.optional(),
banned: z.boolean().optional(),
lbOptOut: z.boolean().optional(),
verified: z.boolean().optional(),
needsToChangeName: z.boolean().optional(),
quoteMod: QuoteModSchema.optional(),
resultFilterPresets: z.array(ResultFiltersSchema).optional(),
testActivity: TestActivitySchema.optional(),
});
export type User = z.infer<typeof UserSchema>;
export type ResultFiltersGroup = keyof ResultFilters;
export type ResultFiltersGroupItem<T extends ResultFiltersGroup> =
keyof ResultFilters[T];
export const TypingStatsSchema = z.object({
completedTests: z.number().int().nonnegative().optional(),
startedTests: z.number().int().nonnegative().optional(),
timeTyping: z.number().int().nonnegative().optional(),
});
export type TypingStats = z.infer<typeof TypingStatsSchema>;
export const UserProfileSchema = UserSchema.pick({
uid: true,
name: true,
banned: true,
addedAt: true,
discordId: true,
discordAvatar: true,
xp: true,
lbOptOut: true,
isPremium: true,
inventory: true,
allTimeLbs: true,
testActivity: true,
})
.extend({
typingStats: TypingStatsSchema,
personalBests: PersonalBestsSchema.pick({ time: true, words: true }),
streak: z.number().int().nonnegative(),
maxStreak: z.number().int().nonnegative(),
details: UserProfileDetailsSchema,
})
.partial({
//omitted for banned users
inventory: true,
details: true,
allTimeLbs: true,
uid: true,
});
export type UserProfile = z.infer<typeof UserProfileSchema>;
export const RewardTypeSchema = z.enum(["xp", "badge"]);
export type RewardType = z.infer<typeof RewardTypeSchema>;
export const XpRewardSchema = z.object({
type: z.literal(RewardTypeSchema.enum.xp),
item: z.number().int(),
});
export type XpReward = z.infer<typeof XpRewardSchema>;
export const BadgeRewardSchema = z.object({
type: z.literal(RewardTypeSchema.enum.badge),
item: BadgeSchema,
});
export type BadgeReward = z.infer<typeof BadgeRewardSchema>;
export const AllRewardsSchema = XpRewardSchema.or(BadgeRewardSchema);
export type AllRewards = z.infer<typeof AllRewardsSchema>;
export const MonkeyMailSchema = z.object({
id: IdSchema,
subject: z.string(),
body: z.string(),
timestamp: z.number().int().nonnegative(),
read: z.boolean(),
rewards: z.array(AllRewardsSchema),
});
export type MonkeyMail = z.infer<typeof MonkeyMailSchema>;
export const ReportUserReasonSchema = z.enum([
"Inappropriate name",
"Inappropriate bio",
"Inappropriate social links",
"Suspected cheating",
]);
export type ReportUserReason = z.infer<typeof ReportUserReasonSchema>;
export const PasswordSchema = z
.string()
.min(8, { message: "must be at least 8 characters" })
.max(64, { message: "must be at most 64 characters" })
.regex(/[A-Z]/, { message: "must contain at least one capital letter" })
.regex(/[\d]/, { message: "must contain at least one number" })
.regex(/[!@#$%^&*()_+\-=[\]{};':"\\|,.<>/?]/, {
message: "must contain at least one special character",
});
export type Password = z.infer<typeof PasswordSchema>;
export const FriendSchema = UserSchema.pick({
uid: true,
name: true,
discordId: true,
discordAvatar: true,
startedTests: true,
completedTests: true,
timeTyping: true,
xp: true,
banned: true,
lbOptOut: true,
})
.extend({
connectionId: IdSchema.optional(),
top15: PersonalBestSchema.optional(),
top60: PersonalBestSchema.optional(),
badgeId: z.number().int().optional(),
isPremium: z.boolean().optional(),
streak: UserStreakSchema.pick({ length: true, maxLength: true }),
})
.merge(ConnectionSchema.pick({ lastModified: true }).partial());
export type Friend = z.infer<typeof FriendSchema>;