From a72612a2b1bad25c389845ca7f110aeed17c210b Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Wed, 26 Sep 2018 16:01:27 -0300 Subject: [PATCH] Decrypt last message --- .../rocketchat-e2e/client/rocketchat.e2e.js | 109 ++++++++++++++---- .../server/functions/notifications/desktop.js | 4 + .../client/sidebarItem.js | 8 +- .../client/lib/RoomHistoryManager.js | 4 - .../rocketchat-ui/client/lib/notification.js | 8 +- 5 files changed, 101 insertions(+), 32 deletions(-) diff --git a/packages/rocketchat-e2e/client/rocketchat.e2e.js b/packages/rocketchat-e2e/client/rocketchat.e2e.js index a5f6818102aa..61c32ca29c72 100644 --- a/packages/rocketchat-e2e/client/rocketchat.e2e.js +++ b/packages/rocketchat-e2e/client/rocketchat.e2e.js @@ -1,6 +1,4 @@ -/* globals alerts, modal, ChatMessage */ - -import _ from 'underscore'; +/* globals alerts, modal */ import './stylesheets/e2e'; @@ -38,8 +36,6 @@ class E2E { this.readyPromise.then(() => { this._ready.set(true); }); - - this.decryptPendingMessagesDeferred = _.debounce(this.decryptPendingMessages.bind(this), 100); } isEnabled() { @@ -173,12 +169,13 @@ class E2E { this.readyPromise.resolve(); - this.setupListener(); + this.setupListeners(); this.decryptPendingMessages(); + this.decryptPendingSubscriptions(); } - setupListener() { + setupListeners() { RocketChat.Notifications.onUser('e2ekeyRequest', async(roomId, keyId) => { const e2eRoom = await this.getInstanceByRoomId(roomId); if (!e2eRoom) { @@ -187,6 +184,22 @@ class E2E { e2eRoom.provideKeyToUser(keyId); }); + + RocketChat.models.Subscriptions.after.update((userId, doc) => { + this.decryptSubscription(doc); + }); + + RocketChat.models.Subscriptions.after.insert((userId, doc) => { + this.decryptSubscription(doc); + }); + + RocketChat.models.Messages.after.update((userId, doc) => { + this.decryptMessage(doc); + }); + + RocketChat.models.Messages.after.insert((userId, doc) => { + this.decryptMessage(doc); + }); } async loadKeysFromDB() { @@ -307,32 +320,82 @@ class E2E { } } + async decryptMessage(message) { + if (!this.isEnabled()) { + return; + } + + if (message.t !== 'e2e' || message.e2e === 'done') { + return; + } + + const e2eRoom = await this.getInstanceByRoomId(message.rid); + + if (!e2eRoom) { + return; + } + + const data = await e2eRoom.decrypt(message.msg); + if (!data) { + return; + } + + RocketChat.models.Messages.direct.update({ _id: message._id }, { + $set: { + msg: data.text, + e2e: 'done', + }, + }); + } + async decryptPendingMessages() { if (!this.isEnabled()) { return; } - return await ChatMessage.find({ t: 'e2e', e2e: 'pending' }).forEach(async(item) => { - const e2eRoom = await this.getInstanceByRoomId(item.rid); + return await RocketChat.models.Messages.find({ t: 'e2e', e2e: 'pending' }).forEach(async(item) => { + await this.decryptMessage(item); + }); + } - if (!e2eRoom) { - return; - } + async decryptSubscription(subscription) { + if (!this.isEnabled()) { + return; + } - const data = await e2eRoom.decrypt(item.msg); - if (!data) { - return; - } + if (!subscription.lastMessage || subscription.lastMessage.t !== 'e2e' || subscription.lastMessage.e2e === 'done') { + return; + } - item.msg = data.text; - item.ack = data.ack; - if (data.ts) { - item.ts = data.ts; - } - item.e2e = 'done'; - ChatMessage.upsert({ _id: item._id }, item); + const e2eRoom = await this.getInstanceByRoomId(subscription.rid); + + if (!e2eRoom) { + return; + } + + const data = await e2eRoom.decrypt(subscription.lastMessage.msg); + if (!data) { + return; + } + + RocketChat.models.Subscriptions.direct.update({ + _id: subscription._id, + }, { + $set: { + 'lastMessage.msg': data.text, + 'lastMessage.e2e': 'done', + }, }); } + + async decryptPendingSubscriptions() { + RocketChat.models.Subscriptions.find({ + 'lastMessage.t': 'e2e', + 'lastMessage.e2e': { + $ne: 'done', + }, + }).forEach(this.decryptSubscription.bind(this)); + } } export const e2e = new E2E(); diff --git a/packages/rocketchat-lib/server/functions/notifications/desktop.js b/packages/rocketchat-lib/server/functions/notifications/desktop.js index 1e41c572a05e..18bebf99b1ab 100644 --- a/packages/rocketchat-lib/server/functions/notifications/desktop.js +++ b/packages/rocketchat-lib/server/functions/notifications/desktop.js @@ -29,6 +29,10 @@ export function notifyDesktopUser({ sender: message.u, type: room.t, name: room.name, + message: { + msg: message.msg, + t: message.t, + }, }, }); } diff --git a/packages/rocketchat-ui-sidenav/client/sidebarItem.js b/packages/rocketchat-ui-sidenav/client/sidebarItem.js index e4a201fcc3c9..3ea87245b6fd 100644 --- a/packages/rocketchat-ui-sidenav/client/sidebarItem.js +++ b/packages/rocketchat-ui-sidenav/client/sidebarItem.js @@ -70,6 +70,12 @@ Template.sidebarItem.onCreated(function() { return this.renderedMessage = currentData.lastMessage.msg; } + setLastMessageTs(this, currentData.lastMessage.ts); + + if (currentData.lastMessage.t === 'e2e' && currentData.lastMessage.e2e !== 'done') { + return this.renderedMessage = '******'; + } + const otherUser = RocketChat.settings.get('UI_Use_Real_Name') ? currentData.lastMessage.u.name || currentData.lastMessage.u.username : currentData.lastMessage.u.username; const renderedMessage = renderMessageBody(currentData.lastMessage).replace(//g, ' '); const sender = this.user._id === currentData.lastMessage.u._id ? t('You') : otherUser; @@ -79,8 +85,6 @@ Template.sidebarItem.onCreated(function() { } else { this.renderedMessage = currentData.lastMessage.msg === '' ? t('user_sent_an_attachment', { user: sender }) : `${ sender }: ${ renderedMessage }`; } - - setLastMessageTs(this, currentData.lastMessage.ts); }); }); diff --git a/packages/rocketchat-ui/client/lib/RoomHistoryManager.js b/packages/rocketchat-ui/client/lib/RoomHistoryManager.js index 857fa935dbbd..71c2acd7a39e 100644 --- a/packages/rocketchat-ui/client/lib/RoomHistoryManager.js +++ b/packages/rocketchat-ui/client/lib/RoomHistoryManager.js @@ -1,6 +1,5 @@ /* globals readMessage UserRoles RoomRoles*/ import _ from 'underscore'; -import { e2e } from 'meteor/rocketchat:e2e'; export const upsertMessage = ({ msg, subscription }) => { const userId = msg.u && msg.u._id; @@ -15,7 +14,6 @@ export const upsertMessage = ({ msg, subscription }) => { msg.roles = _.union.apply(_.union, roles); if (msg.t === 'e2e' && !msg.file) { msg.e2e = 'pending'; - e2e.decryptPendingMessagesDeferred(); } return ChatMessage.upsert({ _id: msg._id }, msg); @@ -78,8 +76,6 @@ export const RoomHistoryManager = new class { return; } - e2e.decryptPendingMessagesDeferred(); - let previousHeight; const { messages = [] } = result; room.unreadNotLoaded.set(result.unreadNotLoaded); diff --git a/packages/rocketchat-ui/client/lib/notification.js b/packages/rocketchat-ui/client/lib/notification.js index daf731f39f74..3686fd9b083a 100644 --- a/packages/rocketchat-ui/client/lib/notification.js +++ b/packages/rocketchat-ui/client/lib/notification.js @@ -73,9 +73,11 @@ const KonchatNotification = { return; } - const e2eRoom = await e2e.getInstanceByRoomId(notification.payload.rid); - if (e2eRoom) { - notification.text = (await e2eRoom.decrypt(notification.text)).text; + if (notification.payload.message && notification.payload.message.t === 'e2e') { + const e2eRoom = await e2e.getInstanceByRoomId(notification.payload.rid); + if (e2eRoom) { + notification.text = (await e2eRoom.decrypt(notification.payload.message.msg)).text; + } } /* globals getAvatarAsPng*/