getrooms just get updated rooms

This commit is contained in:
Guilherme Gazzo 2017-11-19 02:09:56 -02:00
parent 441016330b
commit d4d031e2e7
1 changed files with 23 additions and 15 deletions

View File

@ -347,23 +347,31 @@ const RocketChat = {
}); });
} }
}, },
getRooms() { async getRooms() {
return Promise.all([call('subscriptions/get'), call('rooms/get')]).then(([subscriptions, rooms]) => {
const { server, login } = reduxStore.getState(); const { server, login } = reduxStore.getState();
let lastMessage = realm
.objects('subscriptions')
.filtered('_server.id = $0', server.server)
.sorted('_updatedAt', true)[0];
lastMessage = lastMessage && new Date(lastMessage._updatedAt);
let [subscriptions, rooms] = await Promise.all([call('subscriptions/get', lastMessage), call('rooms/get', lastMessage)]);
if (lastMessage) {
subscriptions = subscriptions.update;
rooms = rooms.update;
}
const data = subscriptions.map((subscription) => { const data = subscriptions.map((subscription) => {
subscription._updatedAt = (rooms.find(room => room._id === subscription.rid) || {})._updatedAt; subscription._updatedAt = (rooms.find(room => room._id === subscription.rid) || subscription)._updatedAt;
subscription._server = { id: server.server }; subscription._server = { id: server.server };
return subscription; return subscription;
}); });
realm.write(() => { realm.write(() => {
data.forEach((subscription) => { data.forEach(subscription =>
realm.create('subscriptions', subscription, true); realm.create('subscriptions', subscription, true));
});
}); });
Meteor.subscribe('stream-notify-user', `${ login.user.id }/subscriptions-changed`, false); Meteor.subscribe('stream-notify-user', `${ login.user.id }/subscriptions-changed`, false);
return data; return data;
});
}, },
logout({ server }) { logout({ server }) {
Meteor.logout(); Meteor.logout();