2014-03-12 22:11:48 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
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 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/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) {
|
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]]');
|
2014-01-09 22:46:51 -05:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
return await meta.userOrGroupExists(data.username);
|
2014-01-09 22:46:51 -05:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.deleteAccount = async function (socket, data) {
|
2015-04-22 10:41:44 -04:00
|
|
|
if (!socket.uid) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:no-privileges]]');
|
2015-04-22 10:41:44 -04:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
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]]');
|
|
|
|
|
}
|
2020-02-13 11:31:20 -05:00
|
|
|
if (meta.config.allowAccountDelete !== 1) {
|
|
|
|
|
throw new Error('[[error:no-privileges]]');
|
|
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
const userData = await user.deleteAccount(socket.uid);
|
|
|
|
|
require('./index').server.sockets.emit('event:user_status_change', { uid: socket.uid, status: 'offline' });
|
2015-04-08 18:11:17 -04:00
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
await events.log({
|
|
|
|
|
type: 'user-delete',
|
|
|
|
|
uid: socket.uid,
|
|
|
|
|
targetUid: socket.uid,
|
|
|
|
|
ip: socket.ip,
|
|
|
|
|
username: userData.username,
|
|
|
|
|
email: userData.email,
|
|
|
|
|
});
|
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);
|
2014-01-09 22:46:51 -05:00
|
|
|
};
|
|
|
|
|
|
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
|
|
|
}
|
2014-06-03 18:39:54 -04:00
|
|
|
|
2018-10-21 16:47:51 -04:00
|
|
|
if (!meta.config.requireEmailConfirmation) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:email-confirmations-are-disabled]]');
|
2014-06-03 18:39:54 -04:00
|
|
|
}
|
2016-03-09 19:13:36 +02:00
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
return await user.email.sendValidationEmail(socket.uid);
|
2014-06-03 18:39:54 -04:00
|
|
|
};
|
|
|
|
|
|
2014-01-19 22:07:29 -05: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]]');
|
|
|
|
|
await sleep(2500);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
await logEvent(err.message);
|
2019-02-08 10:50:15 -05:00
|
|
|
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
|
|
|
}
|
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),
|
|
|
|
|
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]]',
|
|
|
|
|
});
|
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.follow = async function (socket, data) {
|
2014-09-08 23:03:37 -04:00
|
|
|
if (!socket.uid || !data) {
|
2019-09-15 02:14:51 -04:00
|
|
|
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;
|
2014-09-08 23:03:37 -04:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
notifObj.user = userData;
|
|
|
|
|
await notifications.push(notifObj, [data.uid]);
|
2014-01-09 22:46:51 -05:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.unfollow = async function (socket, data) {
|
2016-03-09 19:13:36 +02:00
|
|
|
if (!socket.uid || !data) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2014-01-09 22:46:51 -05:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
await toggleFollow('unfollow', socket.uid, data.uid);
|
2014-01-09 22:46:51 -05:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
async function toggleFollow(method, uid, theiruid) {
|
|
|
|
|
await user[method](uid, theiruid);
|
|
|
|
|
plugins.fireHook('action:user.' + method, {
|
|
|
|
|
fromUid: uid,
|
|
|
|
|
toUid: theiruid,
|
|
|
|
|
});
|
2014-08-14 17:59:33 -04:00
|
|
|
}
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.saveSettings = async function (socket, data) {
|
2019-03-15 15:26:41 -04:00
|
|
|
if (!socket.uid || !data || !data.settings) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2014-01-09 22:46:51 -05:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
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);
|
2014-01-09 22:46:51 -05:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.setTopicSort = async function (socket, sort) {
|
|
|
|
|
await user.setSetting(socket.uid, 'topicPostSort', sort);
|
2014-06-06 22:12:14 -04:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketUser.setCategorySort = async function (socket, sort) {
|
|
|
|
|
await user.setSetting(socket.uid, 'categoryTopicSort', sort);
|
2015-01-08 13:47:15 -05: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.invite = async function (socket, email) {
|
2015-06-28 21:54:21 -04:00
|
|
|
if (!email || !socket.uid) {
|
2019-09-15 02:14:51 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
2015-06-28 21:54:21 -04:00
|
|
|
}
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
const registrationType = meta.config.registrationType;
|
2015-11-28 15:33:17 -07:00
|
|
|
if (registrationType !== 'invite-only' && registrationType !== 'admin-invite-only') {
|
2019-09-15 02:14:51 -04:00
|
|
|
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]]');
|
2015-06-28 21:54:21 -04:00
|
|
|
}
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
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);
|
|
|
|
|
});
|
2015-06-28 21:54:21 -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]]');
|
|
|
|
|
}
|
|
|
|
|
await db.sortedSetAdd('uid:' + data.uid + ':moderation:notes', noteData.timestamp, noteData.timestamp);
|
|
|
|
|
await db.setObject('uid:' + data.uid + ':moderation:note:' + noteData.timestamp, noteData);
|
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;
|
|
|
|
|
}
|
|
|
|
|
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);
|