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

296 lines
8.6 KiB
JavaScript
Raw Normal View History

2017-02-18 01:56:23 -07:00
'use strict';
2017-02-18 02:28:54 -07:00
2019-09-15 02:14:51 -04:00
const validator = require('validator');
2015-12-23 11:27:34 +02:00
2019-09-15 02:14:51 -04:00
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');
2019-09-15 02:14:51 -04:00
const SocketModules = module.exports;
2017-05-20 20:03:41 -04:00
SocketModules.chats = {};
SocketModules.sounds = {};
SocketModules.settings = {};
2014-01-10 16:00:03 -05:00
/* Chat */
2019-09-15 02:14:51 -04:00
SocketModules.chats.getRaw = async function (socket, data) {
2017-11-29 11:46:30 -05:00
if (!data || !data.hasOwnProperty('mid')) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
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
}
2019-09-15 02:14:51 -04:00
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),
]);
if (!isAdmin && (!inRoom || !hasMessage)) {
throw new Error('[[error:not-allowed]]');
}
return await Messaging.getMessageField(data.mid, 'content');
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
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.isDnD = async function (socket, uid) {
const status = await db.getObjectField('user:' + uid, 'status');
return status === 'dnd';
2017-04-14 10:08:50 -04:00
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.newRoom = async function (socket, data) {
2015-12-16 11:15:43 +02:00
if (!data) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
2015-12-16 11:15:43 +02:00
}
2016-09-07 18:48:00 +03:00
if (rateLimitExceeded(socket)) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:too-many-messages]]');
2015-12-16 11:15:43 +02:00
}
2019-09-15 02:14:51 -04:00
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]);
2015-12-16 11:15:43 +02:00
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.send = async function (socket, data) {
2016-10-03 20:35:36 +03:00
if (!data || !data.roomId || !socket.uid) {
2019-09-15 02:14:51 -04:00
throw 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)) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:too-many-messages]]');
2014-10-15 14:49:11 -04:00
}
2019-09-15 02:14:51 -04:00
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;
2019-09-15 02:14:51 -04:00
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;
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) {
2019-09-15 02:14:51 -04:00
const now = Date.now();
2016-09-07 18:48:00 +03:00
socket.lastChatMessageTime = socket.lastChatMessageTime || 0;
if (now - socket.lastChatMessageTime < meta.config.chatMessageDelay) {
2016-09-07 18:48:00 +03:00
return true;
}
socket.lastChatMessageTime = now;
2016-09-07 18:48:00 +03:00
return false;
}
2019-09-15 02:14:51 -04:00
SocketModules.chats.loadRoom = async function (socket, data) {
if (!data || !data.roomId) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
}
2015-12-23 11:27:34 +02:00
2019-09-15 02:14:51 -04:00
return await Messaging.loadRoom(socket.uid, data);
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.getUsersInRoom = async function (socket, data) {
2018-01-06 12:03:52 -05:00
if (!data || !data.roomId) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
2018-01-06 12:03:52 -05:00
}
2019-09-15 02:14:51 -04:00
const [userData, isOwner] = await Promise.all([
Messaging.getUsersInRoom(data.roomId, 0, -1),
Messaging.isRoomOwner(socket.uid, data.roomId),
]);
2018-01-06 12:03:52 -05:00
2019-09-15 02:14:51 -04:00
userData.forEach((user) => {
user.canKick = (parseInt(user.uid, 10) !== parseInt(socket.uid, 10)) && isOwner;
2018-04-30 12:35:17 -04:00
});
2019-09-15 02:14:51 -04:00
return userData;
2018-01-06 12:03:52 -05:00
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.addUserToRoom = async function (socket, data) {
if (!data || !data.roomId || !data.username) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
}
const canChat = await privileges.global.can('chat', socket.uid);
if (!canChat) {
throw new Error('[[error:no-privileges]]');
}
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]]');
}
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]]');
}
2019-09-15 02:14:51 -04:00
await Messaging.addUsersToRoom(socket.uid, [uid], data.roomId);
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.removeUserFromRoom = async function (socket, data) {
if (!data || !data.roomId) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
}
const exists = await user.exists(data.uid);
if (!exists) {
throw new Error('[[error:no-user]]');
}
2018-04-30 12:35:17 -04:00
2019-09-15 02:14:51 -04:00
await Messaging.removeUsersFromRoom(socket.uid, [data.uid], data.roomId);
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.leave = async function (socket, roomid) {
2016-01-08 12:34:09 +02:00
if (!socket.uid || !roomid) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
2016-01-08 12:34:09 +02:00
}
2019-09-15 02:14:51 -04:00
await Messaging.leaveRoom([socket.uid], roomid);
2016-01-08 12:34:09 +02:00
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.edit = async function (socket, data) {
2017-05-20 20:03:41 -04:00
if (!data || !data.roomId || !data.message) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
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
}
2019-09-15 02:14:51 -04:00
await Messaging.canEdit(data.mid, socket.uid);
await Messaging.editMessage(socket.uid, data.mid, data.roomId, data.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
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.delete = async function (socket, data) {
2015-12-15 14:10:32 +02:00
if (!data || !data.roomId || !data.messageId) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
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
}
2019-09-15 02:14:51 -04:00
await Messaging.canDelete(data.messageId, socket.uid);
await Messaging.deleteMessage(data.messageId);
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
2019-09-15 02:14:51 -04:00
SocketModules.chats.restore = async function (socket, data) {
if (!data || !data.roomId || !data.messageId) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
}
2019-09-15 02:14:51 -04:00
await Messaging.canDelete(data.messageId, socket.uid);
await Messaging.restoreMessage(data.messageId);
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.canMessage = async function (socket, roomId) {
await Messaging.canMessageRoom(socket.uid, roomId);
2014-01-10 16:00:03 -05:00
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.markRead = async function (socket, roomId) {
2017-05-20 20:03:41 -04:00
if (!socket.uid || !roomId) {
2019-09-15 02:14:51 -04:00
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;
2016-10-03 20:35:36 +03:00
}
2019-09-15 02:14:51 -04:00
// 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);
2014-07-19 10:33:27 -04:00
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.markAllRead = async function (socket) {
await Messaging.markAllRead(socket.uid);
Messaging.pushUnreadCount(socket.uid);
2016-03-11 13:38:52 +02:00
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.renameRoom = async function (socket, data) {
2017-05-20 20:03:41 -04:00
if (!data || !data.roomId || !data.newName) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
2015-12-23 11:27:34 +02:00
}
2019-09-15 02:14:51 -04:00
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);
});
2015-12-23 11:27:34 +02:00
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.getRecentChats = async function (socket, data) {
2016-10-03 20:35:36 +03:00
if (!data || !utils.isNumber(data.after) || !utils.isNumber(data.uid)) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
2014-09-15 16:26:25 -04:00
}
2019-09-15 02:14:51 -04:00
const start = parseInt(data.after, 10);
const stop = start + 9;
return await Messaging.getRecentChats(socket.uid, data.uid, start, stop);
2014-01-10 16:00:03 -05:00
};
2019-09-15 02:14:51 -04:00
SocketModules.chats.hasPrivateChat = async function (socket, uid) {
2018-11-17 22:31:39 -05:00
if (socket.uid <= 0 || uid <= 0) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
2016-01-18 15:35:24 +02:00
}
2019-09-15 02:14:51 -04:00
return await Messaging.hasPrivateChat(socket.uid, uid);
2016-01-18 15:35:24 +02:00
};
2014-01-10 16:00:03 -05:00
2019-09-15 02:14:51 -04:00
SocketModules.chats.getMessages = async function (socket, data) {
2017-05-20 20:03:41 -04:00
if (!socket.uid || !data || !data.uid || !data.roomId) {
2019-09-15 02:14:51 -04:00
throw new Error('[[error:invalid-data]]');
2016-09-20 16:58:50 +03:00
}
2016-10-02 15:17:22 +03:00
2019-09-15 02:14:51 -04:00
return await Messaging.getMessages({
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,
2017-02-17 19:31:21 -07:00
count: 50,
2019-09-15 02:14:51 -04:00
});
2016-09-20 16:58:50 +03:00
};
2019-09-15 02:14:51 -04:00
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');
2018-05-31 15:05:12 -04:00
};
/* Sounds */
2019-09-15 02:14:51 -04:00
SocketModules.sounds.getUserSoundMap = async function getUserSoundMap(socket) {
return await meta.sounds.getUserSoundMap(socket.uid);
2015-10-20 18:47:34 -04:00
};
2019-09-15 02:14:51 -04:00
require('../promisify')(SocketModules);