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

248 lines
6.8 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 sleep = util.promisify(setTimeout);
const api = require('../api');
2019-09-15 02:14:51 -04:00
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 sockets = require('.');
2019-09-15 02:14:51 -04:00
const SocketUser = module.exports;
2015-09-25 15:56:58 -04:00
require('./user/profile')(SocketUser);
require('./user/search')(SocketUser);
require('./user/status')(SocketUser);
require('./user/picture')(SocketUser);
2016-01-25 13:36:10 +02:00
require('./user/ban')(SocketUser);
2019-01-29 13:11:45 -05:00
require('./user/registration')(SocketUser);
2015-09-25 15:56:58 -04:00
2019-09-15 02:14:51 -04:00
SocketUser.exists = async function (socket, data) {
sockets.warnDeprecated(socket, 'HEAD /api/v3/users/bySlug/:userslug *AND* HEAD /api/v3/groups/:slug');
2020-11-06 11:55:04 -05:00
2016-03-09 19:13:36 +02:00
if (!data || !data.username) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
}
2019-09-15 02:14:51 -04:00
return await meta.userOrGroupExists(data.username);
};
2019-09-15 02:14:51 -04:00
SocketUser.deleteAccount = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/users/:uid/account');
await api.users.deleteAccount(socket, data);
2014-08-26 13:47:48 -04:00
};
2019-09-15 02:14:51 -04:00
SocketUser.emailExists = async function (socket, data) {
2016-03-09 19:13:36 +02:00
if (!data || !data.email) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
2014-01-16 18:18:42 -05:00
}
2019-09-15 02:14:51 -04:00
return await user.email.exists(data.email);
};
2019-09-15 02:14:51 -04:00
SocketUser.emailConfirm = async function (socket) {
2016-03-09 19:13:36 +02:00
if (!socket.uid) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:no-privileges]]');
2016-03-09 19:13:36 +02:00
}
if (!meta.config.requireEmailConfirmation) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:email-confirmations-are-disabled]]');
}
2016-03-09 19:13:36 +02:00
2019-09-15 02:14:51 -04:00
return await user.email.sendValidationEmail(socket.uid);
};
// 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);
} catch (err) {
await logEvent(err.message);
const internalErrors = ['[[error:invalid-email]]', '[[error:reset-rate-limited]]'];
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();
const parsedDate = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate();
emailer.send('reset_notify', uid, {
username: username,
date: parsedDate,
subject: '[[email:reset.notify.subject]]',
});
};
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.follow = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v3/users/follow');
await api.users.follow(socket, data);
};
2019-09-15 02:14:51 -04:00
SocketUser.unfollow = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/users/unfollow');
await api.users.unfollow(socket, data);
};
2019-09-15 02:14:51 -04:00
SocketUser.saveSettings = async function (socket, data) {
2020-10-22 14:07:45 -04:00
sockets.warnDeprecated(socket, 'PUT /api/v3/users/:uid/settings');
const settings = await api.users.updateSettings(socket, data);
return settings;
};
2019-09-15 02:14:51 -04:00
SocketUser.setTopicSort = async function (socket, sort) {
2020-10-22 14:07:45 -04:00
sockets.warnDeprecated(socket, 'PUT /api/v3/users/:uid/setting/topicPostSort');
await api.users.updateSetting(socket, {
uid: socket.uid,
setting: 'topicPostSort',
value: sort,
});
};
2019-09-15 02:14:51 -04:00
SocketUser.setCategorySort = async function (socket, sort) {
2020-10-22 14:07:45 -04:00
sockets.warnDeprecated(socket, 'PUT /api/v3/users/:uid/setting/categoryTopicSort');
await api.users.updateSetting(socket, {
uid: socket.uid,
setting: 'categoryTopicSort',
value: sort,
});
2015-01-08 13:47:15 -05: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 });
};
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;
}
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);