mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-28 17:46:16 +01:00
more tests
This commit is contained in:
@@ -12,16 +12,16 @@ var utils = require('../utils');
|
||||
var server = require('./');
|
||||
var user = require('../user');
|
||||
|
||||
var SocketModules = {
|
||||
chats: {},
|
||||
sounds: {},
|
||||
settings: {},
|
||||
};
|
||||
var SocketModules = module.exports;
|
||||
|
||||
SocketModules.chats = {};
|
||||
SocketModules.sounds = {};
|
||||
SocketModules.settings = {};
|
||||
|
||||
/* Chat */
|
||||
|
||||
SocketModules.chats.getRaw = function (socket, data, callback) {
|
||||
if (!data || !data.hasOwnProperty('mid')) {
|
||||
if (!data || !data.hasOwnProperty('mid') || !data.hasOwnProperty('roomId')) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
async.waterfall([
|
||||
@@ -57,13 +57,14 @@ SocketModules.chats.newRoom = function (socket, data, callback) {
|
||||
return callback(new Error('[[error:too-many-messages]]'));
|
||||
}
|
||||
|
||||
Messaging.canMessageUser(socket.uid, data.touid, function (err) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
|
||||
Messaging.newRoom(socket.uid, [data.touid], callback);
|
||||
});
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Messaging.canMessageUser(socket.uid, data.touid, next);
|
||||
},
|
||||
function (next) {
|
||||
Messaging.newRoom(socket.uid, [data.touid], next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.send = function (socket, data, callback) {
|
||||
@@ -223,17 +224,21 @@ SocketModules.chats.leave = function (socket, roomid, callback) {
|
||||
|
||||
|
||||
SocketModules.chats.edit = function (socket, data, callback) {
|
||||
if (!data || !data.roomId) {
|
||||
if (!data || !data.roomId || !data.message) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
Messaging.canEdit(data.mid, socket.uid, function (err, allowed) {
|
||||
if (err || !allowed) {
|
||||
return callback(err || new Error('[[error:cant-edit-chat-message]]'));
|
||||
}
|
||||
|
||||
Messaging.editMessage(socket.uid, data.mid, data.roomId, data.message, callback);
|
||||
});
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Messaging.canEdit(data.mid, socket.uid, next);
|
||||
},
|
||||
function (allowed, next) {
|
||||
if (!allowed) {
|
||||
return next(new Error('[[error:cant-edit-chat-message]]'));
|
||||
}
|
||||
Messaging.editMessage(socket.uid, data.mid, data.roomId, data.message, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.delete = function (socket, data, callback) {
|
||||
@@ -241,13 +246,18 @@ SocketModules.chats.delete = function (socket, data, callback) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
Messaging.canEdit(data.messageId, socket.uid, function (err, allowed) {
|
||||
if (err || !allowed) {
|
||||
return callback(err || new Error('[[error:cant-delete-chat-message]]'));
|
||||
}
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
Messaging.canEdit(data.messageId, socket.uid, next);
|
||||
},
|
||||
function (allowed, next) {
|
||||
if (!allowed) {
|
||||
return next(new Error('[[error:cant-delete-chat-message]]'));
|
||||
}
|
||||
|
||||
Messaging.deleteMessage(data.messageId, data.roomId, callback);
|
||||
});
|
||||
Messaging.deleteMessage(data.messageId, data.roomId, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.canMessage = function (socket, roomId, callback) {
|
||||
@@ -255,37 +265,38 @@ SocketModules.chats.canMessage = function (socket, roomId, callback) {
|
||||
};
|
||||
|
||||
SocketModules.chats.markRead = function (socket, roomId, callback) {
|
||||
if (!socket.uid) {
|
||||
if (!socket.uid || !roomId) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
async.parallel({
|
||||
uidsInRoom: async.apply(Messaging.getUidsInRoom, roomId, 0, -1),
|
||||
markRead: async.apply(Messaging.markRead, socket.uid, roomId),
|
||||
}, function (err, results) {
|
||||
if (err) {
|
||||
return callback(err);
|
||||
}
|
||||
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 });
|
||||
|
||||
Messaging.pushUnreadCount(socket.uid);
|
||||
server.in('uid_' + socket.uid).emit('event:chats.markedAsRead', { roomId: roomId });
|
||||
if (results.uidsInRoom.indexOf(socket.uid.toString()) === -1) {
|
||||
return callback();
|
||||
}
|
||||
|
||||
if (results.uidsInRoom.indexOf(socket.uid.toString()) === -1) {
|
||||
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;
|
||||
});
|
||||
|
||||
// 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);
|
||||
});
|
||||
|
||||
notifications.markReadMultiple(nids, socket.uid, function () {
|
||||
user.notifications.pushCount(socket.uid);
|
||||
});
|
||||
|
||||
callback();
|
||||
});
|
||||
next();
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.markAllRead = function (socket, data, callback) {
|
||||
@@ -301,8 +312,8 @@ SocketModules.chats.markAllRead = function (socket, data, callback) {
|
||||
};
|
||||
|
||||
SocketModules.chats.renameRoom = function (socket, data, callback) {
|
||||
if (!data) {
|
||||
return callback(new Error('[[error:invalid-name]]'));
|
||||
if (!data || !data.roomId || !data.newName) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
@@ -333,13 +344,13 @@ SocketModules.chats.getRecentChats = function (socket, data, callback) {
|
||||
|
||||
SocketModules.chats.hasPrivateChat = function (socket, uid, callback) {
|
||||
if (!socket.uid || !uid) {
|
||||
return callback(null, new Error('[[error:invalid-data]]'));
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
Messaging.hasPrivateChat(socket.uid, uid, callback);
|
||||
};
|
||||
|
||||
SocketModules.chats.getMessages = function (socket, data, callback) {
|
||||
if (!socket.uid || !data.uid || !data.roomId) {
|
||||
if (!socket.uid || !data || !data.uid || !data.roomId) {
|
||||
return callback(new Error('[[error:invalid-data]]'));
|
||||
}
|
||||
|
||||
@@ -358,5 +369,3 @@ SocketModules.chats.getMessages = function (socket, data, callback) {
|
||||
SocketModules.sounds.getUserSoundMap = function getUserSoundMap(socket, data, callback) {
|
||||
meta.sounds.getUserSoundMap(socket.uid, callback);
|
||||
};
|
||||
|
||||
module.exports = SocketModules;
|
||||
|
||||
Reference in New Issue
Block a user