2017-02-18 01:56:23 -07:00
|
|
|
'use strict';
|
2017-02-18 02:28:54 -07:00
|
|
|
|
2023-11-15 10:36:39 -05:00
|
|
|
/**
|
|
|
|
|
* v4 note — all methods here are deprecated and can be removed except for:
|
|
|
|
|
* - SocketModules.chats.(enter|leave)(Public)? => related to socket.io rooms
|
|
|
|
|
*/
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
const Messaging = require('../messaging');
|
|
|
|
|
const utils = require('../utils');
|
|
|
|
|
const user = require('../user');
|
2023-07-12 20:43:53 -04:00
|
|
|
const groups = require('../groups');
|
2015-12-16 13:35:24 +02:00
|
|
|
|
2023-11-09 15:18:55 -05:00
|
|
|
const api = require('../api');
|
|
|
|
|
const sockets = require('.');
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
const SocketModules = module.exports;
|
2017-05-20 20:03:41 -04:00
|
|
|
|
|
|
|
|
SocketModules.chats = {};
|
|
|
|
|
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) {
|
2023-11-09 15:18:55 -05:00
|
|
|
sockets.warnDeprecated(socket, 'GET /api/v3/chats/:roomId/messages/:mid/raw');
|
|
|
|
|
|
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]]');
|
2015-12-11 12:07:02 -05:00
|
|
|
}
|
2019-09-15 02:14:51 -04:00
|
|
|
const roomId = await Messaging.getMessageField(data.mid, 'roomId');
|
|
|
|
|
|
2023-11-09 15:18:55 -05:00
|
|
|
const { content } = await api.chats.getRawMessage(socket, {
|
|
|
|
|
mid: data.mid,
|
|
|
|
|
roomId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return content;
|
2015-12-11 12:07:02 -05:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketModules.chats.isDnD = async function (socket, uid) {
|
2023-11-13 10:12:04 -05:00
|
|
|
sockets.warnDeprecated(socket, 'GET /api/v3/users/:uid/status OR HEAD /api/v3/users/:uid/status/:status');
|
|
|
|
|
|
|
|
|
|
const { status } = await api.users.getStatus(socket, { uid });
|
2019-09-15 02:14:51 -04:00
|
|
|
return status === 'dnd';
|
2017-04-14 10:08:50 -04:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketModules.chats.canMessage = async function (socket, roomId) {
|
2023-11-13 15:35:46 -05:00
|
|
|
sockets.warnDeprecated(socket);
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
await Messaging.canMessageRoom(socket.uid, roomId);
|
2014-01-10 16:00:03 -05:00
|
|
|
};
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
SocketModules.chats.markAllRead = async function (socket) {
|
2023-11-13 15:35:46 -05:00
|
|
|
sockets.warnDeprecated(socket);
|
|
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
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.getRecentChats = async function (socket, data) {
|
2023-11-13 15:35:46 -05:00
|
|
|
sockets.warnDeprecated(socket, 'GET /api/v3/chats');
|
|
|
|
|
|
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;
|
2023-11-13 15:35:46 -05:00
|
|
|
const { uid } = data;
|
|
|
|
|
|
|
|
|
|
return api.chats.list(socket, { 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) {
|
2023-11-13 16:10:40 -05:00
|
|
|
sockets.warnDeprecated(socket, 'GET /api/v3/users/:uid/chat');
|
|
|
|
|
|
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
|
|
|
}
|
2023-11-13 16:10:40 -05:00
|
|
|
|
|
|
|
|
// despite the `has` prefix, this method actually did return the roomId.
|
|
|
|
|
const { roomId } = await api.users.getPrivateRoomId(socket, { uid });
|
|
|
|
|
return roomId;
|
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.getIP = async function (socket, mid) {
|
2023-11-14 11:53:53 -05:00
|
|
|
sockets.warnDeprecated(socket, 'GET /api/v3/chats/:roomId/messages/:mid/ip');
|
|
|
|
|
|
|
|
|
|
const { ip } = await api.chats.getIpAddress(socket, { mid });
|
|
|
|
|
return ip;
|
2018-05-31 15:05:12 -04:00
|
|
|
};
|
|
|
|
|
|
2023-07-12 13:03:54 -04:00
|
|
|
SocketModules.chats.getUnreadCount = async function (socket) {
|
2023-11-14 12:19:23 -05:00
|
|
|
sockets.warnDeprecated(socket, 'GET /api/v3/chats/unread');
|
|
|
|
|
|
|
|
|
|
const { count } = await api.chats.getUnread(socket);
|
|
|
|
|
return count;
|
2023-07-12 13:03:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SocketModules.chats.enter = async function (socket, roomIds) {
|
|
|
|
|
await joinLeave(socket, roomIds, 'join');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SocketModules.chats.leave = async function (socket, roomIds) {
|
|
|
|
|
await joinLeave(socket, roomIds, 'leave');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SocketModules.chats.enterPublic = async function (socket, roomIds) {
|
|
|
|
|
await joinLeave(socket, roomIds, 'join', 'chat_room_public');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SocketModules.chats.leavePublic = async function (socket, roomIds) {
|
|
|
|
|
await joinLeave(socket, roomIds, 'leave', 'chat_room_public');
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function joinLeave(socket, roomIds, method, prefix = 'chat_room') {
|
|
|
|
|
if (!(socket.uid > 0)) {
|
|
|
|
|
throw new Error('[[error:not-allowed]]');
|
|
|
|
|
}
|
|
|
|
|
if (!Array.isArray(roomIds)) {
|
|
|
|
|
roomIds = [roomIds];
|
|
|
|
|
}
|
|
|
|
|
if (roomIds.length) {
|
|
|
|
|
const [isAdmin, inRooms, roomData] = await Promise.all([
|
|
|
|
|
user.isAdministrator(socket.uid),
|
|
|
|
|
Messaging.isUserInRoom(socket.uid, roomIds),
|
|
|
|
|
Messaging.getRoomsData(roomIds, ['public', 'groups']),
|
|
|
|
|
]);
|
2023-07-14 13:01:41 -04:00
|
|
|
|
2023-07-12 13:03:54 -04:00
|
|
|
await Promise.all(roomIds.map(async (roomId, idx) => {
|
|
|
|
|
const isPublic = roomData[idx] && roomData[idx].public;
|
2023-07-12 20:43:53 -04:00
|
|
|
const roomGroups = roomData[idx] && roomData[idx].groups;
|
2023-07-17 22:42:00 -04:00
|
|
|
|
|
|
|
|
if (isAdmin ||
|
|
|
|
|
(
|
|
|
|
|
inRooms[idx] &&
|
|
|
|
|
(!isPublic || !roomGroups.length || await groups.isMemberOfAny(socket.uid, roomGroups))
|
|
|
|
|
)
|
|
|
|
|
) {
|
2023-07-12 13:03:54 -04:00
|
|
|
socket[method](`${prefix}_${roomId}`);
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SocketModules.chats.sortPublicRooms = async function (socket, data) {
|
2023-11-15 11:41:02 -05:00
|
|
|
sockets.warnDeprecated(socket, 'PUT /api/v3/chats/sort');
|
|
|
|
|
|
|
|
|
|
if (!data) {
|
2023-07-12 13:03:54 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
2023-11-15 11:41:02 -05:00
|
|
|
|
|
|
|
|
await api.chats.sortPublicRooms(socket, data);
|
2023-07-12 13:03:54 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
SocketModules.chats.searchMembers = async function (socket, data) {
|
2023-11-15 14:16:08 -05:00
|
|
|
sockets.warnDeprecated(socket, 'GET /api/v3/search/chats/:roomId/users?query=');
|
|
|
|
|
|
2023-07-12 13:03:54 -04:00
|
|
|
if (!data || !data.roomId) {
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-15 14:16:08 -05:00
|
|
|
// parameter renamed; backwards compatibility
|
|
|
|
|
data.query = data.username;
|
|
|
|
|
delete data.username;
|
|
|
|
|
return await api.search.roomUsers(socket, data);
|
2023-07-12 13:03:54 -04:00
|
|
|
};
|
|
|
|
|
|
2023-07-17 22:42:00 -04:00
|
|
|
SocketModules.chats.toggleOwner = async (socket, data) => {
|
2023-11-15 15:06:42 -05:00
|
|
|
sockets.warnDeprecated(socket, 'PUT/DELETE /api/v3/chats/:roomId/owners/:uid');
|
|
|
|
|
|
2023-07-17 22:42:00 -04:00
|
|
|
if (!data || !data.uid || !data.roomId) {
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-15 15:06:42 -05:00
|
|
|
await api.chats.toggleOwner(socket, data);
|
2023-07-17 22:42:00 -04:00
|
|
|
};
|
|
|
|
|
|
2023-07-21 15:31:34 -04:00
|
|
|
SocketModules.chats.setNotificationSetting = async (socket, data) => {
|
2023-11-16 11:23:39 -05:00
|
|
|
sockets.warnDeprecated(socket, 'PUT/DELETE /api/v3/chats/:roomId/watch');
|
|
|
|
|
|
2023-07-21 15:31:34 -04:00
|
|
|
if (!data || !utils.isNumber(data.value) || !data.roomId) {
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-16 11:23:39 -05:00
|
|
|
await api.chats.watch(socket, data);
|
2023-07-21 15:31:34 -04:00
|
|
|
};
|
|
|
|
|
|
2023-07-28 10:56:25 -04:00
|
|
|
SocketModules.chats.searchMessages = async (socket, data) => {
|
2023-11-16 15:21:57 -05:00
|
|
|
sockets.warnDeprecated(socket, 'GET /api/v3/search/chats/:roomId/messages');
|
|
|
|
|
|
2023-07-28 10:56:25 -04:00
|
|
|
if (!data || !utils.isNumber(data.roomId) || !data.content) {
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-16 15:21:57 -05:00
|
|
|
// parameter renamed; backwards compatibility
|
|
|
|
|
data.query = data.content;
|
|
|
|
|
delete data.content;
|
|
|
|
|
return await api.search.roomMessages(socket, data);
|
2023-07-28 10:56:25 -04:00
|
|
|
};
|
|
|
|
|
|
2023-08-28 15:57:30 -04:00
|
|
|
SocketModules.chats.loadPinnedMessages = async (socket, data) => {
|
2023-11-17 11:37:51 -05:00
|
|
|
sockets.warnDeprecated(socket, 'GET /api/v3/chats/:roomId/messages/pinned');
|
|
|
|
|
|
2023-08-28 15:57:30 -04:00
|
|
|
if (!data || !data.roomId || !utils.isNumber(data.start)) {
|
|
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
2023-11-17 11:37:51 -05:00
|
|
|
|
|
|
|
|
const { messages } = await api.chats.getPinnedMessages(socket, data);
|
|
|
|
|
return messages;
|
2023-08-28 15:57:30 -04:00
|
|
|
};
|
|
|
|
|
|
2023-09-02 21:18:00 -04:00
|
|
|
SocketModules.chats.typing = async (socket, data) => {
|
2023-11-17 14:10:29 -05:00
|
|
|
sockets.warnDeprecated(socket, 'PUT /api/v3/chats/:roomId/typing');
|
|
|
|
|
|
|
|
|
|
if (!data) {
|
2023-09-02 21:18:00 -04:00
|
|
|
throw new Error('[[error:invalid-data]]');
|
|
|
|
|
}
|
2023-11-17 14:10:29 -05:00
|
|
|
|
|
|
|
|
// `username` is now inferred from caller uid
|
|
|
|
|
delete data.username;
|
|
|
|
|
|
|
|
|
|
await api.chats.toggleTyping(socket, data);
|
2023-09-02 21:18:00 -04:00
|
|
|
};
|
|
|
|
|
|
2023-08-28 15:57:30 -04:00
|
|
|
|
2019-09-15 02:14:51 -04:00
|
|
|
require('../promisify')(SocketModules);
|