Skip to content

Commit

Permalink
BREAKING CHANGE: getRoomObjects now returns list of users for the o…
Browse files Browse the repository at this point in the history
…bjects in the specified room. Also, add `addRoomToUser` and `removeRoomFromUser` methods.
  • Loading branch information
artch committed Sep 13, 2017
1 parent 6b3a5c1 commit 13e0a7c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
24 changes: 22 additions & 2 deletions lib/bulk.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,32 @@ module.exports = function(collectionName) {
id = id._id;
}
opsCnt++;
bulk.push({op: 'inc', id, key, amount});
bulk.push({op: 'update', id, update: {$inc: {[key]: amount}}});
},
addToSet(id, key, value) {
if(!id) {
return;
}
if (_.isObject(id)) {
id = id._id;
}
opsCnt++;
bulk.push({op: 'update', id, update: {$addToSet: {[key]: value}}});
},
pull(id, key, value) {
if(!id) {
return;
}
if (_.isObject(id)) {
id = id._id;
}
opsCnt++;
bulk.push({op: 'update', id, update: {$pull: {[key]: value}}});
},
execute() {
if(!opsCnt) return q.when({});
for(var id in updates) {
bulk.push({op: 'update', id, $set: updates[id]});
bulk.push({op: 'update', id, update: {$set: updates[id]}});
}
return common.storage.db[collectionName].bulk(bulk);
}
Expand Down
37 changes: 35 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,27 @@ exports.getRoomIntents = function(roomId) {
};

exports.getRoomObjects = function(roomId) {
var result = {};
return db['rooms.objects'].find({room: roomId})
.then((result) => exports.mapById(result));
.then((objects) => {
var users = {};
result.objects = exports.mapById(objects, obj => {
if(obj.user) {
users[obj.user] = true;
}
});
users = Object.keys(users);
if(users.length) {
return db['users'].find({_id: {$in: users}})
}
else {
return [];
}
})
.then(users => {
result.users = exports.mapById(users);
return result;
});
};

exports.getRoomFlags = function(roomId) {
Expand Down Expand Up @@ -742,9 +761,10 @@ exports.clearMarketIntents = function(roomId) {
};


exports.mapById = function(array) {
exports.mapById = function(array, fn) {
return _.reduce(array, (result, i) => {
result[i._id.toString()] = i;
fn && fn(i);
return result;
}, {});
};
Expand Down Expand Up @@ -925,6 +945,19 @@ exports.getWorldSize = () => {
return worldSize;
};

exports.addRoomToUser = (roomId, user, bulk) => {
if(!user.rooms || user.rooms.indexOf(roomId) == -1) {
bulk.addToSet(user, 'rooms', roomId);
}
};

exports.removeRoomFromUser = (roomId, user, bulk) => {
if(user.rooms && user.rooms.indexOf(roomId) != -1) {
bulk.pull(user, 'rooms', roomId);
}
};


function EvalCodeError(message) {
this.toString = () => message;
}
Expand Down

0 comments on commit 13e0a7c

Please sign in to comment.