-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
user.js
329 lines (280 loc) · 9.08 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
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
'use strict';
const async = require('async');
const util = require('util');
const sleep = util.promisify(setTimeout);
const user = require('../user');
const topics = require('../topics');
const notifications = require('../notifications');
const messaging = require('../messaging');
const plugins = require('../plugins');
const meta = require('../meta');
const events = require('../events');
const emailer = require('../emailer');
const db = require('../database');
const userController = require('../controllers/user');
const privileges = require('../privileges');
const utils = require('../utils');
const flags = require('../flags');
const SocketUser = module.exports;
require('./user/profile')(SocketUser);
require('./user/search')(SocketUser);
require('./user/status')(SocketUser);
require('./user/picture')(SocketUser);
require('./user/ban')(SocketUser);
require('./user/registration')(SocketUser);
SocketUser.exists = async function (socket, data) {
if (!data || !data.username) {
throw new Error('[[error:invalid-data]]');
}
return await meta.userOrGroupExists(data.username);
};
SocketUser.deleteAccount = async function (socket, data) {
if (!socket.uid) {
throw new Error('[[error:no-privileges]]');
}
const hasPassword = await user.hasPassword(socket.uid);
if (hasPassword) {
const ok = await user.isPasswordCorrect(socket.uid, data.password, socket.ip);
if (!ok) {
throw new Error('[[error:invalid-password]]');
}
}
const isAdmin = await user.isAdministrator(socket.uid);
if (isAdmin) {
throw new Error('[[error:cant-delete-admin]]');
}
if (meta.config.allowAccountDelete !== 1) {
throw new Error('[[error:no-privileges]]');
}
await flags.resolveFlag('user', socket.uid, socket.uid);
const userData = await user.deleteAccount(socket.uid);
require('./index').server.sockets.emit('event:user_status_change', { uid: socket.uid, status: 'offline' });
await events.log({
type: 'user-delete',
uid: socket.uid,
targetUid: socket.uid,
ip: socket.ip,
username: userData.username,
email: userData.email,
});
};
SocketUser.emailExists = async function (socket, data) {
if (!data || !data.email) {
throw new Error('[[error:invalid-data]]');
}
return await user.email.exists(data.email);
};
SocketUser.emailConfirm = async function (socket) {
if (!socket.uid) {
throw new Error('[[error:no-privileges]]');
}
if (!meta.config.requireEmailConfirmation) {
throw new Error('[[error:email-confirmations-are-disabled]]');
}
return await user.email.sendValidationEmail(socket.uid);
};
// Password Reset
SocketUser.reset = {};
SocketUser.reset.send = async function (socket, email) {
if (!email) {
throw new Error('[[error:invalid-data]]');
}
if (meta.config['password:disableEdit']) {
throw new Error('[[error:no-privileges]]');
}
async function logEvent(text) {
await events.log({
type: 'password-reset',
text: text,
ip: socket.ip,
uid: socket.uid,
email: email,
});
}
try {
await user.reset.send(email);
await logEvent('[[success:success]]');
await sleep(2500);
} catch (err) {
await logEvent(err.message);
const internalErrors = ['[[error:invalid-email]]', '[[error:reset-rate-limited]]'];
if (!internalErrors.includes(err.message)) {
throw err;
}
}
};
SocketUser.reset.commit = async function (socket, data) {
if (!data || !data.code || !data.password) {
throw new Error('[[error:invalid-data]]');
}
const [uid] = await Promise.all([
db.getObjectField('reset:uid', data.code),
user.reset.commit(data.code, data.password),
plugins.fireHook('action:password.reset', { uid: socket.uid }),
]);
await events.log({
type: 'password-reset',
uid: uid,
ip: socket.ip,
});
const username = await user.getUserField(uid, 'username');
const now = new Date();
const parsedDate = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate();
emailer.send('reset_notify', uid, {
username: username,
date: parsedDate,
subject: '[[email:reset.notify.subject]]',
});
};
SocketUser.isFollowing = async function (socket, data) {
if (!socket.uid || !data.uid) {
return false;
}
return await user.isFollowing(socket.uid, data.uid);
};
SocketUser.follow = async function (socket, data) {
if (!socket.uid || !data) {
throw new Error('[[error:invalid-data]]');
}
await toggleFollow('follow', socket.uid, data.uid);
const userData = await user.getUserFields(socket.uid, ['username', 'userslug']);
const notifObj = await notifications.create({
type: 'follow',
bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]',
nid: 'follow:' + data.uid + ':uid:' + socket.uid,
from: socket.uid,
path: '/uid/' + data.uid + '/followers',
mergeId: 'notifications:user_started_following_you',
});
if (!notifObj) {
return;
}
notifObj.user = userData;
await notifications.push(notifObj, [data.uid]);
};
SocketUser.unfollow = async function (socket, data) {
if (!socket.uid || !data) {
throw new Error('[[error:invalid-data]]');
}
await toggleFollow('unfollow', socket.uid, data.uid);
};
async function toggleFollow(method, uid, theiruid) {
await user[method](uid, theiruid);
plugins.fireHook('action:user.' + method, {
fromUid: uid,
toUid: theiruid,
});
}
SocketUser.saveSettings = async function (socket, data) {
if (!socket.uid || !data || !data.settings) {
throw new Error('[[error:invalid-data]]');
}
const canEdit = await privileges.users.canEdit(socket.uid, data.uid);
if (!canEdit) {
throw new Error('[[error:no-privileges]]');
}
return await user.saveSettings(data.uid, data.settings);
};
SocketUser.setTopicSort = async function (socket, sort) {
await user.setSetting(socket.uid, 'topicPostSort', sort);
};
SocketUser.setCategorySort = async function (socket, sort) {
await user.setSetting(socket.uid, 'categoryTopicSort', sort);
};
SocketUser.getUnreadCount = async function (socket) {
if (!socket.uid) {
return 0;
}
return await topics.getTotalUnread(socket.uid, '');
};
SocketUser.getUnreadChatCount = async function (socket) {
if (!socket.uid) {
return 0;
}
return await messaging.getUnreadCount(socket.uid);
};
SocketUser.getUnreadCounts = async function (socket) {
if (!socket.uid) {
return {};
}
const results = await utils.promiseParallel({
unreadCounts: topics.getUnreadTids({ uid: socket.uid, count: true }),
unreadChatCount: messaging.getUnreadCount(socket.uid),
unreadNotificationCount: user.notifications.getUnreadCount(socket.uid),
});
results.unreadTopicCount = results.unreadCounts[''];
results.unreadNewTopicCount = results.unreadCounts.new;
results.unreadWatchedTopicCount = results.unreadCounts.watched;
results.unreadUnrepliedTopicCount = results.unreadCounts.unreplied;
return results;
};
SocketUser.invite = async function (socket, email) {
if (!email || !socket.uid) {
throw new Error('[[error:invalid-data]]');
}
const registrationType = meta.config.registrationType;
if (registrationType !== 'invite-only' && registrationType !== 'admin-invite-only') {
throw new Error('[[error:forum-not-invite-only]]');
}
const isAdmin = await user.isAdministrator(socket.uid);
if (registrationType === 'admin-invite-only' && !isAdmin) {
throw new Error('[[error:no-privileges]]');
}
const max = meta.config.maximumInvites;
email = email.split(',').map(email => email.trim()).filter(Boolean);
await async.eachSeries(email, async function (email) {
let invites = 0;
if (max) {
invites = await user.getInvitesNumber(socket.uid);
}
if (!isAdmin && max && invites >= max) {
throw new Error('[[error:invite-maximum-met, ' + invites + ', ' + max + ']]');
}
await user.sendInvitationEmail(socket.uid, email);
});
};
SocketUser.getUserByUID = async function (socket, uid) {
return await userController.getUserDataByField(socket.uid, 'uid', uid);
};
SocketUser.getUserByUsername = async function (socket, username) {
return await userController.getUserDataByField(socket.uid, 'username', username);
};
SocketUser.getUserByEmail = async function (socket, email) {
return await userController.getUserDataByField(socket.uid, 'email', email);
};
SocketUser.setModerationNote = async function (socket, data) {
if (!socket.uid || !data || !data.uid || !data.note) {
throw new Error('[[error:invalid-data]]');
}
const noteData = {
uid: socket.uid,
note: data.note,
timestamp: Date.now(),
};
let canEdit = await privileges.users.canEdit(socket.uid, data.uid);
if (!canEdit) {
canEdit = await user.isModeratorOfAnyCategory(socket.uid);
}
if (!canEdit) {
throw new Error('[[error:no-privileges]]');
}
await user.appendModerationNote({ uid: data.uid, noteData });
};
SocketUser.deleteUpload = async function (socket, data) {
if (!data || !data.name || !data.uid) {
throw new Error('[[error:invalid-data]]');
}
await user.deleteUpload(socket.uid, data.uid, data.name);
};
SocketUser.gdpr = {};
SocketUser.gdpr.consent = async function (socket) {
await user.setUserField(socket.uid, 'gdpr_consent', 1);
};
SocketUser.gdpr.check = async function (socket, data) {
const isAdmin = await user.isAdministrator(socket.uid);
if (!isAdmin) {
data.uid = socket.uid;
}
return await db.getObjectField('user:' + data.uid, 'gdpr_consent');
};
require('../promisify')(SocketUser);