From ee9b2bc53a7f73da44383af176fccb95e7cdefa5 Mon Sep 17 00:00:00 2001 From: Aditya Date: Sun, 3 Nov 2019 15:56:51 +0530 Subject: [PATCH 1/2] Message prefetch added --- app/models/client/models/ChatMessage.js | 112 ++++++++++++++++++++++++ 1 file changed, 112 insertions(+) diff --git a/app/models/client/models/ChatMessage.js b/app/models/client/models/ChatMessage.js index 6579b95b95d1..dfe8c24462d3 100644 --- a/app/models/client/models/ChatMessage.js +++ b/app/models/client/models/ChatMessage.js @@ -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) { @@ -12,3 +21,106 @@ ChatMessage.unsetReactions = function(messageId) { }; new PersistentMinimongo2(ChatMessage, 'Message'); + +const normalizeThreadMessage = (message) => { + if (message.msg) { + return renderMessageBody(message).replace(//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, + }); + }); + }); + } +}); From b24543df3130efd0cb46c882a22ac994cc04c0b4 Mon Sep 17 00:00:00 2001 From: Aditya Date: Sat, 9 Nov 2019 13:12:05 +0530 Subject: [PATCH 2/2] Message autoremoval fixed --- app/ui-utils/client/lib/RoomHistoryManager.js | 1 - 1 file changed, 1 deletion(-) diff --git a/app/ui-utils/client/lib/RoomHistoryManager.js b/app/ui-utils/client/lib/RoomHistoryManager.js index f7ed31f3df5e..c8b6455f8318 100644 --- a/app/ui-utils/client/lib/RoomHistoryManager.js +++ b/app/ui-utils/client/lib/RoomHistoryManager.js @@ -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);