v1.18.x
Baris Usakli 7 years ago
parent ecc2b9560d
commit e99d4a5c61

@ -177,9 +177,44 @@ module.exports = function (Messaging) {
}));
db.sortedSetsRemove(keys, roomId, next);
},
function (next) {
updateOwner(roomId, next);
},
], callback);
};
Messaging.leaveRooms = function (uid, roomIds, callback) {
async.waterfall([
function (next) {
var roomKeys = roomIds.map(function (roomId) {
return 'chat:room:' + roomId + ':uids';
});
db.sortedSetsRemove(roomKeys, uid, next);
},
function (next) {
db.sortedSetRemove('uid:' + uid + ':chat:rooms', roomIds, next);
},
function (next) {
db.sortedSetRemove('uid:' + uid + ':chat:rooms:unread', roomIds, next);
},
function (next) {
async.eachSeries(roomIds, updateOwner, next);
},
], callback);
};
function updateOwner(roomId, callback) {
async.waterfall([
function (next) {
db.getSortedSetRange('chat:room:' + roomId + ':uids', 0, 0, next);
},
function (uids, next) {
var newOwner = uids[0] || 0;
db.setObjectField('chat:room:' + roomId, 'owner', newOwner, next);
},
], callback);
}
Messaging.getUidsInRoom = function (roomId, start, stop, callback) {
db.getSortedSetRevRange('chat:room:' + roomId + ':uids', start, stop, callback);
};

@ -7,6 +7,7 @@ var db = require('../database');
var posts = require('../posts');
var topics = require('../topics');
var groups = require('../groups');
var messaging = require('../messaging');
var plugins = require('../plugins');
var batch = require('../batch');
@ -173,12 +174,9 @@ module.exports = function (User) {
var userKeys = roomIds.map(function (roomId) {
return 'uid:' + uid + ':chat:room:' + roomId + ':mids';
});
var roomKeys = roomIds.map(function (roomId) {
return 'chat:room:' + roomId + ':uids';
});
async.parallel([
async.apply(db.sortedSetsRemove, roomKeys, uid),
async.apply(messaging.leaveRooms, uid, roomIds),
async.apply(db.deleteAll, userKeys),
], next);
},

@ -177,7 +177,48 @@ describe('Messaging Library', function () {
Messaging.isUserInRoom(bazUid, roomId, function (err, isUserInRoom) {
assert.ifError(err);
assert.equal(isUserInRoom, false);
done();
Messaging.getRoomData(roomId, function (err, data) {
assert.ifError(err);
assert.equal(data.owner, fooUid);
done();
});
});
});
});
it('should change owner when owner leaves room', function (done) {
socketModules.chats.newRoom({ uid: herpUid }, { touid: fooUid }, function (err, roomId) {
assert.ifError(err);
socketModules.chats.addUserToRoom({ uid: herpUid }, { roomId: roomId, username: 'baz' }, function (err) {
assert.ifError(err);
socketModules.chats.leave({ uid: herpUid }, roomId, function (err) {
assert.ifError(err);
Messaging.getRoomData(roomId, function (err, data) {
assert.ifError(err);
assert.equal(data.owner, fooUid);
done();
});
});
});
});
});
it('should change owner if owner is deleted', function (done) {
User.create({ username: 'deleted_chat_user' }, function (err, sender) {
assert.ifError(err);
User.create({ username: 'receiver' }, function (err, receiver) {
assert.ifError(err);
socketModules.chats.newRoom({ uid: sender }, { touid: receiver }, function (err, roomId) {
assert.ifError(err);
User.deleteAccount(sender, function (err) {
assert.ifError(err);
Messaging.getRoomData(roomId, function (err, data) {
assert.ifError(err);
assert.equal(data.owner, receiver);
done();
});
});
});
});
});
});

Loading…
Cancel
Save