Skip to content

Commit

Permalink
Regression: sidebar sorting was being wrong in some cases where the r…
Browse files Browse the repository at this point in the history
…ooms records were returned before the subscriptions (#11273)

* fix sort sidebar

* fix review

* Update roomList.js

* Update cachedCollection.js
  • Loading branch information
ggazzo authored and rodrigok committed Jun 27, 2018
1 parent 7233097 commit 204baf6
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions packages/rocketchat-ui-sidenav/client/roomList.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* globals RocketChat */
import { UiTextContext } from 'meteor/rocketchat:lib';
import _ from 'underscore';

Template.roomList.helpers({
rooms() {
Expand Down Expand Up @@ -110,26 +109,35 @@ const getLowerCaseNames = (room, nameDefault = '') => {
};
};

// RocketChat.Notifications['onUser']('rooms-changed', );

const mergeSubRoom = (record/*, t*/) => {
const room = Tracker.nonreactive(() => RocketChat.models.Rooms.findOne({ _id: record.rid }));
if (!room) {
return record;
}
record.lastMessage = room.lastMessage;
record.lm = room._updatedAt;
return _.extend(record, getLowerCaseNames(record));
const mergeSubRoom = subscription => {
const room = RocketChat.models.Rooms.findOne(subscription.rid) || { _updatedAt: subscription.ts };
subscription.lastMessage = room.lastMessage;
subscription.lm = room._updatedAt;
return Object.assign(subscription, getLowerCaseNames(subscription));
};

RocketChat.callbacks.add('cachedCollection-received-rooms', (room) => {
const mergeRoomSub = room => {
const sub = RocketChat.models.Subscriptions.findOne({ rid: room._id });
if (!sub) {
return;
return room;
}
const $set = {lastMessage : room.lastMessage, lm: room._updatedAt, ...getLowerCaseNames(room, sub.name)};
RocketChat.models.Subscriptions.update({ rid: room._id }, {$set});
});

RocketChat.models.Subscriptions.update({
rid: room._id
}, {
$set: {
lastMessage: room.lastMessage,
lm: room._updatedAt,
...getLowerCaseNames(room, sub.name)
}
});

return room;
};

RocketChat.callbacks.add('cachedCollection-received-rooms', mergeRoomSub);
RocketChat.callbacks.add('cachedCollection-sync-rooms', mergeRoomSub);
RocketChat.callbacks.add('cachedCollection-loadFromServer-rooms', mergeRoomSub);

RocketChat.callbacks.add('cachedCollection-received-subscriptions', mergeSubRoom);
RocketChat.callbacks.add('cachedCollection-sync-subscriptions', mergeSubRoom);
Expand Down

0 comments on commit 204baf6

Please sign in to comment.