Skip to content

Commit 7ad3f25

Browse files
ggazzosampaiodiego
authored andcommitted
[IMPROVE] Message rendering time (#14252)
1 parent 40cab8f commit 7ad3f25

File tree

17 files changed

+103
-121
lines changed

17 files changed

+103
-121
lines changed

app/autotranslate/client/lib/actionButton.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ Meteor.startup(function() {
3131
const action = message.autoTranslateShowInverse ? '$unset' : '$set';
3232
Messages.update({ _id: message._id }, { [action]: { autoTranslateShowInverse: true } });
3333
},
34-
condition(message) {
35-
return message && message.u && message.u._id !== Meteor.userId() && message.translations && !message.translations.original;
34+
condition({ msg, u }) {
35+
return msg && msg.u && msg.u._id !== u._id && msg.translations && !msg.translations.original;
3636
},
3737
order: 90,
3838
});
@@ -56,8 +56,8 @@ Meteor.startup(function() {
5656
const action = message.autoTranslateShowInverse ? '$unset' : '$set';
5757
Messages.update({ _id: message._id }, { [action]: { autoTranslateShowInverse: true } });
5858
},
59-
condition(message) {
60-
return message && message.u && message.u._id !== Meteor.userId() && message.translations && message.translations.original;
59+
condition({ msg, u }) {
60+
return msg && msg.u && msg.u._id !== u._id && msg.translations && msg.translations.original;
6161
},
6262
order: 90,
6363
});

app/autotranslate/client/lib/autotranslate.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import { settings } from '../../../settings';
99
import { hasAtLeastOnePermission } from '../../../authorization';
1010
import { CachedCollectionManager } from '../../../ui-cached-collection';
1111

12+
let userLanguage = 'en';
13+
let username = '';
14+
15+
Meteor.startup(() => Tracker.autorun(() => {
16+
const user = Meteor.user();
17+
if (!user) {
18+
return;
19+
}
20+
userLanguage = user.language || 'en';
21+
username = user.username;
22+
}));
23+
1224
export const AutoTranslate = {
1325
findSubscriptionByRid: mem((rid) => Subscriptions.findOne({ rid })),
1426
messageIdsToWait: {},
@@ -19,7 +31,7 @@ export const AutoTranslate = {
1931
if (rid) {
2032
subscription = this.findSubscriptionByRid(rid);
2133
}
22-
const language = (subscription && subscription.autoTranslateLanguage) || Meteor.user().language || window.defaultUserLanguage();
34+
const language = (subscription && subscription.autoTranslateLanguage) || userLanguage || window.defaultUserLanguage();
2335
if (language.indexOf('-') !== -1) {
2436
if (!_.findWhere(this.supportedLanguages, { language })) {
2537
return language.substr(0, 2);
@@ -30,7 +42,7 @@ export const AutoTranslate = {
3042

3143
translateAttachments(attachments, language) {
3244
for (const attachment of attachments) {
33-
if (attachment.author_name !== Meteor.user().username) {
45+
if (attachment.author_name !== username) {
3446
if (attachment.text && attachment.translations && attachment.translations[language]) {
3547
attachment.text = attachment.translations[language];
3648
}

app/discussion/client/createDiscussionMessageAction.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,12 @@
11
import { Meteor } from 'meteor/meteor';
22
import { Tracker } from 'meteor/tracker';
33

4-
import { Subscriptions } from '../../models/client';
54
import { settings } from '../../settings/client';
65
import { hasPermission } from '../../authorization/client';
76
import { MessageAction, modal } from '../../ui-utils/client';
87
import { messageArgs } from '../../ui-utils/client/lib/messageArgs';
98
import { t } from '../../utils/client';
109

11-
const condition = (rid, uid) => {
12-
if (!Subscriptions.findOne({ rid })) {
13-
return false;
14-
}
15-
return uid !== Meteor.userId() ? hasPermission('start-discussion-other-user') : hasPermission('start-discussion');
16-
};
17-
1810
Meteor.startup(function() {
1911
Tracker.autorun(() => {
2012
if (!settings.get('Discussion_enabled')) {
@@ -43,11 +35,15 @@ Meteor.startup(function() {
4335
showCancelButton: false,
4436
});
4537
},
46-
condition({ rid, u: { _id: uid }, drid, dcount }) {
38+
condition({ msg: { u: { _id: uid }, drid, dcount }, subscription, u }) {
4739
if (drid || !isNaN(dcount)) {
4840
return false;
4941
}
50-
return condition(rid, uid);
42+
if (!subscription) {
43+
return false;
44+
}
45+
46+
return uid !== u._id ? hasPermission('start-discussion-other-user') : hasPermission('start-discussion');
5147
},
5248
order: 0,
5349
group: 'menu',

app/emoji/client/emojiParser.js

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,20 @@ Tracker.autorun(() => {
2525

2626
message.html = Object.entries(emoji.packages).reduce((value, [, emojiPackage]) => emojiPackage.render(value), message.html);
2727

28-
const checkEmojiOnly = $(`<div>${ message.html }</div>`);
29-
let emojiOnly = true;
28+
const checkEmojiOnly = document.createElement('div');
3029

30+
checkEmojiOnly.innerHTML = message.html;
3131

32-
for (let i = 0, len = checkEmojiOnly[0].childNodes.length; i < len; i++) {
33-
const childNode = checkEmojiOnly[0].childNodes[i];
32+
const emojis = Array.from(checkEmojiOnly.querySelectorAll('.emoji:not(:empty), .emojione:not(:empty)'));
3433

35-
if (childNode.classList && (childNode.classList.contains('emoji') || childNode.classList.contains('emojione'))) {
36-
childNode.classList.add('big');
37-
continue;
38-
}
39-
40-
if (s.trim(childNode.nodeValue) === '') {
41-
continue;
42-
}
43-
44-
emojiOnly = false;
45-
break;
34+
for (let i = 0, len = emojis.length; i < len; i++) {
35+
const { classList } = emojis[i];
36+
classList.add('big');
4637
}
38+
const emojiOnly = checkEmojiOnly.childNodes.length === emojis.length;
4739

4840
if (emojiOnly) {
49-
message.html = checkEmojiOnly.unwrap().html();
41+
message.html = checkEmojiOnly.innerHTML;
5042
}
5143

5244
// apostrophe (') back to &#39;

app/lib/client/lib/formatDate.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import moment from 'moment';
2+
import mem from 'mem';
23
import { Meteor } from 'meteor/meteor';
34
import { Tracker } from 'meteor/tracker';
45

@@ -49,4 +50,4 @@ export const timeAgo = (date) => moment(date).calendar(null, {
4950
sameElse,
5051
});
5152

52-
export const formatDate = (time) => moment(time).format(settings.get('Message_DateFormat'));
53+
export const formatDate = mem((time) => moment(time).format(settings.get('Message_DateFormat')), { maxAge: 5000 });

app/message-mark-as-unread/client/actionButton.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ Meteor.startup(() => {
2828
return FlowRouter.go('home');
2929
});
3030
},
31-
condition(message) {
32-
return Meteor.userId() && message.u._id !== Meteor.userId();
31+
condition({ msg, u }) {
32+
return msg.u._id !== u._id;
3333
},
3434
order: 10,
3535
group: 'menu',

app/message-pin/client/actionButton.js

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { RoomHistoryManager, MessageAction } from '../../ui-utils';
77
import { messageArgs } from '../../ui-utils/client/lib/messageArgs';
88
import { handleError } from '../../utils';
99
import { settings } from '../../settings';
10-
import { Subscriptions } from '../../models';
1110
import { hasAtLeastOnePermission } from '../../authorization';
1211

1312
Meteor.startup(function() {
@@ -25,12 +24,12 @@ Meteor.startup(function() {
2524
}
2625
});
2726
},
28-
condition(message) {
29-
if (!settings.get('Message_AllowPinning') || message.pinned || !Subscriptions.findOne({ rid: message.rid }, { fields: { _id: 1 } })) {
27+
condition({ msg, subscription }) {
28+
if (!settings.get('Message_AllowPinning') || msg.pinned || !subscription) {
3029
return false;
3130
}
3231

33-
return hasAtLeastOnePermission('pin-message', message.rid);
32+
return hasAtLeastOnePermission('pin-message', msg.rid);
3433
},
3534
order: 7,
3635
group: 'menu',
@@ -50,12 +49,12 @@ Meteor.startup(function() {
5049
}
5150
});
5251
},
53-
condition(message) {
54-
if (!settings.get('Message_AllowPinning') || !message.pinned || !Subscriptions.findOne({ rid: message.rid }, { fields: { _id: 1 } })) {
52+
condition({ msg, subscription }) {
53+
if (!subscription || !settings.get('Message_AllowPinning') || !msg.pinned) {
5554
return false;
5655
}
5756

58-
return hasAtLeastOnePermission('pin-message', message.rid);
57+
return hasAtLeastOnePermission('pin-message', msg.rid);
5958
},
6059
order: 8,
6160
group: 'menu',
@@ -73,11 +72,8 @@ Meteor.startup(function() {
7372
}
7473
return RoomHistoryManager.getSurroundingMessages(message, 50);
7574
},
76-
condition(message) {
77-
if (!Subscriptions.findOne({ rid: message.rid }, { fields: { _id: 1 } })) {
78-
return false;
79-
}
80-
return true;
75+
condition({ subscription }) {
76+
return !!subscription;
8177
},
8278
order: 100,
8379
group: 'menu',
@@ -94,11 +90,8 @@ Meteor.startup(function() {
9490
$(event.currentTarget).attr('data-clipboard-text', await MessageAction.getPermaLink(message._id));
9591
toastr.success(TAPi18n.__('Copied'));
9692
},
97-
condition(message) {
98-
if (!Subscriptions.findOne({ rid: message.rid }, { fields: { _id: 1 } })) {
99-
return false;
100-
}
101-
return true;
93+
condition({ subscription }) {
94+
return !!subscription;
10295
},
10396
order: 101,
10497
group: 'menu',

app/message-star/client/actionButton.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { TAPi18n } from 'meteor/tap:i18n';
44
import toastr from 'toastr';
55

66
import { handleError } from '../../utils';
7-
import { Subscriptions } from '../../models';
87
import { settings } from '../../settings';
98
import { RoomHistoryManager, MessageAction } from '../../ui-utils';
109
import { messageArgs } from '../../ui-utils/client/lib/messageArgs';
@@ -24,12 +23,12 @@ Meteor.startup(function() {
2423
}
2524
});
2625
},
27-
condition(message) {
28-
if (Subscriptions.findOne({ rid: message.rid }) == null || !settings.get('Message_AllowStarring')) {
26+
condition({ msg: message, subscription, u }) {
27+
if (subscription == null && settings.get('Message_AllowStarring')) {
2928
return false;
3029
}
3130

32-
return !message.starred || !message.starred.find((star) => star._id === Meteor.userId());
31+
return !message.starred || !message.starred.find((star) => star._id === u._id);
3332
},
3433
order: 9,
3534
group: 'menu',
@@ -49,12 +48,12 @@ Meteor.startup(function() {
4948
}
5049
});
5150
},
52-
condition(message) {
53-
if (Subscriptions.findOne({ rid: message.rid }) == null || !settings.get('Message_AllowStarring')) {
51+
condition({ msg: message, subscription, u }) {
52+
if (subscription == null && settings.get('Message_AllowStarring')) {
5453
return false;
5554
}
5655

57-
return message.starred && message.starred.find((star) => star._id === Meteor.userId());
56+
return message.starred && message.starred.find((star) => star._id === u._id);
5857
},
5958
order: 9,
6059
group: 'menu',
@@ -72,12 +71,12 @@ Meteor.startup(function() {
7271
}
7372
RoomHistoryManager.getSurroundingMessages(message, 50);
7473
},
75-
condition(message) {
76-
if (Subscriptions.findOne({ rid: message.rid }) == null || !settings.get('Message_AllowStarring')) {
74+
condition({ msg, subscription, u }) {
75+
if (subscription == null || !settings.get('Message_AllowStarring')) {
7776
return false;
7877
}
7978

80-
return message.starred && message.starred.find((star) => star._id === Meteor.userId());
79+
return msg.starred && msg.starred.find((star) => star._id === u._id);
8180
},
8281
order: 100,
8382
group: 'menu',
@@ -94,12 +93,12 @@ Meteor.startup(function() {
9493
$(event.currentTarget).attr('data-clipboard-text', await MessageAction.getPermaLink(message._id));
9594
toastr.success(TAPi18n.__('Copied'));
9695
},
97-
condition(message) {
98-
if (Subscriptions.findOne({ rid: message.rid }) == null || !settings.get('Message_AllowStarring')) {
96+
condition({ msg, subscription, u }) {
97+
if (subscription == null) {
9998
return false;
10099
}
101100

102-
return message.starred && message.starred.find((star) => star._id === Meteor.userId());
101+
return msg.starred && msg.starred.find((star) => star._id === u._id);
103102
},
104103
order: 101,
105104
group: 'menu',

app/reactions/client/init.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Meteor } from 'meteor/meteor';
22
import { Blaze } from 'meteor/blaze';
33
import { Template } from 'meteor/templating';
44

5-
import { Rooms, Subscriptions } from '../../models';
5+
import { Rooms } from '../../models';
66
import { MessageAction } from '../../ui-utils';
77
import { messageArgs } from '../../ui-utils/client/lib/messageArgs';
88
import { EmojiPicker } from '../../emoji';
@@ -68,10 +68,7 @@ Meteor.startup(function() {
6868
const { msg } = messageArgs(this);
6969
EmojiPicker.open(event.currentTarget, (emoji) => Meteor.call('setReaction', `:${ emoji }:`, msg._id));
7070
},
71-
condition(message) {
72-
const room = Rooms.findOne({ _id: message.rid });
73-
const user = Meteor.user();
74-
71+
condition({ msg: message, u: user, room, subscription }) {
7572
if (!room) {
7673
return false;
7774
}
@@ -86,7 +83,7 @@ Meteor.startup(function() {
8683
return false;
8784
}
8885

89-
if (!Subscriptions.findOne({ rid: message.rid })) {
86+
if (!subscription) {
9087
return false;
9188
}
9289

app/threads/client/messageAction/follow.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ Meteor.startup(function() {
2020
const { msg } = messageArgs(this);
2121
call('followMessage', { mid: msg._id });
2222
},
23-
condition({ tmid, replies = [] }) {
23+
condition({ msg: { tmid, replies = [] }, u }) {
2424
if (tmid) {
2525
const parentMessage = Messages.findOne({ _id: tmid }, { fields: { replies: 1 } });
2626
if (parentMessage) {
2727
replies = parentMessage.replies || [];
2828
}
2929
}
30-
return !replies.includes(Meteor.userId());
30+
return !replies.includes(u._id);
3131
},
3232
order: 0,
3333
group: 'menu',

0 commit comments

Comments
 (0)