You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.3 KiB
JavaScript

8 years ago
'use strict';
const db = require('../database');
const notifications = require('../notifications');
const Messaging = require('../messaging');
const utils = require('../utils');
5 years ago
const server = require('./index');
const user = require('../user');
const privileges = require('../privileges');
const sockets = require('.');
const api = require('../api');
const SocketModules = module.exports;
8 years ago
SocketModules.chats = {};
SocketModules.settings = {};
/* Chat */
SocketModules.chats.getRaw = async function (socket, data) {
7 years ago
if (!data || !data.hasOwnProperty('mid')) {
throw new Error('[[error:invalid-data]]');
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <[email protected]> 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 <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <[email protected]> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <[email protected]> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
9 years ago
}
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 <[email protected]> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <[email protected]> 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 <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <[email protected]> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <[email protected]> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
9 years ago
};
SocketModules.chats.isDnD = async function (socket, uid) {
const status = await db.getObjectField(`user:${uid}`, 'status');
return status === 'dnd';
8 years ago
};
SocketModules.chats.newRoom = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v3/chats');
if (!data) {
throw new Error('[[error:invalid-data]]');
}
9 years ago
const roomObj = await api.chats.create(socket, {
uids: [data.touid],
});
return roomObj.roomId;
};
SocketModules.chats.send = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v3/chats/:roomId');
9 years ago
if (!data || !data.roomId || !socket.uid) {
throw new Error('[[error:invalid-data]]');
11 years ago
}
const canChat = await privileges.global.can('chat', socket.uid);
if (!canChat) {
throw new Error('[[error:no-privileges]]');
}
11 years ago
return api.chats.post(socket, data);
};
9 years ago
SocketModules.chats.loadRoom = async function (socket, data) {
sockets.warnDeprecated(socket, 'GET /api/v3/chats/:roomId');
if (!data || !data.roomId) {
throw new Error('[[error:invalid-data]]');
}
9 years ago
return await Messaging.loadRoom(socket.uid, data);
};
SocketModules.chats.getUsersInRoom = async function (socket, data) {
sockets.warnDeprecated(socket, 'GET /api/v3/chats/:roomId/users');
if (!data || !data.roomId) {
throw new Error('[[error:invalid-data]]');
}
const isUserInRoom = await Messaging.isUserInRoom(socket.uid, data.roomId);
if (!isUserInRoom) {
throw new Error('[[error:no-privileges]]');
}
return api.chats.users(socket, data);
};
SocketModules.chats.addUserToRoom = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v3/chats/:roomId/users');
if (!data || !data.roomId || !data.username) {
throw new Error('[[error:invalid-data]]');
}
const canChat = await privileges.global.can('chat', socket.uid);
if (!canChat) {
throw new Error('[[error:no-privileges]]');
}
// Revised API now takes uids, not usernames
data.uids = [await user.getUidByUsername(data.username)];
delete data.username;
await api.chats.invite(socket, data);
};
SocketModules.chats.removeUserFromRoom = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/chats/:roomId/users OR DELETE /api/v3/chats/:roomId/users/:uid');
if (!data || !data.roomId) {
throw new Error('[[error:invalid-data]]');
}
// Revised API can accept multiple uids now
data.uids = [data.uid];
delete data.uid;
await api.chats.kick(socket, data);
};
SocketModules.chats.leave = async function (socket, roomid) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/chats/:roomId/users OR DELETE /api/v3/chats/:roomId/users/:uid');
9 years ago
if (!socket.uid || !roomid) {
throw new Error('[[error:invalid-data]]');
9 years ago
}
await Messaging.leaveRoom([socket.uid], roomid);
9 years ago
};
SocketModules.chats.edit = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/chats/:roomId/:mid');
8 years ago
if (!data || !data.roomId || !data.message) {
throw new Error('[[error:invalid-data]]');
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <[email protected]> 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 <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <[email protected]> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <[email protected]> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
9 years ago
}
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 <[email protected]> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <[email protected]> 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 <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <[email protected]> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <[email protected]> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
9 years ago
};
SocketModules.chats.delete = async function (socket, data) {
sockets.warnDeprecated(socket, 'DELETE /api/v3/chats/:roomId/:mid');
if (!data || !data.roomId || !data.messageId) {
throw new Error('[[error:invalid-data]]');
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <[email protected]> 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 <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <[email protected]> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <[email protected]> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
9 years ago
}
await Messaging.canDelete(data.messageId, socket.uid);
await Messaging.deleteMessage(data.messageId, socket.uid);
};
Squashed commit of the following: Closes #2668 commit 3d4f494ed3257bceda8f6f82057cab83f0f252b3 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:06:42 2015 -0500 theme minvers for #2668 commit b608ce61854f8195143685bb9753b80d32b26e95 Author: Julian Lam <[email protected]> Date: Fri Dec 11 12:01:03 2015 -0500 Allowing chat modal to edit and delete messages re: #2668 commit 0104db90a4070582f3938b6929dae35f985bac35 Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Fri Dec 11 11:07:12 2015 -0500 If edited content matches existing content... ... then edit is aborted. commit 6e7495247b1895589c716db29f919a934087b924 Author: Julian Lam <[email protected]> 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 <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:55:23 2015 -0500 wiring up the edit button, #2668 commit 5f2afdcf6f2b9eae6b5873ca100149e65e3d385d Author: Julian Lam <[email protected]> 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 <[email protected]> Date: Tue Dec 8 14:06:39 2015 -0500 Allowing editing of chat messages commit bfd991be1cb1769599f7d5d2b1638e313c3c2dcb Author: Julian Lam <[email protected]> Date: Tue Dec 8 10:33:49 2015 -0500 Added messageId to messages object return commit 0306ee6657b3288dd4547c66869d7d4ece0b31ad Author: Julian Lam <[email protected]> Date: Tue Dec 8 08:20:17 2015 -0500 WIP #2668
9 years ago
SocketModules.chats.restore = async function (socket, data) {
sockets.warnDeprecated(socket, 'POST /api/v3/chats/:roomId/:mid');
if (!data || !data.roomId || !data.messageId) {
throw new Error('[[error:invalid-data]]');
}
await Messaging.canDelete(data.messageId, socket.uid);
await Messaging.restoreMessage(data.messageId, socket.uid);
};
SocketModules.chats.canMessage = async function (socket, roomId) {
await Messaging.canMessageRoom(socket.uid, roomId);
};
SocketModules.chats.markRead = async function (socket, roomId) {
8 years ago
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;
9 years ago
}
// 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);
9 years ago
};
SocketModules.chats.renameRoom = async function (socket, data) {
sockets.warnDeprecated(socket, 'PUT /api/v3/chats/:roomId');
8 years ago
if (!data || !data.roomId || !data.newName) {
throw new Error('[[error:invalid-data]]');
9 years ago
}
data.name = data.newName;
delete data.newName;
await api.chats.rename(socket, data);
9 years ago
};
SocketModules.chats.getRecentChats = async function (socket, data) {
9 years ago
if (!data || !utils.isNumber(data.after) || !utils.isNumber(data.uid)) {
throw new Error('[[error:invalid-data]]');
11 years ago
}
const start = parseInt(data.after, 10);
const stop = start + 9;
return await Messaging.getRecentChats(socket.uid, data.uid, start, stop);
};
SocketModules.chats.hasPrivateChat = async function (socket, uid) {
6 years ago
if (socket.uid <= 0 || uid <= 0) {
throw new Error('[[error:invalid-data]]');
9 years ago
}
return await Messaging.hasPrivateChat(socket.uid, uid);
9 years ago
};
SocketModules.chats.getMessages = async function (socket, data) {
sockets.warnDeprecated(socket, 'GET /api/v3/chats/:roomId/messages');
8 years ago
if (!socket.uid || !data || !data.uid || !data.roomId) {
throw new Error('[[error:invalid-data]]');
}
9 years ago
return await Messaging.getMessages({
9 years ago
callerUid: socket.uid,
uid: data.uid,
roomId: data.roomId,
9 years ago
start: parseInt(data.start, 10) || 0,
count: 50,
});
};
SocketModules.chats.getIP = async function (socket, mid) {
const allowed = await privileges.global.can('view:users:info', socket.uid);
if (!allowed) {
throw new Error('[[error:no-privilege]]');
}
return await Messaging.getMessageField(mid, 'ip');
7 years ago
};
require('../promisify')(SocketModules);