Skip to content

Commit

Permalink
Improve: Decrypt last message (#12173)
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok authored and sampaiodiego committed Sep 26, 2018
1 parent 939349d commit 59de13b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 32 deletions.
109 changes: 86 additions & 23 deletions packages/rocketchat-e2e/client/rocketchat.e2e.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
/* globals alerts, modal, ChatMessage */

import _ from 'underscore';
/* globals alerts, modal */

import './stylesheets/e2e';

Expand Down Expand Up @@ -38,8 +36,6 @@ class E2E {
this.readyPromise.then(() => {
this._ready.set(true);
});

this.decryptPendingMessagesDeferred = _.debounce(this.decryptPendingMessages.bind(this), 100);
}

isEnabled() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export function notifyDesktopUser({
sender: message.u,
type: room.t,
name: room.name,
message: {
msg: message.msg,
t: message.t,
},
},
});
}
Expand Down
8 changes: 6 additions & 2 deletions packages/rocketchat-ui-sidenav/client/sidebarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(/<br\s?\\?>/g, ' ');
const sender = this.user._id === currentData.lastMessage.u._id ? t('You') : otherUser;
Expand All @@ -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);
});
});

Expand Down
4 changes: 0 additions & 4 deletions packages/rocketchat-ui/client/lib/RoomHistoryManager.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -78,8 +76,6 @@ export const RoomHistoryManager = new class {
return;
}

e2e.decryptPendingMessagesDeferred();

let previousHeight;
const { messages = [] } = result;
room.unreadNotLoaded.set(result.unreadNotLoaded);
Expand Down
8 changes: 5 additions & 3 deletions packages/rocketchat-ui/client/lib/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down

0 comments on commit 59de13b

Please sign in to comment.