2015-12-15 14:10:32 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async');
|
2015-12-23 11:27:34 +02:00
|
|
|
var validator = require('validator');
|
2015-12-15 14:10:32 +02:00
|
|
|
|
|
|
|
|
var db = require('../database');
|
2015-12-15 19:05:32 +02:00
|
|
|
var user = require('../user');
|
2016-10-03 20:35:36 +03:00
|
|
|
var plugins = require('../plugins');
|
2015-12-15 14:10:32 +02:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Messaging) {
|
|
|
|
|
Messaging.getRoomData = function (roomId, callback) {
|
|
|
|
|
db.getObject('chat:room:' + roomId, function (err, data) {
|
2015-12-23 11:27:34 +02:00
|
|
|
if (err || !data) {
|
|
|
|
|
return callback(err || new Error('[[error:no-chat-room]]'));
|
|
|
|
|
}
|
2016-09-30 13:18:29 +03:00
|
|
|
modifyRoomData([data]);
|
2015-12-23 11:27:34 +02:00
|
|
|
callback(null, data);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.getRoomsData = function (roomIds, callback) {
|
|
|
|
|
var keys = roomIds.map(function (roomId) {
|
2016-08-25 16:05:02 +03:00
|
|
|
return 'chat:room:' + roomId;
|
|
|
|
|
});
|
2016-10-13 11:43:39 +02:00
|
|
|
db.getObjects(keys, function (err, roomData) {
|
2016-08-25 16:05:02 +03:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2016-09-30 13:18:29 +03:00
|
|
|
modifyRoomData(roomData);
|
2016-08-25 16:05:02 +03:00
|
|
|
callback(null, roomData);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-30 13:18:29 +03:00
|
|
|
function modifyRoomData(rooms) {
|
2016-10-13 11:43:39 +02:00
|
|
|
rooms.forEach(function (data) {
|
2016-09-30 13:18:29 +03:00
|
|
|
if (data) {
|
2016-10-12 11:53:25 +03:00
|
|
|
data.roomName = data.roomName || '';
|
2016-09-30 13:18:29 +03:00
|
|
|
data.roomName = validator.escape(String(data.roomName));
|
|
|
|
|
if (data.hasOwnProperty('groupChat')) {
|
|
|
|
|
data.groupChat = parseInt(data.groupChat, 10) === 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.newRoom = function (uid, toUids, callback) {
|
2015-12-16 13:35:24 +02:00
|
|
|
var roomId;
|
|
|
|
|
var now = Date.now();
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.incrObjectField('global', 'nextChatRoomId', next);
|
|
|
|
|
},
|
|
|
|
|
function (_roomId, next) {
|
|
|
|
|
roomId = _roomId;
|
2015-12-23 10:10:59 +02:00
|
|
|
var room = {
|
|
|
|
|
owner: uid,
|
2017-02-17 19:31:21 -07:00
|
|
|
roomId: roomId,
|
2015-12-23 10:10:59 +02:00
|
|
|
};
|
|
|
|
|
db.setObject('chat:room:' + roomId, room, next);
|
2015-12-16 13:35:24 +02:00
|
|
|
},
|
|
|
|
|
function (next) {
|
2015-12-26 08:41:03 +02:00
|
|
|
db.sortedSetAdd('chat:room:' + roomId + ':uids', now, uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
Messaging.addUsersToRoom(uid, toUids, roomId, next);
|
2015-12-16 13:35:24 +02:00
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
Messaging.addRoomToUsers(roomId, [uid].concat(toUids), now, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
next(null, roomId);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-12-16 13:35:24 +02:00
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.isUserInRoom = function (uid, roomId, callback) {
|
2016-10-03 20:35:36 +03:00
|
|
|
async.waterfall([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2016-10-03 20:35:36 +03:00
|
|
|
db.isSortedSetMember('chat:room:' + roomId + ':uids', uid, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (inRoom, next) {
|
2016-10-03 20:35:36 +03:00
|
|
|
plugins.fireHook('filter:messaging.isUserInRoom', {uid: uid, roomId: roomId, inRoom: inRoom}, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (data, next) {
|
2016-10-03 20:35:36 +03:00
|
|
|
next(null, data.inRoom);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-10-03 20:35:36 +03:00
|
|
|
], callback);
|
2015-12-15 19:05:32 +02:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.roomExists = function (roomId, callback) {
|
2015-12-15 14:10:32 +02:00
|
|
|
db.exists('chat:room:' + roomId + ':uids', callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.getUserCountInRoom = function (roomId, callback) {
|
2015-12-23 12:28:19 +02:00
|
|
|
db.sortedSetCard('chat:room:' + roomId + ':uids', callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.isRoomOwner = function (uid, roomId, callback) {
|
|
|
|
|
db.getObjectField('chat:room:' + roomId, 'owner', function (err, owner) {
|
2015-12-15 14:10:32 +02:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
2015-12-23 10:10:59 +02:00
|
|
|
|
|
|
|
|
callback(null, parseInt(uid, 10) === parseInt(owner, 10));
|
2015-12-15 14:10:32 +02:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.addUsersToRoom = function (uid, uids, roomId, callback) {
|
2015-12-15 14:10:32 +02:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
2015-12-16 15:43:01 +02:00
|
|
|
Messaging.isUserInRoom(uid, roomId, next);
|
2015-12-15 14:10:32 +02:00
|
|
|
},
|
2015-12-16 15:43:01 +02:00
|
|
|
function (inRoom, next) {
|
|
|
|
|
if (!inRoom) {
|
2015-12-15 14:10:32 +02:00
|
|
|
return next(new Error('[[error:cant-add-users-to-chat-room]]'));
|
|
|
|
|
}
|
|
|
|
|
var now = Date.now();
|
2016-10-13 11:43:39 +02:00
|
|
|
var timestamps = uids.map(function () {
|
2015-12-15 14:10:32 +02:00
|
|
|
return now;
|
|
|
|
|
});
|
2015-12-16 13:35:24 +02:00
|
|
|
db.sortedSetAdd('chat:room:' + roomId + ':uids', timestamps, uids, next);
|
2016-09-30 13:18:29 +03:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2016-09-30 13:18:29 +03:00
|
|
|
async.parallel({
|
|
|
|
|
userCount: async.apply(db.sortedSetCard, 'chat:room:' + roomId + ':uids'),
|
2017-02-17 19:31:21 -07:00
|
|
|
roomData: async.apply(db.getObject, 'chat:room:' + roomId),
|
2016-09-30 13:18:29 +03:00
|
|
|
}, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (results, next) {
|
2016-09-30 13:18:29 +03:00
|
|
|
if (!results.roomData.hasOwnProperty('groupChat') && results.userCount > 2) {
|
|
|
|
|
return db.setObjectField('chat:room:' + roomId, 'groupChat', 1, next);
|
|
|
|
|
}
|
|
|
|
|
next();
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-12-15 14:10:32 +02:00
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.removeUsersFromRoom = function (uid, uids, roomId, callback) {
|
2015-12-16 11:30:10 +02:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
2015-12-23 12:28:19 +02:00
|
|
|
async.parallel({
|
|
|
|
|
isOwner: async.apply(Messaging.isRoomOwner, uid, roomId),
|
2017-02-17 19:31:21 -07:00
|
|
|
userCount: async.apply(Messaging.getUserCountInRoom, roomId),
|
2015-12-23 12:28:19 +02:00
|
|
|
}, next);
|
2015-12-16 13:35:24 +02:00
|
|
|
},
|
2015-12-23 12:28:19 +02:00
|
|
|
function (results, next) {
|
|
|
|
|
if (!results.isOwner) {
|
2016-01-08 12:34:09 +02:00
|
|
|
return next(new Error('[[error:cant-remove-users-from-chat-room]]'));
|
2015-12-16 13:35:24 +02:00
|
|
|
}
|
2015-12-23 12:28:19 +02:00
|
|
|
if (results.userCount === 2) {
|
|
|
|
|
return next(new Error('[[error:cant-remove-last-user]]'));
|
|
|
|
|
}
|
2016-01-08 12:34:09 +02:00
|
|
|
Messaging.leaveRoom(uids, roomId, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-01-08 12:34:09 +02:00
|
|
|
], callback);
|
|
|
|
|
};
|
2015-12-23 12:28:19 +02:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.leaveRoom = function (uids, roomId, callback) {
|
2016-01-08 12:34:09 +02:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
2015-12-16 13:35:24 +02:00
|
|
|
db.sortedSetRemove('chat:room:' + roomId + ':uids', uids, next);
|
2015-12-16 11:30:10 +02:00
|
|
|
},
|
|
|
|
|
function (next) {
|
2016-10-13 11:43:39 +02:00
|
|
|
var keys = uids.map(function (uid) {
|
2015-12-16 13:35:24 +02:00
|
|
|
return 'uid:' + uid + ':chat:rooms';
|
|
|
|
|
});
|
2016-10-18 20:45:25 +03:00
|
|
|
keys = keys.concat(uids.map(function (uid) {
|
2016-01-07 17:18:31 +02:00
|
|
|
return 'uid:' + uid + ':chat:rooms:unread';
|
|
|
|
|
}));
|
2015-12-16 13:35:24 +02:00
|
|
|
db.sortedSetsRemove(keys, roomId, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-12-16 11:30:10 +02:00
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.getUidsInRoom = function (roomId, start, stop, callback) {
|
2015-12-16 16:41:39 +02:00
|
|
|
db.getSortedSetRevRange('chat:room:' + roomId + ':uids', start, stop, callback);
|
2015-12-15 14:10:32 +02:00
|
|
|
};
|
2015-12-15 17:50:30 +02:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.getUsersInRoom = function (roomId, start, stop, callback) {
|
2015-12-15 19:05:32 +02:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
Messaging.getUidsInRoom(roomId, start, stop, next);
|
|
|
|
|
},
|
|
|
|
|
function (uids, next) {
|
2016-10-14 13:22:30 +03:00
|
|
|
user.getUsersFields(uids, ['uid', 'username', 'picture', 'status'], next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-12-15 19:05:32 +02:00
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
Messaging.renameRoom = function (uid, roomId, newName, callback) {
|
2015-12-23 11:27:34 +02:00
|
|
|
if (!newName) {
|
|
|
|
|
return callback(new Error('[[error:invalid-name]]'));
|
|
|
|
|
}
|
2016-03-03 13:51:42 +02:00
|
|
|
newName = newName.trim();
|
|
|
|
|
if (newName.length > 75) {
|
|
|
|
|
return callback(new Error('[[error:chat-room-name-too-long]]'));
|
|
|
|
|
}
|
2015-12-23 11:27:34 +02:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
Messaging.isRoomOwner(uid, roomId, next);
|
|
|
|
|
},
|
|
|
|
|
function (isOwner, next) {
|
|
|
|
|
if (!isOwner) {
|
|
|
|
|
return next(new Error('[[error:no-privileges]]'));
|
|
|
|
|
}
|
|
|
|
|
db.setObjectField('chat:room:' + roomId, 'roomName', newName, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-12-23 11:27:34 +02:00
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-18 17:35:05 +03:00
|
|
|
Messaging.canReply = function (roomId, uid, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
db.isSortedSetMember('chat:room:' + roomId + ':uids', uid, next);
|
|
|
|
|
},
|
|
|
|
|
function (inRoom, next) {
|
|
|
|
|
plugins.fireHook('filter:messaging.canReply', {uid: uid, roomId: roomId, inRoom: inRoom, canReply: inRoom}, next);
|
|
|
|
|
},
|
|
|
|
|
function (data, next) {
|
|
|
|
|
next(null, data.canReply);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-18 17:35:05 +03:00
|
|
|
], callback);
|
|
|
|
|
};
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|