Skip to content

Commit

Permalink
Merge pull request RocketChat#95 from bhardwajaditya/pwa
Browse files Browse the repository at this point in the history
Prefetch Messages
  • Loading branch information
kb0304 authored Nov 14, 2019
2 parents 0c862e0 + b24543d commit 76d1810
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 1 deletion.
112 changes: 112 additions & 0 deletions app/models/client/models/ChatMessage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
import s from 'underscore.string';
import { Tracker } from 'meteor/tracker';
import { PersistentMinimongo2 } from 'meteor/frozeman:persistent-minimongo2';

import { CachedChatSubscription } from './CachedChatSubscription';
import { ChatSubscription } from './ChatSubscription';
import { getConfig } from '../../../ui-utils/client/config';
import { renderMessageBody } from '../../../ui-utils/client/lib/renderMessageBody';
import { promises } from '../../../promises/client';

export const ChatMessage = new Mongo.Collection(null);

ChatMessage.setReactions = function(messageId, reactions) {
Expand All @@ -12,3 +21,106 @@ ChatMessage.unsetReactions = function(messageId) {
};

new PersistentMinimongo2(ChatMessage, 'Message');

const normalizeThreadMessage = (message) => {
if (message.msg) {
return renderMessageBody(message).replace(/<br\s?\\?>/g, ' ');
}

if (message.attachments) {
const attachment = message.attachments.find((attachment) => attachment.title || attachment.description);

if (attachment && attachment.description) {
return s.escapeHTML(attachment.description);
}

if (attachment && attachment.title) {
return s.escapeHTML(attachment.title);
}
}
};

const upsertMessage = async ({ msg, subscription, uid = Tracker.nonreactive(() => Meteor.userId()) }, collection = ChatMessage) => {
const userId = msg.u && msg.u._id;

if (subscription && subscription.ignored && subscription.ignored.indexOf(userId) > -1) {
msg.ignored = true;
}

if (msg.t === 'e2e' && !msg.file) {
msg.e2e = 'pending';
}
msg = await promises.run('onClientMessageReceived', msg) || msg;

const { _id, ...messageToUpsert } = msg;

if (msg.tcount) {
collection.direct.update({ tmid: _id }, {
$set: {
following: msg.replies && msg.replies.indexOf(uid) > -1,
threadMsg: normalizeThreadMessage(messageToUpsert),
repliesCount: msg.tcount,
},
}, { multi: true });
}

return collection.direct.upsert({ _id }, messageToUpsert);
};

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

let messagesFetched = false;
Tracker.autorun(() => {
if (!messagesFetched && CachedChatSubscription.ready.get()) {
const status = Meteor.status();
if (status.status !== 'connected') {
return;
}
messagesFetched = true;
const subscriptions = ChatSubscription.find(
{
open: true,
},
{
fields: {
rid: 1,
ls: 1,
},
}
);
const limit = parseInt(getConfig('roomListLimit')) || 50;
subscriptions.forEach((subscription) => {
let ts;
const { rid, ls } = subscription;
const lastMessage = ChatMessage.findOne(
{ rid, _hidden: { $ne: true } },
{ sort: { ts: 1 } }
);
if (lastMessage) {
({ ts } = lastMessage);
} else {
ts = undefined;
}
Meteor.call('loadHistory', rid, ts, limit, ls, (err, result) => {
if (err) {
return;
}
const { messages = [] } = result;
upsertMessageBulk({
msgs: messages.filter((msg) => msg.t !== 'command'),
subscription,
});
});
});
}
});
1 change: 0 additions & 1 deletion app/ui-utils/client/lib/RoomHistoryManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,6 @@ export const RoomHistoryManager = new class {
}

clear(rid) {
ChatMessage.remove({ rid });
if (this.histories[rid]) {
this.histories[rid].hasMore.set(true);
this.histories[rid].isLoading.set(false);
Expand Down

0 comments on commit 76d1810

Please sign in to comment.