Skip to content

Commit

Permalink
[IMPROVE] Open rooms quicker (#13417)
Browse files Browse the repository at this point in the history
* [IMPROVE] Open rooms quicker

* Close observer on room destroy

* Fix tests

* Back to cursor and improve permission query

* More improvements
  • Loading branch information
rodrigok authored and sampaiodiego committed Feb 14, 2019
1 parent fa7432b commit 212f114
Show file tree
Hide file tree
Showing 10 changed files with 211 additions and 147 deletions.
296 changes: 164 additions & 132 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
"lru-cache": "^5.1.1",
"mailparser": "^2.4.3",
"marked": "^0.5.2",
"mem": "4.1.0",
"meteor-node-stubs": "^0.4.1",
"mime-db": "^1.37.0",
"mime-type": "^3.0.7",
Expand Down
9 changes: 6 additions & 3 deletions packages/rocketchat-autotranslate/client/lib/autotranslate.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { RocketChat } from 'meteor/rocketchat:lib';
import _ from 'underscore';
import mem from 'mem';

const findSubscriptionByRid = mem((rid) => RocketChat.models.Subscriptions.findOne({ rid }));

RocketChat.AutoTranslate = {
messageIdsToWait: {},
Expand All @@ -10,7 +13,7 @@ RocketChat.AutoTranslate = {
getLanguage(rid) {
let subscription = {};
if (rid) {
subscription = RocketChat.models.Subscriptions.findOne({ rid }, { fields: { autoTranslateLanguage: 1 } });
subscription = findSubscriptionByRid(rid);
}
const language = (subscription && subscription.autoTranslateLanguage) || Meteor.user().language || window.defaultUserLanguage();
if (language.indexOf('-') !== -1) {
Expand Down Expand Up @@ -48,7 +51,7 @@ RocketChat.AutoTranslate = {
Tracker.autorun(() => {
if (RocketChat.settings.get('AutoTranslate_Enabled') && RocketChat.authz.hasAtLeastOnePermission(['auto-translate'])) {
RocketChat.callbacks.add('renderMessage', (message) => {
const subscription = RocketChat.models.Subscriptions.findOne({ rid: message.rid }, { fields: { autoTranslate: 1, autoTranslateLanguage: 1 } });
const subscription = findSubscriptionByRid(message.rid);
const autoTranslateLanguage = this.getLanguage(message.rid);
if (message.u && message.u._id !== Meteor.userId()) {
if (!message.translations) {
Expand All @@ -72,7 +75,7 @@ RocketChat.AutoTranslate = {

RocketChat.callbacks.add('streamMessage', (message) => {
if (message.u && message.u._id !== Meteor.userId()) {
const subscription = RocketChat.models.Subscriptions.findOne({ rid: message.rid }, { fields: { autoTranslate: 1, autoTranslateLanguage: 1 } });
const subscription = findSubscriptionByRid(message.rid);
const language = this.getLanguage(message.rid);
if (subscription && subscription.autoTranslate === true && ((message.msg && (!message.translations || !message.translations[language])))) { // || (message.attachments && !_.find(message.attachments, attachment => { return attachment.translations && attachment.translations[language]; }))
RocketChat.models.Messages.update({ _id: message._id }, { $set: { autoTranslateFetching: true } });
Expand Down
3 changes: 3 additions & 0 deletions packages/rocketchat-lib/client/lib/openRoom.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ openRoom = function(type, name) {
BlazeLayout.render('main', { modal: RocketChat.Layout.isEmbedded(), center: 'loading' });
return;
}

BlazeLayout.render('main');

if (currentTracker) {
currentTracker = undefined;
}
Expand Down
14 changes: 8 additions & 6 deletions packages/rocketchat-models/client/models/Subscriptions.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
import { Users } from '..';
import _ from 'underscore';
import mem from 'mem';

const Subscriptions = {};

Object.assign(Subscriptions, {
isUserInRole(userId, roleName, roomId) {
isUserInRole: mem(function(userId, roleName, roomId) {
if (roomId == null) {
return false;
}

const query = {
rid: roomId,
roles: roleName,
};

return !_.isUndefined(this.findOne(query));
},
const subscription = this.findOne(query);

findUsersInRoles(roles, scope, options) {
return subscription && Array.isArray(subscription.roles) && subscription.roles.includes(roleName);
}, { maxAge: 1000 }),

findUsersInRoles: mem(function(roles, scope, options) {
roles = [].concat(roles);

const query = {
Expand All @@ -37,7 +39,7 @@ Object.assign(Subscriptions, {
}));

return Users.find({ _id: { $in: users } }, options);
},
}, { maxAge: 1000 }),
});

export { Subscriptions };
5 changes: 5 additions & 0 deletions packages/rocketchat-ui-utils/client/lib/MessageAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { settings } from 'meteor/rocketchat:settings';
import _ from 'underscore';
import moment from 'moment';
import toastr from 'toastr';
import mem from 'mem';

const call = (method, ...args) => new Promise((resolve, reject) => {
Meteor.call(method, ...args, function(err, data) {
Expand Down Expand Up @@ -56,6 +57,10 @@ export const MessageAction = new class {
config.group = 'menu';
}

if (config.condition) {
config.condition = mem(config.condition);
}

return Tracker.nonreactive(() => {
const btns = this.buttons.get();
btns[config.id] = config;
Expand Down
16 changes: 15 additions & 1 deletion packages/rocketchat-ui-utils/client/lib/RoomHistoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ export const upsertMessage = ({ msg, subscription }) => {
return ChatMessage.upsert({ _id: msg._id }, msg);
};

function upsertMessageBulk({ msgs, subscription }) {
const { queries } = ChatMessage;
ChatMessage.queries = [];
msgs.forEach((msg, index) => {
if (index === msgs.length - 1) {
ChatMessage.queries = queries;
}
upsertMessage({ msg, subscription });
});
}

export const RoomHistoryManager = new class {
constructor() {
this.defaultLimit = 50;
Expand Down Expand Up @@ -91,7 +102,10 @@ export const RoomHistoryManager = new class {
previousHeight = wrapper.scrollHeight;
}

messages.forEach((msg) => msg.t !== 'command' && upsertMessage({ msg, subscription }));
upsertMessageBulk({
msgs: messages.filter((msg) => msg.t !== 'command'),
subscription,
});

if (wrapper) {
const heightDiff = wrapper.scrollHeight - previousHeight;
Expand Down
7 changes: 3 additions & 4 deletions packages/rocketchat-ui-utils/client/lib/RoomManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,9 @@ export const RoomManager = new function() {
Notifications.onRoom(openedRooms[typeName].rid, 'deleteMessageBulk', onDeleteMessageBulkStream); // eslint-disable-line no-use-before-define
}
}
Meteor.defer(() => {
record.ready = true;
Dep.changed();
});

record.ready = true;
Dep.changed();
});
});
}
Expand Down
5 changes: 4 additions & 1 deletion packages/rocketchat-ui/client/views/app/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ Template.room.events({
roomTypes.openRouteLink('d', { name: this._arguments[1].u.username }, { ...FlowRouter.current().queryParams, reply: message._id });
},
'click, touchend'(e, t) {
Meteor.setTimeout(() => t.sendToBottomIfNecessaryDebounced(), 100);
Meteor.setTimeout(() => t.sendToBottomIfNecessaryDebounced && t.sendToBottomIfNecessaryDebounced(), 100);
},

'click .messages-container-main'() {
Expand Down Expand Up @@ -982,6 +982,9 @@ Template.room.onCreated(function() {
}); // Update message to re-render DOM

Template.room.onDestroyed(function() {
if (this.messageObserver) {
this.messageObserver.stop();
}
window.removeEventListener('resize', this.onWindowResize);
});

Expand Down
2 changes: 2 additions & 0 deletions tests/end-to-end/ui/07-emoji.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ describe('[Emoji]', () => {
});

it('it should be that the value on the message is the same as the emoji clicked', () => {
browser.pause(100);
mainContent.lastMessage.getText().should.equal('😀');
});
});
Expand Down Expand Up @@ -132,6 +133,7 @@ describe('[Emoji]', () => {
});

it('it should be that the value on the message is the same as the emoji clicked', () => {
browser.pause(100);
mainContent.lastMessage.getText().should.equal('😄');
});
});
Expand Down

0 comments on commit 212f114

Please sign in to comment.