Files
NodeBB/src/socket.io/user.js

180 lines
4.9 KiB
JavaScript
Raw Normal View History

2014-03-12 22:11:48 -04:00
'use strict';
2019-09-15 02:14:51 -04:00
const util = require('util');
const winston = require('winston');
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;
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
// 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
if (meta.config['password:disableEdit']) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:no-privileges]]');
}
2019-09-15 02:14:51 -04:00
async function logEvent(text) {
await events.log({
type: 'password-reset',
2019-09-15 02:14:51 -04:00
text: text,
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]]');
await sleep(2500 + ((Math.random() * 500) - 250));
2019-09-15 02:14:51 -04:00
} catch (err) {
await logEvent(err.message);
await sleep(2500 + ((Math.random() * 500) - 250));
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
}
};
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),
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]]',
}).catch(err => winston.error(`[emailer.send] ${err.stack}`));
};
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) {
if (!socket.uid) {
2019-09-15 02:14:51 -04:00
return 0;
}
2019-09-15 02:14:51 -04:00
return await topics.getTotalUnread(socket.uid, '');
};
2019-09-15 02:14:51 -04:00
SocketUser.getUnreadChatCount = async function (socket) {
if (!socket.uid) {
2019-09-15 02:14:51 -04:00
return 0;
}
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) {
if (!socket.uid) {
2019-09-15 02:14:51 -04:00
return {};
}
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),
});
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;
};
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]]');
}
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 11:07:46 -04:00
await user.appendModerationNote({ uid: data.uid, noteData });
return await user.getModerationNotes(data.uid, 0, 0);
};
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]]');
}
2019-09-15 02:14:51 -04:00
await user.deleteUpload(socket.uid, data.uid, data.name);
};
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-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);