2014-03-12 22:11:48 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
const util = require('util');
|
2020-12-05 14:25:14 -07:00
|
|
|
const winston = require('winston');
|
2021-02-03 23:53:16 -07:00
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
const sleep = util.promisify(setTimeout);
|
|
|
|
|
|
|
|
|
|
const user = require('../user');
|
|
|
|
|
const topics = require('../topics');
|
|
|
|
|
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 SocketUser = module.exports;
|
2014-01-09 22:46:51 -05:00
|
|
|
|
2015-09-25 15:56:58 -04:00
|
|
|
require('./user/profile')(SocketUser);
|
|
|
|
|
require('./user/status')(SocketUser);
|
|
|
|
|
require('./user/picture')(SocketUser);
|
2019-01-29 13:11:45 -05:00
|
|
|
require('./user/registration')(SocketUser);
|
2015-09-25 15:56:58 -04:00
|
|
|
|
2014-01-09 22:46:51 -05:00
|
|
|
// Password Reset
|
|
|
|
|
SocketUser.reset = {};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.reset.send = async function (socket, email) {
|
2016-03-09 19:13:36 +02:00
|
|
|
if (!email) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2014-01-16 18:18:42 -05:00
|
|
|
}
|
2016-03-09 19:13:36 +02:00
|
|
|
|
2019-05-09 15:51:36 -04:00
|
|
|
if (meta.config['password:disableEdit']) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:no-privileges]]');
|
2019-05-09 15:51:36 -04:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
async function logEvent(text) {
|
|
|
|
|
await events.log({
|
2019-02-08 10:50:15 -05:00
|
|
|
type: 'password-reset',
|
2019-09-15 02:14:51 -04:00
|
|
|
text: text,
|
2019-02-08 10:50:15 -05:00
|
|
|
ip: socket.ip,
|
|
|
|
|
uid: socket.uid,
|
|
|
|
|
email: email,
|
|
|
|
|
});
|
2019-09-15 02:14:51 -04:00
|
|
|
}
|
|
|
|
|
try {
|
|
|
|
|
await user.reset.send(email);
|
|
|
|
|
await logEvent('[[success:success]]');
|
2021-06-11 14:47:13 -04:00
|
|
|
await sleep(2500 + ((Math.random() * 500) - 250));
|
2019-09-15 02:14:51 -04:00
|
|
|
} catch (err) {
|
|
|
|
|
await logEvent(err.message);
|
2021-06-11 14:47:13 -04:00
|
|
|
await sleep(2500 + ((Math.random() * 500) - 250));
|
2023-01-04 11:24:46 -05:00
|
|
|
const internalErrors = ['[[error:invalid-email]]'];
|
2019-09-15 02:14:51 -04:00
|
|
|
if (!internalErrors.includes(err.message)) {
|
|
|
|
|
throw err;
|
2016-08-09 12:56:42 -04:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
}
|
2014-01-09 22:46:51 -05:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.reset.commit = async function (socket, data) {
|
2015-02-11 21:54:20 -05:00
|
|
|
if (!data || !data.code || !data.password) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2015-02-11 21:54:20 -05:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
const [uid] = await Promise.all([
|
|
|
|
|
db.getObjectField('reset:uid', data.code),
|
|
|
|
|
user.reset.commit(data.code, data.password),
|
2020-11-20 16:06:26 -05:00
|
|
|
plugins.hooks.fire('action:password.reset', { uid: socket.uid }),
|
2019-09-15 02:14:51 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
await events.log({
|
|
|
|
|
type: 'password-reset',
|
|
|
|
|
uid: uid,
|
|
|
|
|
ip: socket.ip,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const username = await user.getUserField(uid, 'username');
|
|
|
|
|
const now = new Date();
|
2021-02-03 23:59:08 -07:00
|
|
|
const parsedDate = `${now.getFullYear()}/${now.getMonth() + 1}/${now.getDate()}`;
|
2019-09-15 02:14:51 -04:00
|
|
|
emailer.send('reset_notify', uid, {
|
|
|
|
|
username: username,
|
|
|
|
|
date: parsedDate,
|
|
|
|
|
subject: '[[email:reset.notify.subject]]',
|
2022-01-28 15:25:33 -05:00
|
|
|
}).catch(err => winston.error(`[emailer.send] ${err.stack}`));
|
2014-01-09 22:46:51 -05:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.isFollowing = async function (socket, data) {
|
2015-10-29 22:22:33 -04:00
|
|
|
if (!socket.uid || !data.uid) {
|
2019-09-15 02:14:51 -04:00
|
|
|
return false;
|
2015-10-29 22:22:33 -04:00
|
|
|
}
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
return await user.isFollowing(socket.uid, data.uid);
|
2015-10-29 22:22:33 -04:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.getUnreadCount = async function (socket) {
|
2014-11-01 16:55:50 -04:00
|
|
|
if (!socket.uid) {
|
2019-09-15 02:14:51 -04:00
|
|
|
return 0;
|
2014-11-01 16:55:50 -04:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
return await topics.getTotalUnread(socket.uid, '');
|
2014-01-09 22:46:51 -05:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.getUnreadChatCount = async function (socket) {
|
2014-11-01 16:55:50 -04:00
|
|
|
if (!socket.uid) {
|
2019-09-15 02:14:51 -04:00
|
|
|
return 0;
|
2014-11-01 16:55:50 -04:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
return await messaging.getUnreadCount(socket.uid);
|
2014-07-19 10:33:27 -04:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.getUnreadCounts = async function (socket) {
|
2015-10-20 19:19:50 -04:00
|
|
|
if (!socket.uid) {
|
2019-09-15 02:14:51 -04:00
|
|
|
return {};
|
2015-10-20 19:19:50 -04:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
const results = await utils.promiseParallel({
|
|
|
|
|
unreadCounts: topics.getUnreadTids({ uid: socket.uid, count: true }),
|
|
|
|
|
unreadChatCount: messaging.getUnreadCount(socket.uid),
|
|
|
|
|
unreadNotificationCount: user.notifications.getUnreadCount(socket.uid),
|
2018-09-24 12:58:59 -04:00
|
|
|
});
|
2019-09-15 02:14:51 -04:00
|
|
|
results.unreadTopicCount = results.unreadCounts[''];
|
|
|
|
|
results.unreadNewTopicCount = results.unreadCounts.new;
|
|
|
|
|
results.unreadWatchedTopicCount = results.unreadCounts.watched;
|
|
|
|
|
results.unreadUnrepliedTopicCount = results.unreadCounts.unreplied;
|
|
|
|
|
return results;
|
2015-10-20 19:19:50 -04:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.getUserByUID = async function (socket, uid) {
|
|
|
|
|
return await userController.getUserDataByField(socket.uid, 'uid', uid);
|
2016-03-08 11:24:32 +02:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.getUserByUsername = async function (socket, username) {
|
|
|
|
|
return await userController.getUserDataByField(socket.uid, 'username', username);
|
2016-03-08 11:24:32 +02:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.getUserByEmail = async function (socket, email) {
|
|
|
|
|
return await userController.getUserDataByField(socket.uid, 'email', email);
|
2016-03-08 11:24:32 +02:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.setModerationNote = async function (socket, data) {
|
2017-03-23 10:58:17 +03:00
|
|
|
if (!socket.uid || !data || !data.uid || !data.note) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2016-09-21 12:55:44 +03:00
|
|
|
}
|
2019-04-05 13:44:15 -04:00
|
|
|
const noteData = {
|
|
|
|
|
uid: socket.uid,
|
|
|
|
|
note: data.note,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
};
|
2019-09-15 02:14:51 -04:00
|
|
|
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]]');
|
|
|
|
|
}
|
2020-09-14 10:16:35 -04:00
|
|
|
|
2020-09-14 11:07:46 -04:00
|
|
|
await user.appendModerationNote({ uid: data.uid, noteData });
|
2024-04-23 13:00:47 -04:00
|
|
|
return await user.getModerationNotes(data.uid, 0, 0);
|
2016-09-21 12:55:44 +03:00
|
|
|
};
|
2018-04-09 12:22:44 -04:00
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.deleteUpload = async function (socket, data) {
|
2018-04-12 12:35:05 -04:00
|
|
|
if (!data || !data.name || !data.uid) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2018-04-09 20:03:33 -04:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
await user.deleteUpload(socket.uid, data.uid, data.name);
|
2018-04-09 20:03:33 -04:00
|
|
|
};
|
|
|
|
|
|
2018-04-09 12:22:44 -04:00
|
|
|
SocketUser.gdpr = {};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.gdpr.consent = async function (socket) {
|
|
|
|
|
await user.setUserField(socket.uid, 'gdpr_consent', 1);
|
2018-04-09 12:22:44 -04:00
|
|
|
};
|
2018-05-11 12:18:51 -04:00
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.gdpr.check = async function (socket, data) {
|
|
|
|
|
const isAdmin = await user.isAdministrator(socket.uid);
|
|
|
|
|
if (!isAdmin) {
|
|
|
|
|
data.uid = socket.uid;
|
|
|
|
|
}
|
2021-02-03 23:59:08 -07:00
|
|
|
return await db.getObjectField(`user:${data.uid}`, 'gdpr_consent');
|
2018-05-11 12:18:51 -04:00
|
|
|
};
|
2019-09-15 02:14:51 -04:00
|
|
|
|
|
|
|
|
require('../promisify')(SocketUser);
|