-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.js
238 lines (202 loc) · 5.83 KB
/
user.js
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
'use strict';
const fs = require('fs-extra');
const Path = require('path');
const logger = require('./logger');
const Lock = require('./lock');
const { fileExists, mkdirEx, rmdirEx } = require('./utility');
const usersLock = new Lock();
let userLock = [];
class User {
constructor(username, submitted = [], banned = false, creationTime = 0, ips = []) {
this.username = username;
this.submitted = submitted;
this.banned = banned;
this.creationTime = creationTime;
this.ips = ips;
}
get lock() {
let o = userLock.find((a) => a.username === this.username);
if (!o) {
o = {};
o.username = this.username;
o.lock = new Lock();
userLock.push(o);
}
return o.lock;
}
async ban() {
await this.lock.exec(async () => {
if (this.banned) return;
this.banned = true;
await saveUsers();
});
}
async pardon() {
await this.lock.exec(async () => {
if (!this.banned) return;
this.banned = false;
await saveUsers();
});
}
async forceLogout() {
await this.lock.exec(async () => {
this.creationTime = Date.now();
await saveUsers();
});
}
async cleanFiles() {
await this.lock.exec(async () => {
let dir = this.getDir();
if (await fileExists(dir)) {
try {
await rmdirEx(dir);
} catch (err) {
logger.error(err);
}
}
});
}
async clearSubmitted() {
await this.cleanFiles();
this.submitted = [];
await saveUsers();
}
async cleanup() {
await this.cleanFiles();
await this.lock.acquire();
let p = userLock.findIndex((a) => a.username === this.username);
if (p !== -1) userLock.splice(p, 1);
}
findSubmitted(problem) {
return this.submitted.find((a) => a.problem === problem);
}
getDir() {
return Path.resolve(global.options.save_root, this.username);
}
getCodeFileRelative({ problem, filename }, save_type = global.options.save_type) {
switch (save_type) {
case 'normal':
return filename;
case 'subfolder':
return Path.join(problem, filename);
default:
throw new Error("unknown save_path argument");
}
}
getCodeFile(...arg) {
return Path.join(this.getDir(), this.getCodeFileRelative(...arg));
}
async deleteCode(entry) {
await this.lock.exec(async () => {
await fs.unlink(this.getCodeFile(entry));
});
}
async writeCode(entry, code) {
await this.lock.exec(async () => {
let path = this.getCodeFile(entry);
await mkdirEx(path);
await fs.writeFile(path, code);
});
}
async saveCode({ problem, filename, language, code }) {
let entry = this.findSubmitted(problem);
if (entry) {
try {
await this.deleteCode(entry);
} catch (err) {
logger.error(err);
}
this.submitted.splice(this.submitted.indexOf(entry), 1);
}
entry = {
problem, filename, language, size: code.length
};
await this.writeCode(entry, code);
this.submitted.push(entry);
await saveUsers();
}
async getCode(problem) {
let entry = this.findSubmitted(problem);
if (!entry) return null;
let filename = this.getCodeFile(entry);
return await this.lock.exec(async () => {
return await fs.readFile(filename, 'utf-8');
});
}
async recordIp(ip) {
await this.lock.exec(async () => {
if (!this.ips) this.ips = [];
if (!this.ips.includes(ip)) {
this.ips.push(ip);
await saveUsers();
}
});
}
}
const userDataFile = Path.join(__dirname, 'users.json');
let users = loadUsers();
function loadUsers() {
let data = [];
try {
JSON.parse(fs.readFileSync(userDataFile, 'utf-8')).forEach(function(a) {
data.push(new User(a.username, a.submitted, a.banned, a.creationTime, a.ips));
});
} catch (err) {}
return data;
}
async function saveUsers() {
try {
await usersLock.exec(async () => {
await fs.writeFile(userDataFile, JSON.stringify(users), "utf-8");
});
} catch (err) {
logger.error(err);
}
}
function getUser(username) {
if (!username) return null;
username = username.toLowerCase();
return users.find((a) => a.username.toLowerCase() === username) || null;
}
async function createUser(username) {
if (getUser(username)) throw new Error("user already exists");
let user = new User(username, [], false, Date.now());
users.push(user);
await saveUsers();
return user;
}
async function deleteUser(username) {
let user = getUser(username);
if (!user) throw new Error("could not find user");
let p = users.indexOf(user);
try {
await user.cleanup();
} catch (err) {
logger.error(err);
}
users.splice(p, 1);
await saveUsers();
}
async function deleteAllUsers() {
for (let i = 0; i < users.length; ++i) {
await users[i].cleanup();
}
userLock = [];
users = [];
await saveUsers();
}
async function clearAllSubmissions() {
for (let i = 0; i < users.length; ++i) {
await users[i].clearSubmitted();
}
}
module.exports = {
get all() {
return users.slice(0);
},
get: getUser,
create: createUser,
delete: deleteUser,
deleteAll: deleteAllUsers,
clearAllSubmissions: clearAllSubmissions
};