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.

244 lines
7.4 KiB
JavaScript

'use strict';
9 years ago
var validator = require('validator');
var db = require('../database');
var user = require('../user');
9 years ago
var plugins = require('../plugins');
7 years ago
var privileges = require('../privileges');
var meta = require('../meta');
module.exports = function (Messaging) {
fix(style): updated code to follow new eslint recommendations Squashed commit of the following: commit f9ce878b269b3568f0d649309aae1af4dcfdfeef Author: Julian Lam <[email protected]> Date: Tue Aug 13 14:30:46 2019 -0400 fix(style): updated code to follow new eslint recommendations commit 80dd370e413f22badb96ff2138e7991dfff6d836 Author: Julian Lam <[email protected]> Date: Tue Aug 13 14:14:58 2019 -0400 fix(deps): update dependency sitemap to v4 Squashed commit of the following: commit f4dd9cabb21e26fdc21f8413be822ea7c64251f8 Author: Julian Lam <[email protected]> Date: Tue Aug 13 11:33:05 2019 -0400 fix: resolved breaking changes from sitemap v4 upgrade commit 9043415ee16dcc27a8dcc2e4479d1bc5e2d1b60e Merge: e3352b272 72590b346 Author: Julian Lam <[email protected]> Date: Tue Aug 13 11:09:55 2019 -0400 Merge branch 'master' into renovate/sitemap-4.x commit e3352b272eb9400bdb00774973181397803765e4 Author: Renovate Bot <[email protected]> Date: Mon Aug 12 07:59:05 2019 +0000 fix(deps): update dependency sitemap to v4 commit 8e3c0cdcae22acc32d352be8bb72d60e7502dbc5 Author: Renovate Bot <[email protected]> Date: Fri Aug 9 00:49:51 2019 +0000 fix(deps): update dependency commander to v3 commit 2104449d38818f2fa4d44b3a58a0a168781acbfb Author: Renovate Bot <[email protected]> Date: Tue Aug 13 15:00:27 2019 +0000 fix(deps): update dependency mongodb to v3.3.0 commit d2937f446a21131c070ae5d0ff33d67cfe465b8c Author: Barış Soner Uşaklı <[email protected]> Date: Tue Aug 13 10:36:48 2019 -0400 feat: async/await admin/controllers commit 1b97e8b199f960dc24e5722702f27499ae049914 Author: Misty (Bot) <[email protected]> Date: Tue Aug 13 09:28:39 2019 +0000 Latest translations and fallbacks commit 69a48957a2f0d23c4d194b664bda3a0431179c01 Author: Barış Soner Uşaklı <[email protected]> Date: Mon Aug 12 21:56:09 2019 -0400 feat: async/await commit b9b2a7e593a452de4bef6d0ab6abe368a3bdb8dd Author: Barış Soner Uşaklı <[email protected]> Date: Mon Aug 12 20:58:29 2019 -0400 feat: async/await refactor controllers/accounts commit a8d43a175974a0c8ae3dc132bf51a7ed9a4c6305 Author: Baris Usakli <[email protected]> Date: Mon Aug 12 14:49:40 2019 -0400 feat: async/await controllers/accounts commit 2f25aae57bf9dbe98d655276770e56bed9ec023b Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 23:09:50 2019 -0400 fix: #7831, fix pagination convert to async/await commit c9e83f2374572264855a04156278eef256b0a20c Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 00:14:35 2019 -0400 fix: remove empty line commit 30be91b26c4dd7583412c4e8d56e9c1688e48a44 Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 00:13:41 2019 -0400 fix: remove useless catchs and empty line commit 2e4a71c0b6104738f15ffbfe3246105b922fdfb3 Author: Renovate Bot <[email protected]> Date: Sat Aug 10 06:51:50 2019 +0000 chore(deps): update dependency eslint-config-airbnb-base to v14
6 years ago
Messaging.getRoomData = async roomId => {
const data = await db.getObject('chat:room:' + roomId);
if (!data) {
throw new Error('[[error:no-chat-room]]');
}
modifyRoomData([data]);
return data;
9 years ago
};
fix(style): updated code to follow new eslint recommendations Squashed commit of the following: commit f9ce878b269b3568f0d649309aae1af4dcfdfeef Author: Julian Lam <[email protected]> Date: Tue Aug 13 14:30:46 2019 -0400 fix(style): updated code to follow new eslint recommendations commit 80dd370e413f22badb96ff2138e7991dfff6d836 Author: Julian Lam <[email protected]> Date: Tue Aug 13 14:14:58 2019 -0400 fix(deps): update dependency sitemap to v4 Squashed commit of the following: commit f4dd9cabb21e26fdc21f8413be822ea7c64251f8 Author: Julian Lam <[email protected]> Date: Tue Aug 13 11:33:05 2019 -0400 fix: resolved breaking changes from sitemap v4 upgrade commit 9043415ee16dcc27a8dcc2e4479d1bc5e2d1b60e Merge: e3352b272 72590b346 Author: Julian Lam <[email protected]> Date: Tue Aug 13 11:09:55 2019 -0400 Merge branch 'master' into renovate/sitemap-4.x commit e3352b272eb9400bdb00774973181397803765e4 Author: Renovate Bot <[email protected]> Date: Mon Aug 12 07:59:05 2019 +0000 fix(deps): update dependency sitemap to v4 commit 8e3c0cdcae22acc32d352be8bb72d60e7502dbc5 Author: Renovate Bot <[email protected]> Date: Fri Aug 9 00:49:51 2019 +0000 fix(deps): update dependency commander to v3 commit 2104449d38818f2fa4d44b3a58a0a168781acbfb Author: Renovate Bot <[email protected]> Date: Tue Aug 13 15:00:27 2019 +0000 fix(deps): update dependency mongodb to v3.3.0 commit d2937f446a21131c070ae5d0ff33d67cfe465b8c Author: Barış Soner Uşaklı <[email protected]> Date: Tue Aug 13 10:36:48 2019 -0400 feat: async/await admin/controllers commit 1b97e8b199f960dc24e5722702f27499ae049914 Author: Misty (Bot) <[email protected]> Date: Tue Aug 13 09:28:39 2019 +0000 Latest translations and fallbacks commit 69a48957a2f0d23c4d194b664bda3a0431179c01 Author: Barış Soner Uşaklı <[email protected]> Date: Mon Aug 12 21:56:09 2019 -0400 feat: async/await commit b9b2a7e593a452de4bef6d0ab6abe368a3bdb8dd Author: Barış Soner Uşaklı <[email protected]> Date: Mon Aug 12 20:58:29 2019 -0400 feat: async/await refactor controllers/accounts commit a8d43a175974a0c8ae3dc132bf51a7ed9a4c6305 Author: Baris Usakli <[email protected]> Date: Mon Aug 12 14:49:40 2019 -0400 feat: async/await controllers/accounts commit 2f25aae57bf9dbe98d655276770e56bed9ec023b Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 23:09:50 2019 -0400 fix: #7831, fix pagination convert to async/await commit c9e83f2374572264855a04156278eef256b0a20c Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 00:14:35 2019 -0400 fix: remove empty line commit 30be91b26c4dd7583412c4e8d56e9c1688e48a44 Author: Barış Soner Uşaklı <[email protected]> Date: Sun Aug 11 00:13:41 2019 -0400 fix: remove useless catchs and empty line commit 2e4a71c0b6104738f15ffbfe3246105b922fdfb3 Author: Renovate Bot <[email protected]> Date: Sat Aug 10 06:51:50 2019 +0000 chore(deps): update dependency eslint-config-airbnb-base to v14
6 years ago
Messaging.getRoomsData = async roomIds => {
const roomData = await db.getObjects(roomIds.map(function (roomId) {
return 'chat:room:' + roomId;
}));
modifyRoomData(roomData);
return roomData;
};
9 years ago
function modifyRoomData(rooms) {
rooms.forEach(function (data) {
9 years ago
if (data) {
9 years ago
data.roomName = data.roomName || '';
9 years ago
data.roomName = validator.escape(String(data.roomName));
if (data.hasOwnProperty('groupChat')) {
data.groupChat = parseInt(data.groupChat, 10) === 1;
}
}
});
}
Messaging.newRoom = async (uid, toUids) => {
const now = Date.now();
const roomId = await db.incrObjectField('global', 'nextChatRoomId');
const room = {
owner: uid,
roomId: roomId,
};
await Promise.all([
db.setObject('chat:room:' + roomId, room),
db.sortedSetAdd('chat:room:' + roomId + ':uids', now, uid),
]);
await Promise.all([
Messaging.addUsersToRoom(uid, toUids, roomId),
Messaging.addRoomToUsers(roomId, [uid].concat(toUids), now),
]);
return roomId;
};
Messaging.isUserInRoom = async (uid, roomId) => {
const inRoom = db.isSortedSetMember('chat:room:' + roomId + ':uids', uid);
const data = await plugins.fireHook('filter:messaging.isUserInRoom', { uid: uid, roomId: roomId, inRoom: inRoom });
return data.inRoom;
9 years ago
};
Messaging.roomExists = async roomId => db.exists('chat:room:' + roomId + ':uids');
Messaging.getUserCountInRoom = async roomId => db.sortedSetCard('chat:room:' + roomId + ':uids');
Messaging.isRoomOwner = async (uid, roomId) => {
const owner = await db.getObjectField('chat:room:' + roomId, 'owner');
return parseInt(uid, 10) === parseInt(owner, 10);
};
Messaging.addUsersToRoom = async function (uid, uids, roomId) {
const now = Date.now();
const timestamps = uids.map(() => now);
const inRoom = await Messaging.isUserInRoom(uid, roomId);
if (!inRoom) {
throw new Error('[[error:cant-add-users-to-chat-room]]');
}
await db.sortedSetAdd('chat:room:' + roomId + ':uids', timestamps, uids);
const [userCount, roomData] = await Promise.all([
db.sortedSetCard('chat:room:' + roomId + ':uids'),
db.getObject('chat:room:' + roomId),
]);
if (!roomData.hasOwnProperty('groupChat') && userCount > 2) {
await db.setObjectField('chat:room:' + roomId, 'groupChat', 1);
}
await Promise.all(uids.map(uid => Messaging.addSystemMessage('user-join', uid, roomId)));
};
Messaging.removeUsersFromRoom = async (uid, uids, roomId) => {
const [isOwner, userCount] = await Promise.all([
Messaging.isRoomOwner(uid, roomId),
Messaging.getUserCountInRoom(roomId),
]);
if (!isOwner) {
throw new Error('[[error:cant-remove-users-from-chat-room]]');
}
if (userCount === 2) {
throw new Error('[[error:cant-remove-last-user]]');
}
await Messaging.leaveRoom(uids, roomId);
9 years ago
};
9 years ago
Messaging.leaveRoom = async (uids, roomId) => {
const keys = uids
.map(function (uid) {
return 'uid:' + uid + ':chat:rooms';
})
.concat(uids.map(function (uid) {
return 'uid:' + uid + ':chat:rooms:unread';
}));
await Promise.all([
db.sortedSetRemove('chat:room:' + roomId + ':uids', uids),
db.sortedSetsRemove(keys, roomId),
]);
await Promise.all(uids.map(uid => Messaging.addSystemMessage('user-leave', uid, roomId)));
await updateOwner(roomId);
};
Messaging.leaveRooms = async (uid, roomIds) => {
const roomKeys = roomIds.map(roomId => 'chat:room:' + roomId + ':uids');
await Promise.all([
db.sortedSetsRemove(roomKeys, uid),
db.sortedSetRemove([
'uid:' + uid + ':chat:rooms',
'uid:' + uid + ':chat:rooms:unread',
], roomIds),
]);
await Promise.all(
roomIds.map(roomId => updateOwner(roomId))
.concat(roomIds.map(roomId => Messaging.addSystemMessage('user-leave', uid, roomId)))
);
7 years ago
};
async function updateOwner(roomId) {
const uids = await db.getSortedSetRange('chat:room:' + roomId + ':uids', 0, 0);
const newOwner = uids[0] || 0;
await db.setObjectField('chat:room:' + roomId, 'owner', newOwner);
7 years ago
}
Messaging.getUidsInRoom = async (roomId, start, stop) => db.getSortedSetRevRange('chat:room:' + roomId + ':uids', start, stop);
Messaging.getUsersInRoom = async (roomId, start, stop) => {
const uids = await Messaging.getUidsInRoom(roomId, start, stop);
const [users, ownerId] = await Promise.all([
user.getUsersFields(uids, ['uid', 'username', 'picture', 'status']),
db.getObjectField('chat:room:' + roomId, 'owner'),
]);
return users.map(function (user) {
user.isOwner = parseInt(user.uid, 10) === parseInt(ownerId, 10);
return user;
});
};
Messaging.renameRoom = async function (uid, roomId, newName) {
9 years ago
if (!newName) {
throw new Error('[[error:invalid-name]]');
9 years ago
}
9 years ago
newName = newName.trim();
if (newName.length > 75) {
throw new Error('[[error:chat-room-name-too-long]]');
9 years ago
}
9 years ago
const payload = await plugins.fireHook('filter:chat.renameRoom', {
uid: uid,
roomId: roomId,
newName: newName,
});
const isOwner = await Messaging.isRoomOwner(payload.uid, payload.roomId);
if (!isOwner) {
throw new Error('[[error:no-privileges]]');
}
await db.setObjectField('chat:room:' + payload.roomId, 'roomName', payload.newName);
await Messaging.addSystemMessage('room-rename, ' + payload.newName.replace(',', '%2C'), payload.uid, payload.roomId);
plugins.fireHook('action:chat.renameRoom', {
roomId: payload.roomId,
newName: payload.newName,
});
};
7 years ago
Messaging.canReply = async (roomId, uid) => {
const inRoom = db.isSortedSetMember('chat:room:' + roomId + ':uids', uid);
const data = await plugins.fireHook('filter:messaging.canReply', { uid: uid, roomId: roomId, inRoom: inRoom, canReply: inRoom });
return data.canReply;
};
7 years ago
Messaging.loadRoom = async (uid, data) => {
const canChat = await privileges.global.can('chat', uid);
if (!canChat) {
throw new Error('[[error:no-privileges]]');
}
const inRoom = await Messaging.isUserInRoom(uid, data.roomId);
if (!inRoom) {
return null;
}
7 years ago
const [roomData, canReply, users, messages, isAdminOrGlobalMod] = await Promise.all([
Messaging.getRoomData(data.roomId),
Messaging.canReply(data.roomId, uid),
Messaging.getUsersInRoom(data.roomId, 0, -1),
Messaging.getMessages({
callerUid: uid,
uid: data.uid || uid,
roomId: data.roomId,
isNew: false,
}),
user.isAdminOrGlobalMod(uid),
]);
var room = roomData;
room.messages = messages;
room.isOwner = parseInt(room.owner, 10) === parseInt(uid, 10);
room.users = users.filter(function (user) {
return user && parseInt(user.uid, 10) && parseInt(user.uid, 10) !== uid;
});
room.canReply = canReply;
room.groupChat = room.hasOwnProperty('groupChat') ? room.groupChat : users.length > 2;
room.usernames = Messaging.generateUsernames(users, uid);
room.maximumUsersInChatRoom = meta.config.maximumUsersInChatRoom;
room.maximumChatMessageLength = meta.config.maximumChatMessageLength;
room.showUserInput = !room.maximumUsersInChatRoom || room.maximumUsersInChatRoom > 2;
room.isAdminOrGlobalMod = isAdminOrGlobalMod;
return room;
7 years ago
};
8 years ago
};