mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-28 17:46:16 +01:00
refactor: async/await socket.io
This commit is contained in:
@@ -1,19 +1,18 @@
|
||||
'use strict';
|
||||
|
||||
var async = require('async');
|
||||
var validator = require('validator');
|
||||
const validator = require('validator');
|
||||
|
||||
var db = require('../database');
|
||||
var meta = require('../meta');
|
||||
var notifications = require('../notifications');
|
||||
var plugins = require('../plugins');
|
||||
var Messaging = require('../messaging');
|
||||
var utils = require('../utils');
|
||||
var server = require('./');
|
||||
var user = require('../user');
|
||||
var privileges = require('../privileges');
|
||||
const db = require('../database');
|
||||
const meta = require('../meta');
|
||||
const notifications = require('../notifications');
|
||||
const plugins = require('../plugins');
|
||||
const Messaging = require('../messaging');
|
||||
const utils = require('../utils');
|
||||
const server = require('./');
|
||||
const user = require('../user');
|
||||
const privileges = require('../privileges');
|
||||
|
||||
var SocketModules = module.exports;
|
||||
const SocketModules = module.exports;
|
||||
|
||||
SocketModules.chats = {};
|
||||
SocketModules.sounds = {};
|
||||
@@ -21,403 +20,276 @@ SocketModules.settings = {};
|
||||
|
||||
/* Chat */
|
||||
|
||||
SocketModules.chats.getRaw = function (socket, data, callback) {
|
||||
SocketModules.chats.getRaw = async function (socket, data) {
|
||||
if (!data || !data.hasOwnProperty('mid')) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Messaging.getMessageField(data.mid, 'roomId', next);
|
||||
},
|
||||
function (roomId, next) {
|
||||
async.parallel({
|
||||
isAdmin: function (next) {
|
||||
user.isAdministrator(socket.uid, next);
|
||||
},
|
||||
hasMessage: function (next) {
|
||||
db.isSortedSetMember('uid:' + socket.uid + ':chat:room:' + roomId + ':mids', data.mid, next);
|
||||
},
|
||||
inRoom: function (next) {
|
||||
Messaging.isUserInRoom(socket.uid, roomId, next);
|
||||
},
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
if (!results.isAdmin && (!results.inRoom || !results.hasMessage)) {
|
||||
return next(new Error('[[error:not-allowed]]'));
|
||||
}
|
||||
const roomId = await Messaging.getMessageField(data.mid, 'roomId');
|
||||
const [isAdmin, hasMessage, inRoom] = await Promise.all([
|
||||
user.isAdministrator(socket.uid),
|
||||
db.isSortedSetMember('uid:' + socket.uid + ':chat:room:' + roomId + ':mids', data.mid),
|
||||
Messaging.isUserInRoom(socket.uid, roomId),
|
||||
]);
|
||||
|
||||
Messaging.getMessageField(data.mid, 'content', next);
|
||||
},
|
||||
], callback);
|
||||
if (!isAdmin && (!inRoom || !hasMessage)) {
|
||||
throw new Error('[[error:not-allowed]]');
|
||||
}
|
||||
|
||||
return await Messaging.getMessageField(data.mid, 'content');
|
||||
};
|
||||
|
||||
SocketModules.chats.isDnD = function (socket, uid, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
db.getObjectField('user:' + uid, 'status', next);
|
||||
},
|
||||
function (status, next) {
|
||||
next(null, status === 'dnd');
|
||||
},
|
||||
], callback);
|
||||
SocketModules.chats.isDnD = async function (socket, uid) {
|
||||
const status = await db.getObjectField('user:' + uid, 'status');
|
||||
return status === 'dnd';
|
||||
};
|
||||
|
||||
SocketModules.chats.newRoom = function (socket, data, callback) {
|
||||
SocketModules.chats.newRoom = async function (socket, data) {
|
||||
if (!data) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
if (rateLimitExceeded(socket)) {
|
||||
return callback(new Error('[[error:too-many-messages]]'));
|
||||
throw new Error('[[error:too-many-messages]]');
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
privileges.global.can('chat', socket.uid, next);
|
||||
},
|
||||
function (canChat, next) {
|
||||
if (!canChat) {
|
||||
return next(new Error('[[error:no-privileges]]'));
|
||||
}
|
||||
Messaging.canMessageUser(socket.uid, data.touid, next);
|
||||
},
|
||||
function (next) {
|
||||
Messaging.newRoom(socket.uid, [data.touid], next);
|
||||
},
|
||||
], callback);
|
||||
const canChat = await privileges.global.can('chat', socket.uid);
|
||||
if (!canChat) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
await Messaging.canMessageUser(socket.uid, data.touid);
|
||||
return await Messaging.newRoom(socket.uid, [data.touid]);
|
||||
};
|
||||
|
||||
SocketModules.chats.send = function (socket, data, callback) {
|
||||
SocketModules.chats.send = async function (socket, data) {
|
||||
if (!data || !data.roomId || !socket.uid) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
if (rateLimitExceeded(socket)) {
|
||||
return callback(new Error('[[error:too-many-messages]]'));
|
||||
throw new Error('[[error:too-many-messages]]');
|
||||
}
|
||||
const canChat = await privileges.global.can('chat', socket.uid);
|
||||
if (!canChat) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
const results = await plugins.fireHook('filter:messaging.send', {
|
||||
data: data,
|
||||
uid: socket.uid,
|
||||
});
|
||||
data = results.data;
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
privileges.global.can('chat', socket.uid, next);
|
||||
},
|
||||
function (canChat, next) {
|
||||
if (!canChat) {
|
||||
return next(new Error('[[error:no-privileges]]'));
|
||||
}
|
||||
|
||||
plugins.fireHook('filter:messaging.send', {
|
||||
data: data,
|
||||
uid: socket.uid,
|
||||
}, function (err, results) {
|
||||
data = results.data;
|
||||
next(err);
|
||||
});
|
||||
},
|
||||
function (next) {
|
||||
Messaging.canMessageRoom(socket.uid, data.roomId, next);
|
||||
},
|
||||
function (next) {
|
||||
Messaging.sendMessage({
|
||||
uid: socket.uid,
|
||||
roomId: data.roomId,
|
||||
content: data.message,
|
||||
timestamp: Date.now(),
|
||||
ip: socket.ip,
|
||||
}, next);
|
||||
},
|
||||
function (message, next) {
|
||||
Messaging.notifyUsersInRoom(socket.uid, data.roomId, message);
|
||||
user.updateOnlineUsers(socket.uid);
|
||||
next(null, message);
|
||||
},
|
||||
], callback);
|
||||
await Messaging.canMessageRoom(socket.uid, data.roomId);
|
||||
const message = await Messaging.sendMessage({
|
||||
uid: socket.uid,
|
||||
roomId: data.roomId,
|
||||
content: data.message,
|
||||
timestamp: Date.now(),
|
||||
ip: socket.ip,
|
||||
});
|
||||
Messaging.notifyUsersInRoom(socket.uid, data.roomId, message);
|
||||
user.updateOnlineUsers(socket.uid);
|
||||
return message;
|
||||
};
|
||||
|
||||
function rateLimitExceeded(socket) {
|
||||
var now = Date.now();
|
||||
const now = Date.now();
|
||||
socket.lastChatMessageTime = socket.lastChatMessageTime || 0;
|
||||
if (now - socket.lastChatMessageTime < meta.config.chatMessageDelay) {
|
||||
return true;
|
||||
}
|
||||
socket.lastChatMessageTime = now;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
SocketModules.chats.loadRoom = function (socket, data, callback) {
|
||||
SocketModules.chats.loadRoom = async function (socket, data) {
|
||||
if (!data || !data.roomId) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
Messaging.loadRoom(socket.uid, data, callback);
|
||||
return await Messaging.loadRoom(socket.uid, data);
|
||||
};
|
||||
|
||||
SocketModules.chats.getUsersInRoom = function (socket, data, callback) {
|
||||
SocketModules.chats.getUsersInRoom = async function (socket, data) {
|
||||
if (!data || !data.roomId) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
const [userData, isOwner] = await Promise.all([
|
||||
Messaging.getUsersInRoom(data.roomId, 0, -1),
|
||||
Messaging.isRoomOwner(socket.uid, data.roomId),
|
||||
]);
|
||||
|
||||
userData.forEach((user) => {
|
||||
user.canKick = (parseInt(user.uid, 10) !== parseInt(socket.uid, 10)) && isOwner;
|
||||
});
|
||||
return userData;
|
||||
};
|
||||
|
||||
SocketModules.chats.addUserToRoom = async function (socket, data) {
|
||||
if (!data || !data.roomId || !data.username) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
async.parallel({
|
||||
users: async.apply(Messaging.getUsersInRoom, data.roomId, 0, -1),
|
||||
isOwner: async.apply(Messaging.isRoomOwner, socket.uid, data.roomId),
|
||||
}, function (err, payload) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
const canChat = await privileges.global.can('chat', socket.uid);
|
||||
if (!canChat) {
|
||||
throw new Error('[[error:no-privileges]]');
|
||||
}
|
||||
|
||||
payload.users = payload.users.map((user) => {
|
||||
user.canKick = (parseInt(user.uid, 10) !== parseInt(socket.uid, 10)) && payload.isOwner;
|
||||
return user;
|
||||
});
|
||||
const userCount = await Messaging.getUserCountInRoom(data.roomId);
|
||||
const maxUsers = meta.config.maximumUsersInChatRoom;
|
||||
if (maxUsers && userCount >= maxUsers) {
|
||||
throw new Error('[[error:cant-add-more-users-to-chat-room]]');
|
||||
}
|
||||
|
||||
callback(null, payload.users);
|
||||
const uid = await user.getUidByUsername(data.username);
|
||||
if (!uid) {
|
||||
throw new Error('[[error:no-user]]');
|
||||
}
|
||||
if (socket.uid === parseInt(uid, 10)) {
|
||||
throw new Error('[[error:cant-chat-with-yourself]]');
|
||||
}
|
||||
const [settings, isAdminOrGlobalMod, isFollowing] = await Promise.all([
|
||||
user.getSettings(uid),
|
||||
user.isAdminOrGlobalMod(socket.uid),
|
||||
user.isFollowing(uid, socket.uid),
|
||||
]);
|
||||
|
||||
if (settings.restrictChat && !isAdminOrGlobalMod && !isFollowing) {
|
||||
throw new Error('[[error:chat-restricted]]');
|
||||
}
|
||||
|
||||
await Messaging.addUsersToRoom(socket.uid, [uid], data.roomId);
|
||||
};
|
||||
|
||||
SocketModules.chats.removeUserFromRoom = async function (socket, data) {
|
||||
if (!data || !data.roomId) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
const exists = await user.exists(data.uid);
|
||||
if (!exists) {
|
||||
throw new Error('[[error:no-user]]');
|
||||
}
|
||||
|
||||
await Messaging.removeUsersFromRoom(socket.uid, [data.uid], data.roomId);
|
||||
};
|
||||
|
||||
SocketModules.chats.leave = async function (socket, roomid) {
|
||||
if (!socket.uid || !roomid) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
await Messaging.leaveRoom([socket.uid], roomid);
|
||||
};
|
||||
|
||||
SocketModules.chats.edit = async function (socket, data) {
|
||||
if (!data || !data.roomId || !data.message) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
await Messaging.canEdit(data.mid, socket.uid);
|
||||
await Messaging.editMessage(socket.uid, data.mid, data.roomId, data.message);
|
||||
};
|
||||
|
||||
SocketModules.chats.delete = async function (socket, data) {
|
||||
if (!data || !data.roomId || !data.messageId) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
await Messaging.canDelete(data.messageId, socket.uid);
|
||||
await Messaging.deleteMessage(data.messageId);
|
||||
};
|
||||
|
||||
SocketModules.chats.restore = async function (socket, data) {
|
||||
if (!data || !data.roomId || !data.messageId) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
await Messaging.canDelete(data.messageId, socket.uid);
|
||||
await Messaging.restoreMessage(data.messageId);
|
||||
};
|
||||
|
||||
SocketModules.chats.canMessage = async function (socket, roomId) {
|
||||
await Messaging.canMessageRoom(socket.uid, roomId);
|
||||
};
|
||||
|
||||
SocketModules.chats.markRead = async function (socket, roomId) {
|
||||
if (!socket.uid || !roomId) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
const [uidsInRoom] = await Promise.all([
|
||||
Messaging.getUidsInRoom(roomId, 0, -1),
|
||||
Messaging.markRead(socket.uid, roomId),
|
||||
]);
|
||||
|
||||
Messaging.pushUnreadCount(socket.uid);
|
||||
server.in('uid_' + socket.uid).emit('event:chats.markedAsRead', { roomId: roomId });
|
||||
|
||||
if (!uidsInRoom.includes(String(socket.uid))) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Mark notification read
|
||||
const nids = uidsInRoom.filter(uid => parseInt(uid, 10) !== socket.uid)
|
||||
.map(uid => 'chat_' + uid + '_' + roomId);
|
||||
|
||||
await notifications.markReadMultiple(nids, socket.uid);
|
||||
await user.notifications.pushCount(socket.uid);
|
||||
};
|
||||
|
||||
SocketModules.chats.markAllRead = async function (socket) {
|
||||
await Messaging.markAllRead(socket.uid);
|
||||
Messaging.pushUnreadCount(socket.uid);
|
||||
};
|
||||
|
||||
SocketModules.chats.renameRoom = async function (socket, data) {
|
||||
if (!data || !data.roomId || !data.newName) {
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
await Messaging.renameRoom(socket.uid, data.roomId, data.newName);
|
||||
const uids = await Messaging.getUidsInRoom(data.roomId, 0, -1);
|
||||
const eventData = { roomId: data.roomId, newName: validator.escape(String(data.newName)) };
|
||||
uids.forEach(function (uid) {
|
||||
server.in('uid_' + uid).emit('event:chats.roomRename', eventData);
|
||||
});
|
||||
};
|
||||
|
||||
SocketModules.chats.addUserToRoom = function (socket, data, callback) {
|
||||
if (!data || !data.roomId || !data.username) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
var uid;
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
privileges.global.can('chat', socket.uid, next);
|
||||
},
|
||||
function (canChat, next) {
|
||||
if (!canChat) {
|
||||
return next(new Error('[[error:no-privileges]]'));
|
||||
}
|
||||
|
||||
Messaging.getUserCountInRoom(data.roomId, next);
|
||||
},
|
||||
function (userCount, next) {
|
||||
var maxUsers = meta.config.maximumUsersInChatRoom;
|
||||
if (maxUsers && userCount >= maxUsers) {
|
||||
return next(new Error('[[error:cant-add-more-users-to-chat-room]]'));
|
||||
}
|
||||
next();
|
||||
},
|
||||
function (next) {
|
||||
user.getUidByUsername(data.username, next);
|
||||
},
|
||||
function (_uid, next) {
|
||||
uid = _uid;
|
||||
if (!uid) {
|
||||
return next(new Error('[[error:no-user]]'));
|
||||
}
|
||||
if (socket.uid === parseInt(uid, 10)) {
|
||||
return next(new Error('[[error:cant-chat-with-yourself]]'));
|
||||
}
|
||||
async.parallel({
|
||||
settings: async.apply(user.getSettings, uid),
|
||||
isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, socket.uid),
|
||||
isFollowing: async.apply(user.isFollowing, uid, socket.uid),
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
if (results.settings.restrictChat && !results.isAdminOrGlobalMod && !results.isFollowing) {
|
||||
return next(new Error('[[error:chat-restricted]]'));
|
||||
}
|
||||
|
||||
Messaging.addUsersToRoom(socket.uid, [uid], data.roomId, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.removeUserFromRoom = function (socket, data, callback) {
|
||||
if (!data || !data.roomId) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
user.exists(data.uid, next);
|
||||
},
|
||||
function (exists, next) {
|
||||
if (!exists) {
|
||||
return next(new Error('[[error:no-user]]'));
|
||||
}
|
||||
|
||||
Messaging.removeUsersFromRoom(socket.uid, [data.uid], data.roomId, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.leave = function (socket, roomid, callback) {
|
||||
if (!socket.uid || !roomid) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
Messaging.leaveRoom([socket.uid], roomid, callback);
|
||||
};
|
||||
|
||||
|
||||
SocketModules.chats.edit = function (socket, data, callback) {
|
||||
if (!data || !data.roomId || !data.message) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Messaging.canEdit(data.mid, socket.uid, next);
|
||||
},
|
||||
function (next) {
|
||||
Messaging.editMessage(socket.uid, data.mid, data.roomId, data.message, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.delete = function (socket, data, callback) {
|
||||
if (!data || !data.roomId || !data.messageId) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Messaging.canDelete(data.messageId, socket.uid, next);
|
||||
},
|
||||
function (next) {
|
||||
Messaging.deleteMessage(data.messageId, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.restore = function (socket, data, callback) {
|
||||
if (!data || !data.roomId || !data.messageId) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Messaging.canDelete(data.messageId, socket.uid, next);
|
||||
},
|
||||
function (next) {
|
||||
Messaging.restoreMessage(data.messageId, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.canMessage = function (socket, roomId, callback) {
|
||||
Messaging.canMessageRoom(socket.uid, roomId, callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.markRead = function (socket, roomId, callback) {
|
||||
if (!socket.uid || !roomId) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
async.parallel({
|
||||
uidsInRoom: async.apply(Messaging.getUidsInRoom, roomId, 0, -1),
|
||||
markRead: async.apply(Messaging.markRead, socket.uid, roomId),
|
||||
}, next);
|
||||
},
|
||||
function (results, next) {
|
||||
Messaging.pushUnreadCount(socket.uid);
|
||||
server.in('uid_' + socket.uid).emit('event:chats.markedAsRead', { roomId: roomId });
|
||||
|
||||
if (!results.uidsInRoom.includes(String(socket.uid))) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
// Mark notification read
|
||||
var nids = results.uidsInRoom.filter(function (uid) {
|
||||
return parseInt(uid, 10) !== socket.uid;
|
||||
}).map(function (uid) {
|
||||
return 'chat_' + uid + '_' + roomId;
|
||||
});
|
||||
|
||||
notifications.markReadMultiple(nids, socket.uid, function () {
|
||||
user.notifications.pushCount(socket.uid);
|
||||
});
|
||||
|
||||
next();
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.markAllRead = function (socket, data, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Messaging.markAllRead(socket.uid, next);
|
||||
},
|
||||
function (next) {
|
||||
Messaging.pushUnreadCount(socket.uid);
|
||||
next();
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.renameRoom = function (socket, data, callback) {
|
||||
if (!data || !data.roomId || !data.newName) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Messaging.renameRoom(socket.uid, data.roomId, data.newName, next);
|
||||
},
|
||||
function (next) {
|
||||
Messaging.getUidsInRoom(data.roomId, 0, -1, next);
|
||||
},
|
||||
function (uids, next) {
|
||||
var eventData = { roomId: data.roomId, newName: validator.escape(String(data.newName)) };
|
||||
uids.forEach(function (uid) {
|
||||
server.in('uid_' + uid).emit('event:chats.roomRename', eventData);
|
||||
});
|
||||
next();
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.getRecentChats = function (socket, data, callback) {
|
||||
SocketModules.chats.getRecentChats = async function (socket, data) {
|
||||
if (!data || !utils.isNumber(data.after) || !utils.isNumber(data.uid)) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
var start = parseInt(data.after, 10);
|
||||
var stop = start + 9;
|
||||
Messaging.getRecentChats(socket.uid, data.uid, start, stop, callback);
|
||||
const start = parseInt(data.after, 10);
|
||||
const stop = start + 9;
|
||||
return await Messaging.getRecentChats(socket.uid, data.uid, start, stop);
|
||||
};
|
||||
|
||||
SocketModules.chats.hasPrivateChat = function (socket, uid, callback) {
|
||||
SocketModules.chats.hasPrivateChat = async function (socket, uid) {
|
||||
if (socket.uid <= 0 || uid <= 0) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
Messaging.hasPrivateChat(socket.uid, uid, callback);
|
||||
return await Messaging.hasPrivateChat(socket.uid, uid);
|
||||
};
|
||||
|
||||
SocketModules.chats.getMessages = function (socket, data, callback) {
|
||||
SocketModules.chats.getMessages = async function (socket, data) {
|
||||
if (!socket.uid || !data || !data.uid || !data.roomId) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
throw new Error('[[error:invalid-data]]');
|
||||
}
|
||||
|
||||
var params = {
|
||||
return await Messaging.getMessages({
|
||||
callerUid: socket.uid,
|
||||
uid: data.uid,
|
||||
roomId: data.roomId,
|
||||
start: parseInt(data.start, 10) || 0,
|
||||
count: 50,
|
||||
};
|
||||
|
||||
Messaging.getMessages(params, callback);
|
||||
});
|
||||
};
|
||||
|
||||
SocketModules.chats.getIP = function (socket, mid, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
user.isAdminOrGlobalMod(socket.uid, next);
|
||||
},
|
||||
function (allowed, next) {
|
||||
if (!allowed) {
|
||||
return next(new Error('[[error:no-privilege]]'));
|
||||
}
|
||||
Messaging.getMessageField(mid, 'ip', next);
|
||||
},
|
||||
], callback);
|
||||
SocketModules.chats.getIP = async function (socket, mid) {
|
||||
const allowed = await user.isAdminOrGlobalMod(socket.uid);
|
||||
if (!allowed) {
|
||||
throw new Error('[[error:no-privilege]]');
|
||||
}
|
||||
return await Messaging.getMessageField(mid, 'ip');
|
||||
};
|
||||
|
||||
/* Sounds */
|
||||
SocketModules.sounds.getUserSoundMap = function getUserSoundMap(socket, data, callback) {
|
||||
meta.sounds.getUserSoundMap(socket.uid, callback);
|
||||
SocketModules.sounds.getUserSoundMap = async function getUserSoundMap(socket) {
|
||||
return await meta.sounds.getUserSoundMap(socket.uid);
|
||||
};
|
||||
|
||||
require('../promisify')(SocketModules);
|
||||
|
||||
Reference in New Issue
Block a user