refactor: async/await socket.io
parent
9d41955bca
commit
a7d2b8a1fe
@ -1,63 +1,41 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const user = require('../user');
|
||||||
|
const notifications = require('../notifications');
|
||||||
|
const SocketNotifs = module.exports;
|
||||||
|
|
||||||
var user = require('../user');
|
SocketNotifs.get = async function (socket, data) {
|
||||||
var notifications = require('../notifications');
|
|
||||||
var SocketNotifs = module.exports;
|
|
||||||
|
|
||||||
SocketNotifs.get = function (socket, data, callback) {
|
|
||||||
if (data && Array.isArray(data.nids) && socket.uid) {
|
if (data && Array.isArray(data.nids) && socket.uid) {
|
||||||
user.notifications.getNotifications(data.nids, socket.uid, callback);
|
return await user.notifications.getNotifications(data.nids, socket.uid);
|
||||||
} else {
|
|
||||||
user.notifications.get(socket.uid, callback);
|
|
||||||
}
|
}
|
||||||
|
return await user.notifications.get(socket.uid);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketNotifs.getCount = function (socket, data, callback) {
|
SocketNotifs.getCount = async function (socket) {
|
||||||
user.notifications.getUnreadCount(socket.uid, callback);
|
return await user.notifications.getUnreadCount(socket.uid);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketNotifs.deleteAll = function (socket, data, callback) {
|
SocketNotifs.deleteAll = async function (socket) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:no-privileges]]'));
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
user.notifications.deleteAll(socket.uid, callback);
|
await user.notifications.deleteAll(socket.uid);
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketNotifs.markRead = function (socket, nid, callback) {
|
SocketNotifs.markRead = async function (socket, nid) {
|
||||||
async.waterfall([
|
await notifications.markRead(nid, socket.uid);
|
||||||
function (next) {
|
user.notifications.pushCount(socket.uid);
|
||||||
notifications.markRead(nid, socket.uid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
user.notifications.pushCount(socket.uid);
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketNotifs.markUnread = function (socket, nid, callback) {
|
SocketNotifs.markUnread = async function (socket, nid) {
|
||||||
async.waterfall([
|
await notifications.markUnread(nid, socket.uid);
|
||||||
function (next) {
|
user.notifications.pushCount(socket.uid);
|
||||||
notifications.markUnread(nid, socket.uid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
user.notifications.pushCount(socket.uid);
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketNotifs.markAllRead = function (socket, data, callback) {
|
SocketNotifs.markAllRead = async function (socket) {
|
||||||
async.waterfall([
|
await notifications.markAllRead(socket.uid);
|
||||||
function (next) {
|
user.notifications.pushCount(socket.uid);
|
||||||
notifications.markAllRead(socket.uid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
user.notifications.pushCount(socket.uid);
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
require('../promisify')(SocketNotifs);
|
||||||
|
@ -1,149 +1,101 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const winston = require('winston');
|
||||||
var winston = require('winston');
|
|
||||||
|
|
||||||
var db = require('../../database');
|
const db = require('../../database');
|
||||||
var user = require('../../user');
|
const user = require('../../user');
|
||||||
var meta = require('../../meta');
|
const meta = require('../../meta');
|
||||||
var websockets = require('../index');
|
const websockets = require('../index');
|
||||||
var events = require('../../events');
|
const events = require('../../events');
|
||||||
var privileges = require('../../privileges');
|
const privileges = require('../../privileges');
|
||||||
var plugins = require('../../plugins');
|
const plugins = require('../../plugins');
|
||||||
var emailer = require('../../emailer');
|
const emailer = require('../../emailer');
|
||||||
var translator = require('../../translator');
|
const translator = require('../../translator');
|
||||||
var utils = require('../../../public/src/utils');
|
const utils = require('../../../public/src/utils');
|
||||||
|
|
||||||
module.exports = function (SocketUser) {
|
module.exports = function (SocketUser) {
|
||||||
SocketUser.banUsers = function (socket, data, callback) {
|
SocketUser.banUsers = async function (socket, data) {
|
||||||
if (!data || !Array.isArray(data.uids)) {
|
if (!data || !Array.isArray(data.uids)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleBan(socket.uid, data.uids, function (uid, next) {
|
await toggleBan(socket.uid, data.uids, async function (uid) {
|
||||||
async.waterfall([
|
await banUser(socket.uid, uid, data.until || 0, data.reason || '');
|
||||||
function (next) {
|
await events.log({
|
||||||
banUser(socket.uid, uid, data.until || 0, data.reason || '', next);
|
type: 'user-ban',
|
||||||
},
|
uid: socket.uid,
|
||||||
function (next) {
|
targetUid: uid,
|
||||||
events.log({
|
ip: socket.ip,
|
||||||
type: 'user-ban',
|
reason: data.reason || undefined,
|
||||||
uid: socket.uid,
|
});
|
||||||
targetUid: uid,
|
plugins.fireHook('action:user.banned', {
|
||||||
ip: socket.ip,
|
callerUid: socket.uid,
|
||||||
reason: data.reason || undefined,
|
ip: socket.ip,
|
||||||
}, next);
|
uid: uid,
|
||||||
},
|
until: data.until > 0 ? data.until : undefined,
|
||||||
function (next) {
|
reason: data.reason || undefined,
|
||||||
plugins.fireHook('action:user.banned', {
|
});
|
||||||
callerUid: socket.uid,
|
await user.auth.revokeAllSessions(uid);
|
||||||
ip: socket.ip,
|
});
|
||||||
uid: uid,
|
|
||||||
until: data.until > 0 ? data.until : undefined,
|
|
||||||
reason: data.reason || undefined,
|
|
||||||
});
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
user.auth.revokeAllSessions(uid, next);
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}, callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.unbanUsers = function (socket, uids, callback) {
|
SocketUser.unbanUsers = async function (socket, uids) {
|
||||||
toggleBan(socket.uid, uids, function (uid, next) {
|
await toggleBan(socket.uid, uids, async function (uid) {
|
||||||
async.waterfall([
|
await user.bans.unban(uid);
|
||||||
function (next) {
|
await events.log({
|
||||||
user.bans.unban(uid, next);
|
type: 'user-unban',
|
||||||
},
|
uid: socket.uid,
|
||||||
function (next) {
|
targetUid: uid,
|
||||||
events.log({
|
ip: socket.ip,
|
||||||
type: 'user-unban',
|
});
|
||||||
uid: socket.uid,
|
plugins.fireHook('action:user.unbanned', {
|
||||||
targetUid: uid,
|
callerUid: socket.uid,
|
||||||
ip: socket.ip,
|
ip: socket.ip,
|
||||||
}, next);
|
uid: uid,
|
||||||
},
|
});
|
||||||
function (next) {
|
});
|
||||||
plugins.fireHook('action:user.unbanned', {
|
|
||||||
callerUid: socket.uid,
|
|
||||||
ip: socket.ip,
|
|
||||||
uid: uid,
|
|
||||||
});
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], next);
|
|
||||||
}, callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function toggleBan(uid, uids, method, callback) {
|
async function toggleBan(uid, uids, method) {
|
||||||
if (!Array.isArray(uids)) {
|
if (!Array.isArray(uids)) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
|
}
|
||||||
|
const hasBanPrivilege = await privileges.users.hasBanPrivilege(uid);
|
||||||
|
if (!hasBanPrivilege) {
|
||||||
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
await Promise.all(uids.map(uid => method(uid)));
|
||||||
function (next) {
|
|
||||||
privileges.users.hasBanPrivilege(uid, next);
|
|
||||||
},
|
|
||||||
function (hasBanPrivilege, next) {
|
|
||||||
if (!hasBanPrivilege) {
|
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
|
||||||
}
|
|
||||||
async.each(uids, method, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function banUser(callerUid, uid, until, reason, callback) {
|
async function banUser(callerUid, uid, until, reason) {
|
||||||
async.waterfall([
|
const isAdmin = await user.isAdministrator(uid);
|
||||||
function (next) {
|
if (isAdmin) {
|
||||||
user.isAdministrator(uid, next);
|
throw new Error('[[error:cant-ban-other-admins]]');
|
||||||
},
|
}
|
||||||
function (isAdmin, next) {
|
const username = await user.getUserField(uid, 'username');
|
||||||
if (isAdmin) {
|
const siteTitle = meta.config.title || 'NodeBB';
|
||||||
return next(new Error('[[error:cant-ban-other-admins]]'));
|
const data = {
|
||||||
}
|
subject: '[[email:banned.subject, ' + siteTitle + ']]',
|
||||||
|
username: username,
|
||||||
|
until: until ? utils.toISOString(until) : false,
|
||||||
|
reason: reason,
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
await emailer.send('banned', uid, data);
|
||||||
|
} catch (err) {
|
||||||
|
winston.error('[emailer.send] ' + err.message);
|
||||||
|
}
|
||||||
|
const banData = await user.bans.ban(uid, until, reason);
|
||||||
|
await db.setObjectField('uid:' + uid + ':ban:' + banData.timestamp, 'fromUid', callerUid);
|
||||||
|
|
||||||
user.getUserField(uid, 'username', next);
|
if (!reason) {
|
||||||
},
|
reason = await translator.translate('[[user:info.banned-no-reason]]');
|
||||||
function (username, next) {
|
}
|
||||||
var siteTitle = meta.config.title || 'NodeBB';
|
|
||||||
var data = {
|
|
||||||
subject: '[[email:banned.subject, ' + siteTitle + ']]',
|
|
||||||
username: username,
|
|
||||||
until: until ? utils.toISOString(until) : false,
|
|
||||||
reason: reason,
|
|
||||||
};
|
|
||||||
|
|
||||||
emailer.send('banned', uid, data, function (err) {
|
websockets.in('uid_' + uid).emit('event:banned', {
|
||||||
if (err) {
|
until: until,
|
||||||
winston.error('[emailer.send] ' + err.message);
|
reason: reason,
|
||||||
}
|
});
|
||||||
next();
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
user.bans.ban(uid, until, reason, next);
|
|
||||||
},
|
|
||||||
function (banData, next) {
|
|
||||||
db.setObjectField('uid:' + uid + ':ban:' + banData.timestamp, 'fromUid', callerUid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
if (reason) {
|
|
||||||
return next(null, reason);
|
|
||||||
}
|
|
||||||
translator.translate('[[user:info.banned-no-reason]]', function (translated) {
|
|
||||||
next(null, translated);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function (_reason, next) {
|
|
||||||
websockets.in('uid_' + uid).emit('event:banned', {
|
|
||||||
until: until,
|
|
||||||
reason: _reason,
|
|
||||||
});
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -1,110 +1,82 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const path = require('path');
|
||||||
var path = require('path');
|
const nconf = require('nconf');
|
||||||
var nconf = require('nconf');
|
|
||||||
|
|
||||||
var user = require('../../user');
|
const user = require('../../user');
|
||||||
var plugins = require('../../plugins');
|
const plugins = require('../../plugins');
|
||||||
var file = require('../../file');
|
const file = require('../../file');
|
||||||
|
|
||||||
module.exports = function (SocketUser) {
|
module.exports = function (SocketUser) {
|
||||||
SocketUser.changePicture = function (socket, data, callback) {
|
SocketUser.changePicture = async function (socket, data) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:invalid-uid]]'));
|
throw new Error('[[error:invalid-uid]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
var type = data.type;
|
const type = data.type;
|
||||||
|
let picture = '';
|
||||||
|
await user.isAdminOrGlobalModOrSelf(socket.uid, data.uid);
|
||||||
|
if (type === 'default') {
|
||||||
|
picture = '';
|
||||||
|
} else if (type === 'uploaded') {
|
||||||
|
picture = await user.getUserField(data.uid, 'uploadedpicture');
|
||||||
|
} else {
|
||||||
|
const returnData = await plugins.fireHook('filter:user.getPicture', {
|
||||||
|
uid: socket.uid,
|
||||||
|
type: type,
|
||||||
|
picture: undefined,
|
||||||
|
});
|
||||||
|
picture = returnData && returnData.picture;
|
||||||
|
}
|
||||||
|
|
||||||
async.waterfall([
|
await user.setUserField(data.uid, 'picture', picture);
|
||||||
function (next) {
|
|
||||||
user.isAdminOrGlobalModOrSelf(socket.uid, data.uid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
switch (type) {
|
|
||||||
case 'default':
|
|
||||||
next(null, '');
|
|
||||||
break;
|
|
||||||
case 'uploaded':
|
|
||||||
user.getUserField(data.uid, 'uploadedpicture', next);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
plugins.fireHook('filter:user.getPicture', {
|
|
||||||
uid: socket.uid,
|
|
||||||
type: type,
|
|
||||||
picture: undefined,
|
|
||||||
}, function (err, returnData) {
|
|
||||||
next(err, returnData && returnData.picture);
|
|
||||||
});
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
function (picture, next) {
|
|
||||||
user.setUserField(data.uid, 'picture', picture, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.removeUploadedPicture = function (socket, data, callback) {
|
SocketUser.removeUploadedPicture = async function (socket, data) {
|
||||||
if (!socket.uid || !data || !data.uid) {
|
if (!socket.uid || !data || !data.uid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
await user.isAdminOrSelf(socket.uid, data.uid);
|
||||||
async.waterfall([
|
const userData = await user.getUserFields(data.uid, ['uploadedpicture', 'picture']);
|
||||||
function (next) {
|
if (userData.uploadedpicture && !userData.uploadedpicture.startsWith('http')) {
|
||||||
user.isAdminOrSelf(socket.uid, data.uid, next);
|
const pathToFile = path.join(nconf.get('base_dir'), 'public', userData.uploadedpicture);
|
||||||
},
|
if (pathToFile.startsWith(nconf.get('upload_path'))) {
|
||||||
function (next) {
|
file.delete(pathToFile);
|
||||||
user.getUserFields(data.uid, ['uploadedpicture', 'picture'], next);
|
}
|
||||||
},
|
}
|
||||||
function (userData, next) {
|
await user.setUserFields(data.uid, {
|
||||||
if (userData.uploadedpicture && !userData.uploadedpicture.startsWith('http')) {
|
uploadedpicture: '',
|
||||||
var pathToFile = path.join(nconf.get('base_dir'), 'public', userData.uploadedpicture);
|
// if current picture is uploaded picture, reset to user icon
|
||||||
if (pathToFile.startsWith(nconf.get('upload_path'))) {
|
picture: userData.uploadedpicture === userData.picture ? '' : userData.picture,
|
||||||
file.delete(pathToFile);
|
});
|
||||||
}
|
plugins.fireHook('action:user.removeUploadedPicture', { callerUid: socket.uid, uid: data.uid });
|
||||||
}
|
|
||||||
|
|
||||||
user.setUserFields(data.uid, {
|
|
||||||
uploadedpicture: '',
|
|
||||||
picture: userData.uploadedpicture === userData.picture ? '' : userData.picture, // if current picture is uploaded picture, reset to user icon
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
plugins.fireHook('action:user.removeUploadedPicture', { callerUid: socket.uid, uid: data.uid }, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.getProfilePictures = function (socket, data, callback) {
|
SocketUser.getProfilePictures = async function (socket, data) {
|
||||||
if (!data || !data.uid) {
|
if (!data || !data.uid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
|
}
|
||||||
|
|
||||||
|
const [list, uploaded] = await Promise.all([
|
||||||
|
plugins.fireHook('filter:user.listPictures', {
|
||||||
|
uid: data.uid,
|
||||||
|
pictures: [],
|
||||||
|
}),
|
||||||
|
user.getUserField(data.uid, 'uploadedpicture'),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if (uploaded) {
|
||||||
|
list.pictures.push({
|
||||||
|
type: 'uploaded',
|
||||||
|
url: data.uploaded,
|
||||||
|
text: '[[user:uploaded_picture]]',
|
||||||
|
});
|
||||||
}
|
}
|
||||||
async.waterfall([
|
|
||||||
function (next) {
|
|
||||||
async.parallel({
|
|
||||||
list: async.apply(plugins.fireHook, 'filter:user.listPictures', {
|
|
||||||
uid: data.uid,
|
|
||||||
pictures: [],
|
|
||||||
}),
|
|
||||||
uploaded: async.apply(user.getUserField, data.uid, 'uploadedpicture'),
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (data, next) {
|
|
||||||
if (data.uploaded) {
|
|
||||||
data.list.pictures.push({
|
|
||||||
type: 'uploaded',
|
|
||||||
url: data.uploaded,
|
|
||||||
text: '[[user:uploaded_picture]]',
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
next(null, data.list.pictures);
|
return list.pictures;
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -1,229 +1,143 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const user = require('../../user');
|
||||||
|
const meta = require('../../meta');
|
||||||
var user = require('../../user');
|
const events = require('../../events');
|
||||||
var meta = require('../../meta');
|
const privileges = require('../../privileges');
|
||||||
var events = require('../../events');
|
|
||||||
var privileges = require('../../privileges');
|
|
||||||
|
|
||||||
module.exports = function (SocketUser) {
|
module.exports = function (SocketUser) {
|
||||||
SocketUser.changeUsernameEmail = function (socket, data, callback) {
|
SocketUser.changeUsernameEmail = async function (socket, data) {
|
||||||
if (!data || !data.uid || !socket.uid) {
|
if (!data || !data.uid || !socket.uid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
await isPrivilegedOrSelfAndPasswordMatch(socket, data);
|
||||||
async.waterfall([
|
return await SocketUser.updateProfile(socket, data);
|
||||||
function (next) {
|
|
||||||
isPrivilegedOrSelfAndPasswordMatch(socket, data, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
SocketUser.updateProfile(socket, data, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.updateCover = function (socket, data, callback) {
|
SocketUser.updateCover = async function (socket, data) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:no-privileges]]'));
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
async.waterfall([
|
await user.isAdminOrGlobalModOrSelf(socket.uid, data.uid);
|
||||||
function (next) {
|
await user.checkMinReputation(socket.uid, data.uid, 'min:rep:cover-picture');
|
||||||
user.isAdminOrGlobalModOrSelf(socket.uid, data.uid, next);
|
return await user.updateCoverPicture(data);
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
user.checkMinReputation(socket.uid, data.uid, 'min:rep:cover-picture', next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
user.updateCoverPicture(data, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.uploadCroppedPicture = function (socket, data, callback) {
|
SocketUser.uploadCroppedPicture = async function (socket, data) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:no-privileges]]'));
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
async.waterfall([
|
await user.isAdminOrGlobalModOrSelf(socket.uid, data.uid);
|
||||||
function (next) {
|
await user.checkMinReputation(socket.uid, data.uid, 'min:rep:profile-picture');
|
||||||
user.isAdminOrGlobalModOrSelf(socket.uid, data.uid, next);
|
return await user.uploadCroppedPicture(data);
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
user.checkMinReputation(socket.uid, data.uid, 'min:rep:profile-picture', next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
user.uploadCroppedPicture(data, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.removeCover = function (socket, data, callback) {
|
SocketUser.removeCover = async function (socket, data) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:no-privileges]]'));
|
throw new Error('[[error:no-privileges]]');
|
||||||
}
|
}
|
||||||
|
await user.isAdminOrGlobalModOrSelf(socket.uid, data.uid);
|
||||||
async.waterfall([
|
await user.removeCoverPicture(data);
|
||||||
function (next) {
|
|
||||||
user.isAdminOrGlobalModOrSelf(socket.uid, data.uid, next);
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
user.removeCoverPicture(data, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function isPrivilegedOrSelfAndPasswordMatch(socket, data, callback) {
|
async function isPrivilegedOrSelfAndPasswordMatch(socket, data) {
|
||||||
const uid = socket.uid;
|
const uid = socket.uid;
|
||||||
const isSelf = parseInt(uid, 10) === parseInt(data.uid, 10);
|
const isSelf = parseInt(uid, 10) === parseInt(data.uid, 10);
|
||||||
|
|
||||||
async.waterfall([
|
const [isAdmin, isTargetAdmin, isGlobalMod] = await Promise.all([
|
||||||
function (next) {
|
user.isAdministrator(uid),
|
||||||
async.parallel({
|
user.isAdministrator(data.uid),
|
||||||
isAdmin: async.apply(user.isAdministrator, uid),
|
user.isGlobalModerator(uid),
|
||||||
isTargetAdmin: async.apply(user.isAdministrator, data.uid),
|
]);
|
||||||
isGlobalMod: async.apply(user.isGlobalModerator, uid),
|
|
||||||
}, next);
|
if ((isTargetAdmin && !isAdmin) || (!isSelf && !(isAdmin || isGlobalMod))) {
|
||||||
},
|
throw new Error('[[error:no-privileges]]');
|
||||||
function (results, next) {
|
}
|
||||||
if (results.isTargetAdmin && !results.isAdmin) {
|
const [hasPassword, passwordMatch] = await Promise.all([
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
user.hasPassword(data.uid),
|
||||||
}
|
data.password ? user.isPasswordCorrect(data.uid, data.password, socket.ip) : false,
|
||||||
|
]);
|
||||||
if (!isSelf && !(results.isAdmin || results.isGlobalMod)) {
|
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
if (isSelf && hasPassword && !passwordMatch) {
|
||||||
}
|
throw new Error('[[error:invalid-password]]');
|
||||||
|
}
|
||||||
async.parallel({
|
|
||||||
hasPassword: async.apply(user.hasPassword, data.uid),
|
|
||||||
passwordMatch: function (next) {
|
|
||||||
if (data.password) {
|
|
||||||
user.isPasswordCorrect(data.uid, data.password, socket.ip, next);
|
|
||||||
} else {
|
|
||||||
next(null, false);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
}, function (results, next) {
|
|
||||||
if (isSelf && results.hasPassword && !results.passwordMatch) {
|
|
||||||
return next(new Error('[[error:invalid-password]]'));
|
|
||||||
}
|
|
||||||
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketUser.changePassword = function (socket, data, callback) {
|
SocketUser.changePassword = async function (socket, data) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:invalid-uid]]'));
|
throw new Error('[[error:invalid-uid]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data || !data.uid) {
|
if (!data || !data.uid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
async.waterfall([
|
await user.changePassword(socket.uid, Object.assign(data, { ip: socket.ip }));
|
||||||
function (next) {
|
await events.log({
|
||||||
user.changePassword(socket.uid, Object.assign(data, { ip: socket.ip }), next);
|
type: 'password-change',
|
||||||
},
|
uid: socket.uid,
|
||||||
function (next) {
|
targetUid: data.uid,
|
||||||
events.log({
|
ip: socket.ip,
|
||||||
type: 'password-change',
|
});
|
||||||
uid: socket.uid,
|
|
||||||
targetUid: data.uid,
|
|
||||||
ip: socket.ip,
|
|
||||||
});
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.updateProfile = function (socket, data, callback) {
|
SocketUser.updateProfile = async function (socket, data) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:invalid-uid]]'));
|
throw new Error('[[error:invalid-uid]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!data || !data.uid) {
|
if (!data || !data.uid) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
var oldUserData;
|
const oldUserData = await user.getUserFields(data.uid, ['email', 'username']);
|
||||||
async.waterfall([
|
if (!oldUserData || !oldUserData.username) {
|
||||||
function (next) {
|
throw new Error('[[error:invalid-data]]');
|
||||||
user.getUserFields(data.uid, ['email', 'username'], next);
|
}
|
||||||
},
|
|
||||||
function (_oldUserData, next) {
|
const [isAdminOrGlobalMod, canEdit] = await Promise.all([
|
||||||
oldUserData = _oldUserData;
|
user.isAdminOrGlobalMod(socket.uid),
|
||||||
if (!oldUserData || !oldUserData.username) {
|
privileges.users.canEdit(socket.uid, data.uid),
|
||||||
return next(new Error('[[error:invalid-data]]'));
|
]);
|
||||||
}
|
|
||||||
|
if (!canEdit) {
|
||||||
async.parallel({
|
throw new Error('[[error:no-privileges]]');
|
||||||
isAdminOrGlobalMod: function (next) {
|
}
|
||||||
user.isAdminOrGlobalMod(socket.uid, next);
|
|
||||||
},
|
if (!isAdminOrGlobalMod && meta.config['username:disableEdit']) {
|
||||||
canEdit: function (next) {
|
data.username = oldUserData.username;
|
||||||
privileges.users.canEdit(socket.uid, data.uid, next);
|
}
|
||||||
},
|
|
||||||
}, next);
|
if (!isAdminOrGlobalMod && meta.config['email:disableEdit']) {
|
||||||
},
|
data.email = oldUserData.email;
|
||||||
function (results, next) {
|
}
|
||||||
if (!results.canEdit) {
|
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
const userData = await user.updateProfile(socket.uid, data);
|
||||||
}
|
|
||||||
|
async function log(type, eventData) {
|
||||||
if (!results.isAdminOrGlobalMod && meta.config['username:disableEdit']) {
|
eventData.type = type;
|
||||||
data.username = oldUserData.username;
|
eventData.uid = socket.uid;
|
||||||
}
|
eventData.targetUid = data.uid;
|
||||||
|
eventData.ip = socket.ip;
|
||||||
if (!results.isAdminOrGlobalMod && meta.config['email:disableEdit']) {
|
await events.log(eventData);
|
||||||
data.email = oldUserData.email;
|
}
|
||||||
}
|
|
||||||
|
if (userData.email !== oldUserData.email) {
|
||||||
user.updateProfile(socket.uid, data, next);
|
await log('email-change', { oldEmail: oldUserData.email, newEmail: userData.email });
|
||||||
},
|
}
|
||||||
function (userData, next) {
|
|
||||||
function log(type, eventData) {
|
if (userData.username !== oldUserData.username) {
|
||||||
eventData.type = type;
|
await log('username-change', { oldUsername: oldUserData.username, newUsername: userData.username });
|
||||||
eventData.uid = socket.uid;
|
}
|
||||||
eventData.targetUid = data.uid;
|
return userData;
|
||||||
eventData.ip = socket.ip;
|
|
||||||
|
|
||||||
events.log(eventData);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (userData.email !== oldUserData.email) {
|
|
||||||
log('email-change', { oldEmail: oldUserData.email, newEmail: userData.email });
|
|
||||||
}
|
|
||||||
|
|
||||||
if (userData.username !== oldUserData.username) {
|
|
||||||
log('username-change', { oldUsername: oldUserData.username, newUsername: userData.username });
|
|
||||||
}
|
|
||||||
|
|
||||||
next(null, userData);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.toggleBlock = function (socket, data, callback) {
|
SocketUser.toggleBlock = async function (socket, data) {
|
||||||
let isBlocked;
|
const [is] = await Promise.all([
|
||||||
|
user.blocks.is(data.blockeeUid, data.blockerUid),
|
||||||
async.waterfall([
|
user.blocks.can(socket.uid, data.blockerUid, data.blockeeUid),
|
||||||
function (next) {
|
]);
|
||||||
async.parallel({
|
const isBlocked = is;
|
||||||
can: function (next) {
|
await user.blocks[isBlocked ? 'remove' : 'add'](data.blockeeUid, data.blockerUid);
|
||||||
user.blocks.can(socket.uid, data.blockerUid, data.blockeeUid, next);
|
return !isBlocked;
|
||||||
},
|
|
||||||
is: function (next) {
|
|
||||||
user.blocks.is(data.blockeeUid, data.blockerUid, next);
|
|
||||||
},
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (results, next) {
|
|
||||||
isBlocked = results.is;
|
|
||||||
user.blocks[isBlocked ? 'remove' : 'add'](data.blockeeUid, data.blockerUid, next);
|
|
||||||
},
|
|
||||||
], function (err) {
|
|
||||||
callback(err, !isBlocked);
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -1,70 +1,43 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const user = require('../../user');
|
||||||
var user = require('../../user');
|
const events = require('../../events');
|
||||||
var events = require('../../events');
|
|
||||||
|
|
||||||
module.exports = function (SocketUser) {
|
module.exports = function (SocketUser) {
|
||||||
SocketUser.acceptRegistration = function (socket, data, callback) {
|
SocketUser.acceptRegistration = async function (socket, data) {
|
||||||
async.waterfall([
|
const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(socket.uid);
|
||||||
function (next) {
|
if (!isAdminOrGlobalMod) {
|
||||||
user.isAdminOrGlobalMod(socket.uid, next);
|
throw new Error('[[error:no-privileges]]');
|
||||||
},
|
}
|
||||||
function (isAdminOrGlobalMod, next) {
|
const uid = await user.acceptRegistration(data.username);
|
||||||
if (!isAdminOrGlobalMod) {
|
await events.log({
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
type: 'registration-approved',
|
||||||
}
|
uid: socket.uid,
|
||||||
|
ip: socket.ip,
|
||||||
user.acceptRegistration(data.username, next);
|
targetUid: uid,
|
||||||
},
|
});
|
||||||
function (uid, next) {
|
return uid;
|
||||||
events.log({
|
|
||||||
type: 'registration-approved',
|
|
||||||
uid: socket.uid,
|
|
||||||
ip: socket.ip,
|
|
||||||
targetUid: uid,
|
|
||||||
});
|
|
||||||
next(null, uid);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.rejectRegistration = function (socket, data, callback) {
|
SocketUser.rejectRegistration = async function (socket, data) {
|
||||||
async.waterfall([
|
const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(socket.uid);
|
||||||
function (next) {
|
if (!isAdminOrGlobalMod) {
|
||||||
user.isAdminOrGlobalMod(socket.uid, next);
|
throw new Error('[[error:no-privileges]]');
|
||||||
},
|
}
|
||||||
function (isAdminOrGlobalMod, next) {
|
await user.rejectRegistration(data.username);
|
||||||
if (!isAdminOrGlobalMod) {
|
await events.log({
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
type: 'registration-rejected',
|
||||||
}
|
uid: socket.uid,
|
||||||
|
ip: socket.ip,
|
||||||
user.rejectRegistration(data.username, next);
|
username: data.username,
|
||||||
},
|
});
|
||||||
function (next) {
|
|
||||||
events.log({
|
|
||||||
type: 'registration-rejected',
|
|
||||||
uid: socket.uid,
|
|
||||||
ip: socket.ip,
|
|
||||||
username: data.username,
|
|
||||||
});
|
|
||||||
next();
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.deleteInvitation = function (socket, data, callback) {
|
SocketUser.deleteInvitation = async function (socket, data) {
|
||||||
async.waterfall([
|
const isAdminOrGlobalMod = await user.isAdminOrGlobalMod(socket.uid);
|
||||||
function (next) {
|
if (!isAdminOrGlobalMod) {
|
||||||
user.isAdminOrGlobalMod(socket.uid, next);
|
throw new Error('[[error:no-privileges]]');
|
||||||
},
|
}
|
||||||
function (isAdminOrGlobalMod, next) {
|
await user.deleteInvitation(data.invitedBy, data.email);
|
||||||
if (!isAdminOrGlobalMod) {
|
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
|
||||||
}
|
|
||||||
|
|
||||||
user.deleteInvitation(data.invitedBy, data.email, next);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -1,42 +1,31 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const user = require('../../user');
|
||||||
|
const pagination = require('../../pagination');
|
||||||
var user = require('../../user');
|
const privileges = require('../../privileges');
|
||||||
var pagination = require('../../pagination');
|
|
||||||
var privileges = require('../../privileges');
|
|
||||||
|
|
||||||
module.exports = function (SocketUser) {
|
module.exports = function (SocketUser) {
|
||||||
SocketUser.search = function (socket, data, callback) {
|
SocketUser.search = async function (socket, data) {
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return callback(new Error('[[error:invalid-data]]'));
|
throw new Error('[[error:invalid-data]]');
|
||||||
}
|
}
|
||||||
|
const allowed = await privileges.global.can('search:users', socket.uid);
|
||||||
async.waterfall([
|
if (!allowed) {
|
||||||
function (next) {
|
throw new Error('[[error:no-privileges]]');
|
||||||
privileges.global.can('search:users', socket.uid, next);
|
}
|
||||||
},
|
const result = await user.search({
|
||||||
function (allowed, next) {
|
query: data.query,
|
||||||
if (!allowed) {
|
page: data.page,
|
||||||
return next(new Error('[[error:no-privileges]]'));
|
searchBy: data.searchBy,
|
||||||
}
|
sortBy: data.sortBy,
|
||||||
user.search({
|
onlineOnly: data.onlineOnly,
|
||||||
query: data.query,
|
bannedOnly: data.bannedOnly,
|
||||||
page: data.page,
|
flaggedOnly: data.flaggedOnly,
|
||||||
searchBy: data.searchBy,
|
paginate: data.paginate,
|
||||||
sortBy: data.sortBy,
|
uid: socket.uid,
|
||||||
onlineOnly: data.onlineOnly,
|
});
|
||||||
bannedOnly: data.bannedOnly,
|
result.pagination = pagination.create(data.page, result.pageCount);
|
||||||
flaggedOnly: data.flaggedOnly,
|
result['route_users:' + data.sortBy] = true;
|
||||||
paginate: data.paginate,
|
return result;
|
||||||
uid: socket.uid,
|
|
||||||
}, next);
|
|
||||||
},
|
|
||||||
function (result, next) {
|
|
||||||
result.pagination = pagination.create(data.page, result.pageCount);
|
|
||||||
result['route_users:' + data.sortBy] = true;
|
|
||||||
next(null, result);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
@ -1,59 +1,40 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async');
|
const user = require('../../user');
|
||||||
|
const websockets = require('../index');
|
||||||
var user = require('../../user');
|
|
||||||
var websockets = require('../index');
|
|
||||||
|
|
||||||
module.exports = function (SocketUser) {
|
module.exports = function (SocketUser) {
|
||||||
SocketUser.checkStatus = function (socket, uid, callback) {
|
SocketUser.checkStatus = async function (socket, uid) {
|
||||||
if (!socket.uid) {
|
if (!socket.uid) {
|
||||||
return callback(new Error('[[error:invalid-uid]]'));
|
throw new Error('[[error:invalid-uid]]');
|
||||||
}
|
}
|
||||||
async.waterfall([
|
const userData = await user.getUserFields(uid, ['lastonline', 'status']);
|
||||||
function (next) {
|
return user.getStatus(userData);
|
||||||
user.getUserFields(uid, ['lastonline', 'status'], next);
|
|
||||||
},
|
|
||||||
function (userData, next) {
|
|
||||||
next(null, user.getStatus(userData));
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
SocketUser.setStatus = function (socket, status, callback) {
|
SocketUser.setStatus = async function (socket, status) {
|
||||||
if (socket.uid <= 0) {
|
if (socket.uid <= 0) {
|
||||||
return callback(new Error('[[error:invalid-uid]]'));
|
throw new Error('[[error:invalid-uid]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
var allowedStatus = ['online', 'offline', 'dnd', 'away'];
|
const allowedStatus = ['online', 'offline', 'dnd', 'away'];
|
||||||
if (!allowedStatus.includes(status)) {
|
if (!allowedStatus.includes(status)) {
|
||||||
return callback(new Error('[[error:invalid-user-status]]'));
|
throw new Error('[[error:invalid-user-status]]');
|
||||||
}
|
}
|
||||||
|
|
||||||
var data = { status: status };
|
const userData = { status: status };
|
||||||
if (status !== 'offline') {
|
if (status !== 'offline') {
|
||||||
data.lastonline = Date.now();
|
userData.lastonline = Date.now();
|
||||||
}
|
}
|
||||||
|
await user.setUserFields(socket.uid, userData);
|
||||||
async.waterfall([
|
if (status !== 'offline') {
|
||||||
function (next) {
|
await user.updateOnlineUsers(socket.uid);
|
||||||
user.setUserFields(socket.uid, data, next);
|
}
|
||||||
},
|
const eventData = {
|
||||||
function (next) {
|
uid: socket.uid,
|
||||||
if (status !== 'offline') {
|
status: status,
|
||||||
user.updateOnlineUsers(socket.uid, next);
|
};
|
||||||
} else {
|
websockets.server.emit('event:user_status_change', eventData);
|
||||||
next();
|
return eventData;
|
||||||
}
|
|
||||||
},
|
|
||||||
function (next) {
|
|
||||||
var data = {
|
|
||||||
uid: socket.uid,
|
|
||||||
status: status,
|
|
||||||
};
|
|
||||||
websockets.server.emit('event:user_status_change', data);
|
|
||||||
next(null, data);
|
|
||||||
},
|
|
||||||
], callback);
|
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue