Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
ggazzo committed May 3, 2019
1 parent d3585e3 commit 2204e27
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/ui-message/client/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ Template.message.onCreated(function() {
const { msg, settings } = Template.currentData();

this.wasEdited = msg.editedAt && !MessageTypes.isSystemMessage(msg);
if (msg.tmid && !msg.threadMsg) {
if (settings.shouldCollapseReplies && msg.tmid && !msg.threadMsg) {
findParentMessage(msg.tmid);
}
return this.body = Tracker.nonreactive(() => renderBody(msg, settings));
Expand Down
5 changes: 2 additions & 3 deletions app/ui-utils/client/lib/RoomHistoryManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import mem from 'mem';
import s from 'underscore.string';
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
Expand All @@ -9,8 +8,8 @@ import { getConfig } from '../config';
import { RoomManager } from './RoomManager';
import { readMessage } from './readMessages';
import { renderMessageBody } from './renderMessageBody';
export const normalizeThreadMessage = (message) => {

export const normalizeThreadMessage = mem((message) => {
if (message.msg) {
return renderMessageBody(message).replace(/<br\s?\\?>/g, ' ');
}
Expand All @@ -26,7 +25,7 @@ export const normalizeThreadMessage = mem((message) => {
return s.escapeHTML(attachment.title);
}
}
}, { maxAge: 1000 });
};

export const upsertMessage = ({ msg: { _id, ...msg }, subscription }) => {
const userId = msg.u && msg.u._id;
Expand Down
25 changes: 23 additions & 2 deletions app/ui-utils/client/lib/renderMessageBody.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
import { callbacks } from '../../../callbacks';
import s from 'underscore.string';

export const renderMessageBody = (message) => {
const generateKeyDefault = (...args) => args.map((item) => JSON.stringify(item)).join('-');

const mem = (fn, tm = 500, generateKey = generateKeyDefault) => {
const cache = {};
const timeout = {};

const invalidateCache = (key) => delete cache[key];
return (...args) => {
const key = generateKey(...args);
if (!cache[key]) {
cache[key] = fn(...args);
}
if (timeout[key]) {
clearTimeout(timeout[key]);
}
timeout[key] = setTimeout(invalidateCache, tm, key);
return cache[key];
};
};

export const renderMessageBody = mem((message) => {

message.html = s.trim(message.msg) ? s.escapeHTML(message.msg) : '';

const { tokens, html } = callbacks.run('renderMessage', message);

return (Array.isArray(tokens) ? tokens.reverse() : [])
.reduce((html, { token, text }) => html.replace(token, () => text), html);
};
}, 5000, ({ _id, _updatedAt }) => (_id + _updatedAt));

0 comments on commit 2204e27

Please sign in to comment.