forked from AHSFNU/syzoj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.ts
333 lines (265 loc) · 8.73 KB
/
user.ts
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
import * as TypeORM from "typeorm";
import Model from "./common";
declare var syzoj, ErrorMessage: any;
import JudgeState from "./judge_state";
import UserPrivilege from "./user_privilege";
import Article from "./article";
import TodoList from "./todo-list";
import UserRestriction from "./user-restriction";
import UserIdentity from "./user-identity";
@TypeORM.Entity()
export default class User extends Model {
static cache = true;
@TypeORM.PrimaryGeneratedColumn()
id: number;
@TypeORM.Index({ unique: true })
@TypeORM.Column({ nullable: true, type: "varchar", length: 80 })
username: string;
@TypeORM.Column({ nullable: true, type: "varchar", length: 120 })
email: string;
@TypeORM.Column({ nullable: true, type: "varchar", length: 120 })
password: string;
@TypeORM.Column({ nullable: true, type: "varchar", length: 80 })
nickname: string;
@TypeORM.Column({ nullable: true, type: "text" })
nameplate: string;
@TypeORM.Column({ nullable: true, type: "text" })
information: string;
@TypeORM.Index()
@TypeORM.Column({ nullable: true, type: "integer" })
ac_num: number;
@TypeORM.Index()
@TypeORM.Column({ nullable: true, type: "integer" })
submit_num: number;
@TypeORM.Column({ nullable: true, type: "boolean" })
is_admin: boolean;
@TypeORM.Column({ nullable: true, type: "boolean" })
is_cheater: boolean;
@TypeORM.Column({ nullable: true, type: "boolean" })
banned: boolean;
@TypeORM.Index()
@TypeORM.Column({ nullable: true, type: "boolean" })
is_show: boolean;
@TypeORM.Index()
@TypeORM.Column({ nullable: true, type: "boolean" })
is_rated: boolean;
@TypeORM.Column({ nullable: true, type: "boolean", default: false })
is_verified: boolean;
@TypeORM.Column({ nullable: true, type: "boolean", default: true })
public_email: boolean;
@TypeORM.Column({ nullable: true, type: "boolean", default: true })
prefer_formatted_code: boolean;
@TypeORM.Column({ nullable: true, type: "boolean", default: true })
auto_spacing_page: boolean;
@TypeORM.Column({ nullable: true, type: "boolean", default: true })
can_see_quote: boolean;
@TypeORM.Column({ nullable: true, type: "integer" })
sex: number;
@TypeORM.Column({ nullable: true, type: "integer" })
rating: number;
@TypeORM.Column({ nullable: true, type: "integer" })
register_time: number;
@TypeORM.Column({ type: "integer", default: 0 })
last_sigin: number;
@TypeORM.Column({ type: "integer", default: 0 })
con_tot: number;
privilege_cache?: Map<string, boolean>;
static async fromEmail(email): Promise<User> {
return User.findOne({
where: {
email: String(email)
}
});
}
static async fromName(name): Promise<User> {
return User.findOne({
where: {
username: String(name)
}
});
}
async updateSignIn() {
console.log("cur_date:", syzoj.utils.getCurrentDate(true));
const now = syzoj.utils.getCurrentDate(true);
console.log(now);
const timeDiff = Math.abs(now - this.last_sigin);
let diffDays = Math.ceil(timeDiff / (60 * 60 * 24));
if (diffDays == 0) return;
if (diffDays > 20) diffDays = 20;
const reduce = diffDays == 1 ? 0 : ((1 << (diffDays - 1)) - 1);
this.con_tot -= reduce;
if (this.con_tot < 0) this.con_tot = 0;
this.con_tot++;
this.last_sigin = now;
await this.save();
}
canSignIn() {
const now = syzoj.utils.getCurrentDate(true);
const timeDiff = Math.abs(now - this.last_sigin);
const diffDays = Math.ceil(timeDiff / (60 * 60 * 24));
return diffDays > 0;
}
async isAllowedEditBy(user) {
if (!user) return false;
if (await user.hasPrivilege('manage_user')) return true;
return user && (user.is_admin || this.id === user.id);
}
getQueryBuilderForACProblems() {
return JudgeState.createQueryBuilder()
.select(`DISTINCT(problem_id)`)
.where('user_id = :user_id', { user_id: this.id })
.andWhere('status = :status', { status: 'Accepted' })
.andWhere('type != 1')
.orderBy({ problem_id: 'ASC' })
}
toJSON() {
return {
user_id: this.id,
nameplate: this.nameplate,
username: this.username
}
}
async refreshSubmitInfo() {
await syzoj.utils.lock(['User::refreshSubmitInfo', this.id], async () => {
this.ac_num = await JudgeState.countQuery(this.getQueryBuilderForACProblems());
this.submit_num = await JudgeState.count({
user_id: this.id,
type: TypeORM.Not(1) // Not a contest submission
});
await this.save();
});
}
async getACProblems() {
let queryResult = await this.getQueryBuilderForACProblems().getRawMany();
return queryResult.map(record => record['problem_id'])
}
async getArticles() {
return await Article.find({
where: {
user_id: this.id
}
});
}
async getStatistics() {
let statuses = {
"Accepted": ["Accepted"],
"Wrong Answer": ["Wrong Answer", "File Error", "Output Limit Exceeded"],
"Runtime Error": ["Runtime Error"],
"Time Limit Exceeded": ["Time Limit Exceeded"],
"Memory Limit Exceeded": ["Memory Limit Exceeded"],
"Compile Error": ["Compile Error"]
};
let res = {};
for (let status in statuses) {
res[status] = 0;
for (let s of statuses[status]) {
res[status] += await JudgeState.count({
user_id: this.id,
type: 0,
status: s
});
}
}
return res;
}
async getTodoList(): Promise<number[]> {
let todoList = await TodoList.find({
where: { user_id: this.id }
});
return todoList.map((item) => item.problem_id);
}
async renderInformation() {
this.information = await syzoj.utils.markdown(this.information);
}
ensurePrivilegeCache(): Map<string, boolean> {
return this.privilege_cache = this.privilege_cache || new Map();
}
async getPrivileges(): Promise<string[]> {
let privileges = await UserPrivilege.find({
where: {
user_id: this.id
}
});
const results = privileges.map(x => x.privilege);
this.privilege_cache = new Map(results.map(privilege => [privilege, true]));
return results;
}
async setPrivileges(newPrivileges: string[]) {
let oldPrivileges = await this.getPrivileges();
let delPrivileges = oldPrivileges.filter(x => !newPrivileges.includes(x));
let addPrivileges = newPrivileges.filter(x => !oldPrivileges.includes(x));
for (let privilege of delPrivileges) {
let obj = await UserPrivilege.findOne({ where: {
user_id: this.id,
privilege: privilege
} });
await obj.destroy();
this.privilege_cache.set(privilege, false);
}
for (let privilege of addPrivileges) {
let obj = await UserPrivilege.create({
user_id: this.id,
privilege: privilege
});
await obj.save();
this.privilege_cache.set(privilege, true);
}
}
async hasPrivilege(privilege): Promise<boolean> {
if (this.is_admin) {
return true;
}
if (this.ensurePrivilegeCache().has(privilege)) return this.privilege_cache.get(privilege);
const x = await UserPrivilege.findOne({ where: { user_id: this.id, privilege: privilege } });
const result = !!x;
this.privilege_cache.set(privilege, result);
return result;
}
async isRestricted(restriction: string): Promise<boolean> {
if (this.is_admin) return false;
const item = await UserRestriction.findOne({
where: { user_id: this.id, restriction }
});
return !!item;
}
async checkRestricted(restriction: string, messageTemplate?: string): Promise<void> {
if (this.is_admin) return;
const item = await UserRestriction.findOne({
where: { user_id: this.id, restriction }
});
if (item) {
messageTemplate = messageTemplate || '您被禁止使用此功能,原因:{reason}';
const message = messageTemplate.replace('{reason}', item.reason);
throw new ErrorMessage(message);
}
}
async getLastSubmitLanguage() {
let a = await JudgeState.findOne({
where: {
user_id: this.id
},
order: {
submit_time: 'DESC'
}
});
if (a) return a.language;
return null;
}
async getIdentity(createIfNotExist = false): Promise<UserIdentity> {
let identity = await UserIdentity.findOne({ user_id: this.id });
if (!identity && createIfNotExist) {
identity = UserIdentity.create({
user_id: this.id,
status: null,
creation_time: new Date()
});
}
if (identity) identity.user = this;
return identity;
}
async updateVerifyStatus(is_verified: boolean): Promise<void> {
if (is_verified === this.is_verified) return;
this.is_verified = is_verified;
await this.save();
}
}