Skip to content

Node.js based forum software built for the modern web

closes #6313

Baris Usaklie99d4a5c612a
diff --git a/src/messaging/rooms.js b/src/messaging/rooms.js
index 41f09c5125..1bdbd3cef4 100644
--- a/src/messaging/rooms.js
+++ b/src/messaging/rooms.js
@@ -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);
};
diff --git a/src/user/delete.js b/src/user/delete.js
index 92fd8f27b9..0f1ad61047 100644
--- a/src/user/delete.js
+++ b/src/user/delete.js
@@ -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);
},
diff --git a/test/messaging.js b/test/messaging.js
index 761500e7e4..80ec29d09b 100644
--- a/test/messaging.js
+++ b/test/messaging.js
@@ -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();
+ });
+ });
+ });
});
});
});