-
Notifications
You must be signed in to change notification settings - Fork 201
Expand file tree
/
Copy pathuser-requests.js
More file actions
389 lines (357 loc) · 10.7 KB
/
Copy pathuser-requests.js
File metadata and controls
389 lines (357 loc) · 10.7 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
const Account = require('../../models/account');
const ModAction = require('../../models/modAction');
const PlayerReport = require('../../models/playerReport');
const PlayerNote = require('../../models/playerNote');
const Game = require('../../models/game');
const Signups = require('../../models/signups');
const {
games,
userList,
generalChats,
accountCreationDisabled,
ipbansNotEnforced,
gameCreationDisabled,
limitNewPlayers,
bypassVPNCheck,
userListEmitter,
formattedUserList,
gameListEmitter,
formattedGameList,
staffList
} = require('./models');
const { getProfile } = require('../../models/profile/utils');
const { sendInProgressGameUpdate } = require('./util');
const version = require('../../version');
const { obfIP } = require('./ip-obf');
const { CURRENTSEASONNUMBER } = require('../../src/frontend-scripts/node-constants');
/**
* @param {object} socket - user socket reference.
*/
const sendUserList = (module.exports.sendUserList = socket => {
// eslint-disable-line one-var
if (socket) {
const staffUserList = Object.keys(staffList).filter(
name => staffList[name] === 'moderator' || staffList[name] === 'admin' || staffList[name] === 'trialmod'
);
if (staffUserList.includes(socket?.handshake?.session?.passport?.user)) {
socket.emit('userList', { list: formattedUserList(true) });
} else {
socket.emit('userList', { list: formattedUserList() });
}
} else {
userListEmitter.send = true;
}
});
const getModInfo = (games, users, socket, queryObj, count = 1, isTrial, isAEM) => {
const maskEmail = email => (email && email.split('@')[1]) || '';
ModAction.find(queryObj)
.sort({ $natural: -1 })
.limit(500 * count)
.then(actions => {
const list = users.map(user => {
const usr = userList.find(userListUser => user.username === userListUser.userName);
return usr
? {
status: usr.status,
isRainbow: user.isRainbowOverall,
userName: user.username,
ip: user.lastConnectedIP || user.signupIP,
email: `${user.verified ? '+' : '-'}${maskEmail(user.verification.email)}`
}
: {};
});
list.forEach(user => {
if (user.ip && user.ip != '') {
try {
user.ip = '-' + obfIP(user.ip);
} catch (e) {
user.ip = 'ERROR';
console.log(e);
}
}
});
actions.forEach(action => {
if (action.ip && action.ip != '') {
if (action.ip.startsWith('-')) {
action.ip = 'ERROR'; // There are some bugged IPs in the list right now, need to suppress it.
} else {
try {
action.ip = '-' + obfIP(action.ip);
} catch (e) {
action.ip = 'ERROR';
console.log(e);
}
}
}
});
const gList = [];
if (games) {
Object.values(games).forEach(game => {
gList.push({
name: game.general.name,
uid: game.general.uid,
electionNum: game.general.electionCount,
casual: game.general.casualGame,
private: game.general.private,
custom: game.customGameSettings.enabled,
unlisted: game.general.unlistedGame
});
});
}
socket.emit('modInfo', {
modReports: actions,
accountCreationDisabled,
ipbansNotEnforced,
gameCreationDisabled,
limitNewPlayers,
bypassVPNCheck,
userList: list,
gameList: gList,
showActions: !isTrial && isAEM
});
})
.catch(err => {
console.log(err, 'err in finding mod actions');
});
};
module.exports.getModInfo = getModInfo;
module.exports.sendSignups = socket => {
Signups.find({ type: { $in: ['local', 'discord', 'github'] } })
.sort({ $natural: -1 })
.limit(500)
.select({ unobfuscatedIP: 0 })
.then(signups => {
socket.emit('signupsInfo', signups);
})
.catch(err => {
console.log(err, 'err in finding signups');
});
};
module.exports.sendAllSignups = socket => {
Signups.find({ type: { $nin: ['local', 'private', 'discord', 'github'] } })
.sort({ $natural: -1 })
.limit(500)
.select({ unobfuscatedIP: 0 })
.then(signups => {
socket.emit('signupsInfo', signups);
})
.catch(err => {
console.log(err, 'err in finding signups');
});
};
module.exports.sendPrivateSignups = socket => {
Signups.find({ type: 'private' })
.sort({ $natural: -1 })
.limit(500)
.select({ unobfuscatedIP: 0 })
.then(signups => {
socket.emit('signupsInfo', signups);
})
.catch(err => {
console.log(err, 'err in finding signups');
});
};
/**
* @param {array} games - list of all games
* @param {object} socket - user socket reference.
* @param {number} count - depth of modinfo requested.
* @param {boolean} isTrial - true if the user is a trial mod.
* @param {boolean} isAEM - true if the user is a AEM member.
*/
module.exports.sendModInfo = (games, socket, count, isTrial, isAEM) => {
const userNames = userList.map(user => user.userName);
Account.find({ username: userNames, 'gameSettings.isPrivate': { $ne: true } })
.then(users => {
getModInfo(games, users, socket, {}, count, isTrial, isAEM);
})
.catch(err => {
console.log(err, 'err in sending mod info');
});
};
/**
* @param {object} socket - user socket reference.
*/
module.exports.sendUserGameSettings = socket => {
const { passport } = socket.handshake.session;
if (!passport || !passport.user) {
return;
}
Account.findOne({ username: passport.user })
.then(account => {
socket.emit('gameSettings', account.gameSettings);
const userListNames = userList.map(user => user.userName);
getProfile(passport.user);
if (!userListNames.includes(passport.user)) {
const userListInfo = {
userName: passport.user,
playerPronouns: account.gameSettings.playerPronouns,
staffRole: account.staffRole || '',
isContributor: account.isContributor || false,
staffDisableVisibleElo: account.gameSettings.staffDisableVisibleElo,
staffDisableVisibleXP: account.gameSettings.staffDisableVisibleXP,
staffDisableStaffColor: account.gameSettings.staffDisableStaffColor,
staffIncognito: account.gameSettings.staffIncognito,
wins: account.wins,
losses: account.losses,
rainbowWins: account.rainbowWins,
rainbowLosses: account.rainbowLosses,
isRainbowOverall: account.isRainbowOverall,
isRainbowSeason: account.isRainbowSeason,
isPrivate: account.gameSettings.isPrivate,
tournyWins: account.gameSettings.tournyWins,
blacklist: account.gameSettings.blacklist,
customCardback: account.gameSettings.customCardback,
customCardbackUid: account.gameSettings.customCardbackUid,
previousSeasonAward: account.gameSettings.previousSeasonAward,
specialTournamentStatus: account.gameSettings.specialTournamentStatus,
eloOverall: account.eloOverall,
xpOverall: account.xpOverall,
eloSeason: account.eloSeason,
xpSeason: account.xpSeason,
status: {
type: 'none',
gameId: null
}
};
userListInfo[`winsSeason${CURRENTSEASONNUMBER}`] = account[`winsSeason${CURRENTSEASONNUMBER}`];
userListInfo[`lossesSeason${CURRENTSEASONNUMBER}`] = account[`lossesSeason${CURRENTSEASONNUMBER}`];
userListInfo[`rainbowWinsSeason${CURRENTSEASONNUMBER}`] = account[`rainbowWinsSeason${CURRENTSEASONNUMBER}`];
userListInfo[`rainbowLossesSeason${CURRENTSEASONNUMBER}`] = account[`rainbowLossesSeason${CURRENTSEASONNUMBER}`];
userList.push(userListInfo);
sendUserList();
}
getProfile(passport.user);
socket.emit('version', {
current: version,
lastSeen: account.lastVersionSeen || 'none'
});
})
.catch(err => {
console.log(err);
});
};
/**
* @param {object} socket - user socket reference.
* @param {object} data - data about the request
*/
module.exports.sendPlayerNotes = (socket, data) => {
if (data) {
PlayerNote.find({ userName: data?.userName, notedUser: { $in: data?.seatedPlayers } })
.then(notes => {
if (notes) {
socket.emit('notesUpdate', notes);
}
})
.catch(err => {
console.log(err, 'err in getting playernotes');
});
}
};
/**
* @param {object} socket - user socket reference.
* @param {string} uid - uid of game.
*/
module.exports.sendReplayGameData = (socket, uid) => {
Game.findOne({ uid })
.select({ _id: 0, _v: 0 })
.then((game, err) => {
if (err) {
console.log(err, 'game err retrieving for replay');
}
if (game) {
socket.emit('replayGameData', game);
}
});
};
/**
* @param {object} socket - user socket reference.
* @param {boolean} isAEM - user AEM designation
*/
module.exports.sendGameList = (socket, isAEM) => {
// eslint-disable-line one-var
if (socket) {
let gameList = formattedGameList();
gameList = gameList.filter(game => isAEM || (game && !game.isUnlisted));
socket.emit('gameList', gameList);
} else {
gameListEmitter.send = true;
}
};
/**
* @param {object} socket - user socket reference.
*/
module.exports.sendUserReports = socket => {
PlayerReport.find()
.sort({ $natural: -1 })
.limit(500)
.then(reports => {
socket.emit('reportInfo', reports);
});
};
/**
* @param {object} socket - user socket reference.
*/
module.exports.sendGeneralChats = socket => {
socket.emit('generalChats', generalChats);
};
/**
* @param {object} passport - socket authentication.
* @param {object} game - target game.
* @param {string} override - type of user status to be displayed.
*/
const updateUserStatus = (module.exports.updateUserStatus = (passport, game, override) => {
const user = userList.find(user => user.userName === passport.user);
if (user) {
user.status = {
type:
override && game && !game.general.unlistedGame
? override
: game
? game.general.private
? 'private'
: !game.general.unlistedGame && game.general.rainbowgame
? 'rainbow'
: !game.general.unlistedGame
? 'playing'
: 'none'
: 'none',
gameId: game ? game.general.uid : false
};
sendUserList();
}
});
/**
* @param {object} socket - user socket reference.
* @param {string} uid - uid of game.
*/
module.exports.sendGameInfo = (socket, uid) => {
if (typeof uid !== 'string') {
return;
}
const game = games[uid];
const { passport } = socket.handshake.session;
if (game && game.publicPlayersState && game.general) {
if (passport && Object.keys(passport).length) {
const player = game.publicPlayersState.find(player => player.userName === passport.user);
if (player) {
player.leftGame = false;
player.connected = true;
if (game.general) game.general.timeAbandoned = null;
socket.emit('updateSeatForUser', true);
updateUserStatus(passport, game);
} else {
updateUserStatus(passport, game, 'observing');
}
}
socket.join(uid);
sendInProgressGameUpdate(game);
socket.emit('joinGameRedirect', game.general.uid);
} else {
Game.findOne({ uid }).then((game, err) => {
if (err) {
console.log(err, 'game err retrieving for replay');
}
socket.emit('manualReplayRequest', game ? game.uid : '');
});
}
};