Skip to content

Commit

Permalink
Merge pull request #8529 from RocketChat/improvements/fast-room-sync
Browse files Browse the repository at this point in the history
Improve room sync speed
  • Loading branch information
rodrigok authored Oct 18, 2017
2 parents 7a7e927 + 7ca99c7 commit ae9a9f4
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions server/publications/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ const fields = {
};


const roomMap = (record, fields) => {
const roomMap = (record) => {
if (record._room) {
return _.pick(record._room, ...Object.keys(fields));
}
console.log('Empty Room for Subscription', record);
return {};
};

function getFieldsForUserId(/*userId*/) {
return fields;
}

Meteor.methods({
'rooms/get'(updatedAt) {
let options = {fields};
Expand All @@ -56,7 +52,7 @@ Meteor.methods({
this.unblock();

options = {
fields: getFieldsForUserId(this.userId)
fields
};

if (updatedAt instanceof Date) {
Expand Down Expand Up @@ -92,25 +88,24 @@ Meteor.methods({
throw new Meteor.Error('error-no-permission', 'No permission', { method: 'getRoomByTypeAndName' });
}

return roomMap({_room: room}, getFieldsForUserId(this.userId));
return roomMap({_room: room});
}
});

RocketChat.models.Rooms.cache.on('sync', (type, room/*, diff*/) => {
const records = RocketChat.models.Subscriptions.findByRoomId(room._id).fetch();

const _room = roomMap({_room: room});
for (const record of records) {
const user = RocketChat.models.Users.findOneById(record.u._id);
if (user && (user.statusConnection === 'online' || user.statusConnection === 'away')) {
RocketChat.Notifications.notifyUserInThisInstance(record.u._id, 'rooms-changed', type, roomMap({_room: room}, getFieldsForUserId(record.u._id)));
}
RocketChat.Notifications.notifyUserInThisInstance(record.u._id, 'rooms-changed', type, _room);
}
});

RocketChat.models.Subscriptions.on('changed', (type, subscription/*, diff*/) => {
if (type === 'inserted' || type === 'removed') {
const room = RocketChat.models.Rooms.findOneById(subscription.rid);
if (room) {
RocketChat.Notifications.notifyUserInThisInstance(subscription.u._id, 'rooms-changed', type, roomMap({_room: room}, getFieldsForUserId(subscription.u._id)));
RocketChat.Notifications.notifyUserInThisInstance(subscription.u._id, 'rooms-changed', type, roomMap({_room: room}));
}
}
});

0 comments on commit ae9a9f4

Please sign in to comment.