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

360 lines
9.9 KiB
JavaScript
Raw Normal View History

2014-01-10 16:00:03 -05:00
"use strict";
var async = require('async');
2015-12-23 11:27:34 +02:00
var validator = require('validator');
2015-12-21 10:36:44 +02:00
var meta = require('../meta');
2016-01-18 13:57:02 -05:00
var notifications = require('../notifications');
2016-07-13 17:33:04 -04:00
var plugins = require('../plugins');
var Messaging = require('../messaging');
var utils = require('../../public/src/utils');
var server = require('./');
var user = require('../user');
var SocketModules = {
2016-10-02 15:17:22 +03:00
chats: {},
sounds: {},
settings: {}
};
2014-01-10 16:00:03 -05:00
/* Chat */
SocketModules.chats.getRaw = function (socket, data, callback) {
if (!data || !data.hasOwnProperty('mid')) {
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
return callback(new Error('[[error:invalid-data]]'));
}
async.waterfall([
function (next) {
Messaging.isUserInRoom(socket.uid, data.roomId, next);
},
function (inRoom, next) {
if (!inRoom) {
return next(new Error('[[error:not-allowed]]'));
}
Messaging.getMessageField(data.mid, 'content', next);
}
], callback);
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
};
SocketModules.chats.newRoom = function (socket, data, callback) {
2015-12-16 11:15:43 +02:00
if (!data) {
return callback(new Error('[[error:invalid-data]]'));
}
2016-09-07 18:48:00 +03:00
if (rateLimitExceeded(socket)) {
2015-12-16 11:15:43 +02:00
return callback(new Error('[[error:too-many-messages]]'));
}
Messaging.canMessageUser(socket.uid, data.touid, function (err) {
2016-02-29 10:36:20 +02:00
if (err) {
return callback(err);
2015-12-16 11:15:43 +02:00
}
Messaging.newRoom(socket.uid, [data.touid], callback);
2015-12-16 11:15:43 +02:00
});
};
SocketModules.chats.send = function (socket, data, callback) {
2016-10-03 20:35:36 +03:00
if (!data || !data.roomId || !socket.uid) {
2014-04-09 21:36:57 -04:00
return callback(new Error('[[error:invalid-data]]'));
2014-01-17 12:55:38 -05:00
}
2014-01-10 16:00:03 -05:00
2016-09-07 18:48:00 +03:00
if (rateLimitExceeded(socket)) {
2014-10-15 14:49:11 -04:00
return callback(new Error('[[error:too-many-messages]]'));
}
2016-02-01 21:09:35 +02:00
async.waterfall([
2016-07-13 17:33:04 -04:00
function (next) {
plugins.fireHook('filter:messaging.send', {
data: data,
uid: socket.uid
}, function (err, results) {
2016-07-13 17:33:04 -04:00
data = results.data;
next(err);
});
},
2016-02-01 21:09:35 +02:00
function (next) {
Messaging.canMessageRoom(socket.uid, data.roomId, next);
},
function (next) {
2016-09-07 18:48:00 +03:00
Messaging.sendMessage(socket.uid, data.roomId, data.message, Date.now(), next);
2016-02-01 21:09:35 +02:00
},
function (message, next) {
2015-12-16 11:15:43 +02:00
Messaging.notifyUsersInRoom(socket.uid, data.roomId, message);
2016-02-01 21:09:35 +02:00
user.updateOnlineUsers(socket.uid);
2016-11-08 19:59:19 +03:00
next(null, message);
2016-02-01 21:09:35 +02:00
}
], callback);
2014-10-30 17:50:07 -04:00
};
2014-10-03 15:28:46 -04:00
2016-09-07 18:48:00 +03:00
function rateLimitExceeded(socket) {
var now = Date.now();
socket.lastChatMessageTime = socket.lastChatMessageTime || 0;
var delay = meta.config.hasOwnProperty('chatMessageDelay') ? parseInt(meta.config.chatMessageDelay, 10) : 200;
if (now - socket.lastChatMessageTime < delay) {
return true;
} else {
socket.lastChatMessageTime = now;
}
return false;
}
SocketModules.chats.loadRoom = function (socket, data, callback) {
if (!data || !data.roomId) {
return callback(new Error('[[error:invalid-data]]'));
}
2015-12-23 11:27:34 +02:00
async.waterfall([
function (next) {
2016-10-03 20:35:36 +03:00
Messaging.isUserInRoom(socket.uid, data.roomId, next);
2015-12-23 11:27:34 +02:00
},
2016-10-03 20:35:36 +03:00
function (inRoom, next) {
if (!inRoom) {
2015-12-23 11:27:34 +02:00
return next(new Error('[[error:not-allowed]]'));
}
async.parallel({
2015-12-23 11:27:34 +02:00
roomData: async.apply(Messaging.getRoomData, data.roomId),
2016-10-03 22:18:47 +03:00
users: async.apply(Messaging.getUsersInRoom, data.roomId, 0, -1),
messages: async.apply(Messaging.getMessages, {
callerUid: socket.uid,
uid: data.uid || socket.uid,
roomId: data.roomId,
isNew: false
}),
}, next);
},
function (results, next) {
2015-12-23 11:27:34 +02:00
results.roomData.users = results.users;
2016-10-14 13:22:30 +03:00
results.roomData.usernames = Messaging.generateUsernames(results.users, socket.uid);
2016-10-03 22:18:47 +03:00
results.roomData.messages = results.messages;
2016-09-30 13:18:29 +03:00
results.roomData.groupChat = results.roomData.hasOwnProperty('groupChat') ? results.roomData.groupChat : results.users.length > 2;
2015-12-23 11:27:34 +02:00
results.roomData.isOwner = parseInt(results.roomData.owner, 10) === socket.uid;
2015-12-24 09:52:25 +02:00
results.roomData.maximumUsersInChatRoom = parseInt(meta.config.maximumUsersInChatRoom, 10) || 0;
results.roomData.showUserInput = !results.roomData.maximumUsersInChatRoom || results.roomData.maximumUsersInChatRoom > 2;
2015-12-23 11:27:34 +02:00
next(null, results.roomData);
}
2016-01-19 12:02:51 +02:00
], callback);
};
SocketModules.chats.addUserToRoom = function (socket, data, callback) {
if (!data || !data.roomId || !data.username) {
return callback(new Error('[[error:invalid-data]]'));
}
var uid;
async.waterfall([
2015-12-24 09:52:25 +02:00
function (next) {
Messaging.getUserCountInRoom(data.roomId, next);
},
function (userCount, next) {
var maxUsers = parseInt(meta.config.maximumUsersInChatRoom, 10) || 0;
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-add-self-to-chat-room]]'));
}
async.parallel({
settings: async.apply(user.getSettings, uid),
2016-10-22 10:24:25 +03:00
isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, socket.uid),
isFollowing: async.apply(user.isFollowing, uid, socket.uid)
}, next);
},
function (results, next) {
2016-10-22 10:24:25 +03:00
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.getUidByUsername(data.username, next);
},
function (uid, next) {
if (!uid) {
return next(new Error('[[error:no-user]]'));
}
Messaging.removeUsersFromRoom(socket.uid, [uid], data.roomId, next);
}
], callback);
};
SocketModules.chats.leave = function (socket, roomid, callback) {
2016-01-08 12:34:09 +02:00
if (!socket.uid || !roomid) {
return callback(new Error('[[error:invalid-data]]'));
}
Messaging.leaveRoom([socket.uid], roomid, callback);
};
SocketModules.chats.edit = function (socket, data, callback) {
2015-12-15 19:39:45 +02:00
if (!data || !data.roomId) {
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
return callback(new Error('[[error:invalid-data]]'));
}
Messaging.canEdit(data.mid, socket.uid, function (err, allowed) {
2015-12-15 19:39:45 +02:00
if (err || !allowed) {
return callback(err || new Error('[[error:cant-edit-chat-message]]'));
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
}
2015-12-15 19:39:45 +02:00
Messaging.editMessage(socket.uid, data.mid, data.roomId, data.message, callback);
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
});
};
SocketModules.chats.delete = function (socket, data, callback) {
2015-12-15 14:10:32 +02:00
if (!data || !data.roomId || !data.messageId) {
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
return callback(new Error('[[error:invalid-data]]'));
}
Messaging.canEdit(data.messageId, socket.uid, function (err, allowed) {
2015-12-17 12:05:35 +02:00
if (err || !allowed) {
return callback(err || new Error('[[error:cant-delete-chat-message]]'));
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
}
2015-12-17 12:05:35 +02:00
Messaging.deleteMessage(data.messageId, data.roomId, callback);
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
});
2015-12-16 11:15:43 +02:00
};
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:51:23 2015 -0500 Fixed issue where newSet calculations were off ... sometimes. Also, rendering of edited messages now parses a template partial, instead of just replacing the content. commit 5cb6ca600425ca9320c599b32306e93dcc5aa4ce Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <julian@designcreateplay.com> Date: Fri Dec 11 11:05:08 2015 -0500 some linting and fixed issue where new msgs when deleted would crash server commit db4a9e40d6dff44569c2437378121db8fdf75cf8 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 17:25:56 2015 -0500 Message deletion for #2668, and fixed bug Fixed bug where chat modal would spawn even though user was sitting on the /chats page. commit a5aa2498ab4a8bba02a6daa43a9dbed7b3e37976 Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:20:39 2015 -0500 added indicator to show if and when a message had been edited commit e8301132d525c1b9fd46c98cdb282ac7ea7a0d7f Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <julian@designcreateplay.com> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
2015-12-11 12:07:02 -05:00
SocketModules.chats.canMessage = function (socket, roomId, callback) {
2016-02-01 21:09:35 +02:00
Messaging.canMessageRoom(socket.uid, roomId, callback);
2014-01-10 16:00:03 -05:00
};
SocketModules.chats.markRead = function (socket, roomId, callback) {
2016-10-03 20:35:36 +03:00
if (!socket.uid) {
return callback(new Error('[[error:invalid-data]]'));
}
2016-01-19 12:02:51 +02:00
async.parallel({
usersInRoom: async.apply(Messaging.getUidsInRoom, roomId, 0, -1),
markRead: async.apply(Messaging.markRead, socket.uid, roomId)
}, function (err, results) {
2015-10-02 17:31:18 -04:00
if (err) {
return callback(err);
2014-07-19 10:33:27 -04:00
}
2015-10-02 17:31:18 -04:00
Messaging.pushUnreadCount(socket.uid);
2016-02-01 21:09:35 +02:00
2016-01-19 12:02:51 +02:00
// Mark notification read
var nids = results.usersInRoom.filter(function (uid) {
2016-01-19 12:02:51 +02:00
return parseInt(uid, 10) !== socket.uid;
}).map(function (uid) {
2016-01-19 12:02:51 +02:00
return 'chat_' + uid + '_' + roomId;
});
2016-02-01 21:09:35 +02:00
notifications.markReadMultiple(nids, socket.uid, function () {
2016-01-19 12:02:51 +02:00
user.notifications.pushCount(socket.uid);
});
2016-02-01 21:09:35 +02:00
2016-03-21 12:57:44 -04:00
server.in('uid_' + socket.uid).emit('event:chats.markedAsRead', {roomId: roomId});
2015-10-02 17:31:18 -04:00
callback();
2014-07-19 10:33:27 -04:00
});
};
SocketModules.chats.markAllRead = function (socket, data, callback) {
2016-03-11 13:38:52 +02:00
async.waterfall([
function (next) {
Messaging.markAllRead(socket.uid, next);
},
function (next) {
Messaging.pushUnreadCount(socket.uid);
next();
}
], callback);
};
SocketModules.chats.renameRoom = function (socket, data, callback) {
2015-12-23 11:27:34 +02:00
if (!data) {
return callback(new Error('[[error:invalid-name]]'));
}
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) {
2015-12-23 11:27:34 +02:00
server.in('uid_' + uid).emit('event:chats.roomRename', eventData);
});
next();
}
], callback);
};
SocketModules.chats.getRecentChats = function (socket, data, callback) {
2016-10-03 20:35:36 +03:00
if (!data || !utils.isNumber(data.after) || !utils.isNumber(data.uid)) {
2014-09-15 16:26:25 -04:00
return callback(new Error('[[error:invalid-data]]'));
}
2016-09-20 14:41:53 +03:00
var start = parseInt(data.after, 10);
var stop = start + 9;
2016-10-03 20:35:36 +03:00
Messaging.getRecentChats(socket.uid, data.uid, start, stop, callback);
2014-01-10 16:00:03 -05:00
};
SocketModules.chats.hasPrivateChat = function (socket, uid, callback) {
2016-01-18 15:35:24 +02:00
if (!socket.uid || !uid) {
return callback(null, new Error('[[error:invalid-data]]'));
}
Messaging.hasPrivateChat(socket.uid, uid, callback);
};
2014-01-10 16:00:03 -05:00
SocketModules.chats.getMessages = function (socket, data, callback) {
2016-09-20 16:58:50 +03:00
if (!socket.uid || !data.uid || !data.roomId) {
return callback(new Error('[[error:invalid-data]]'));
}
2016-10-02 15:17:22 +03:00
2016-09-20 16:58:50 +03:00
var params = {
2016-10-03 20:35:36 +03:00
callerUid: socket.uid,
2016-09-20 16:58:50 +03:00
uid: data.uid,
roomId: data.roomId,
2016-10-02 15:17:22 +03:00
start: parseInt(data.start, 10) || 0,
2016-09-20 16:58:50 +03:00
count: 50,
markRead: false
};
2016-10-02 15:17:22 +03:00
if (data.hasOwnProperty('markRead')) {
params.markRead = data.markRead;
}
2016-10-03 20:35:36 +03:00
Messaging.getMessages(params, callback);
2016-09-20 16:58:50 +03:00
};
/* Sounds */
SocketModules.sounds.getSounds = function (socket, data, callback) {
// Read sounds from local directory
meta.sounds.getFiles(callback);
};
SocketModules.sounds.getMapping = function (socket, data, callback) {
2016-08-29 12:43:32 +03:00
meta.sounds.getMapping(socket.uid, callback);
};
SocketModules.sounds.getData = function (socket, data, callback) {
2015-10-20 18:47:34 -04:00
async.parallel({
2016-08-29 12:43:32 +03:00
mapping: async.apply(meta.sounds.getMapping, socket.uid),
2015-10-20 18:47:34 -04:00
files: async.apply(meta.sounds.getFiles)
}, callback);
};
2014-04-10 20:31:57 +01:00
module.exports = SocketModules;