From 07bbe358ea457784e78654cf99e8d2e35345189f Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 15 Mar 2016 15:07:26 +0000 Subject: [PATCH] Clean up a codepath that was only used for crypto messages Transmission of encrypted messages was happening somewhat differently to normal messages. In particular, we weren't copying the 'unsigned' field when we got the remote-echo, which meant the 'sync' code didn't correctly match up the echo with the original. The separate codepath was becoming a thorn in my side, so fix things up to bring it back in line. --- lib/client.js | 26 +++----------------------- lib/models/room.js | 7 ++++++- 2 files changed, 9 insertions(+), 24 deletions(-) diff --git a/lib/client.js b/lib/client.js index 6f40f7aa5..19395744a 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1112,6 +1112,7 @@ function _decryptMessage(client, event) { room_id: payload.room_id, user_id: event.getSender(), event_id: event.getId(), + unsigned: event.getUnsigned(), type: payload.type, content: payload.content }, "encrypted"); @@ -1129,6 +1130,7 @@ function _badEncryptedMessage(event, reason) { room_id: event.getRoomId(), user_id: event.getSender(), event_id: event.getId(), + unsigned: event.getUnsigned(), content: { msgtype: "m.bad.encrypted", body: reason, @@ -1170,29 +1172,7 @@ function _sendEvent(client, room, event, callback) { // FIXME: This manipulation of the room should probably be done // inside the room class, not by the client. var timeline = room.getTimelineForEvent(eventId); - if (timeline) { - // we've already received the event via the event stream. - // update it and remove the fake event. - // - // (This codepath is only useful for encrypted events; for - // normal events, everything here is already done when we - // got the echo). - var matchingEvent = - utils.findElement(timeline.getEvents(), function(ev) { - return ev.getId() === eventId; - }, true); - if (event.encryptedType) { - // Replace the content and type of the event with the - // plaintext that we sent to the server. - // TODO: Persist the changes if we storing events somewhere - // otherthan in memory. - matchingEvent.event.content = event.event.content; - matchingEvent.event.type = event.event.type; - } - room.removeEvents([localEventId]); - matchingEvent.status = null; // make sure it's still marked as sent - } - else { + if (!timeline) { // we haven't yet received the event from the stream; we // need to update the fake event with the right event id. // diff --git a/lib/models/room.js b/lib/models/room.js index 3e24c7143..fb0790b80 100644 --- a/lib/models/room.js +++ b/lib/models/room.js @@ -621,8 +621,13 @@ Room.prototype._addLiveEvents = function(events) { this._eventIdToTimeline[events[i].getId()] = existingTimeline; } - // replace the event source + // replace the event source, but preserve the original content + // and type in case it was encrypted (we won't be able to + // decrypt it, even though we sent it.) + var existingSource = existingEvent.event; existingEvent.event = events[i].event; + existingEvent.event.content = existingSource.content; + existingEvent.event.type = existingSource.type; // successfully sent. existingEvent.status = null;