From 871c48f69b8039f82da7aeaff0b8a6e87ae973a5 Mon Sep 17 00:00:00 2001
From: Germain Souquet <germain@souquet.com>
Date: Tue, 18 May 2021 10:02:21 +0100
Subject: [PATCH] stop assuming that decryption happens ahead of time

---
 src/components/structures/FilePanel.js            | 4 ++++
 src/components/structures/RoomView.tsx            | 2 +-
 src/components/views/messages/MessageActionBar.js | 7 +++++++
 src/components/views/messages/ViewSourceEvent.js  | 7 +++++++
 src/indexing/EventIndex.js                        | 4 ++++
 5 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/src/components/structures/FilePanel.js b/src/components/structures/FilePanel.js
index d5e4b092e2..8f9908f1ca 100644
--- a/src/components/structures/FilePanel.js
+++ b/src/components/structures/FilePanel.js
@@ -50,6 +50,10 @@ class FilePanel extends React.Component {
         if (room?.roomId !== this.props?.roomId) return;
         if (toStartOfTimeline || !data || !data.liveEvent || ev.isRedacted()) return;
 
+        if (ev.shouldAttemptDecryption()) {
+            ev.attemptDecryption(room._client._crypto);
+        }
+
         if (ev.isBeingDecrypted()) {
             this.decryptingEvents.add(ev.getId());
         } else {
diff --git a/src/components/structures/RoomView.tsx b/src/components/structures/RoomView.tsx
index c0f3c59457..dbfba13297 100644
--- a/src/components/structures/RoomView.tsx
+++ b/src/components/structures/RoomView.tsx
@@ -811,7 +811,7 @@ export default class RoomView extends React.Component<IProps, IState> {
     };
 
     private onEvent = (ev) => {
-        if (ev.isBeingDecrypted() || ev.isDecryptionFailure()) return;
+        if (ev.isBeingDecrypted() || ev.isDecryptionFailure() || ev.shouldAttemptDecryption()) return;
         this.handleEffects(ev);
     };
 
diff --git a/src/components/views/messages/MessageActionBar.js b/src/components/views/messages/MessageActionBar.js
index b2f7f8a692..cf3a0a704f 100644
--- a/src/components/views/messages/MessageActionBar.js
+++ b/src/components/views/messages/MessageActionBar.js
@@ -31,6 +31,7 @@ import {RovingAccessibleTooltipButton, useRovingTabIndex} from "../../../accessi
 import {replaceableComponent} from "../../../utils/replaceableComponent";
 import {canCancel} from "../context_menus/MessageContextMenu";
 import Resend from "../../../Resend";
+import { MatrixClientPeg } from "../../../MatrixClientPeg";
 
 const OptionsButton = ({mxEvent, getTile, getReplyThread, permalinkCreator, onFocusChange}) => {
     const [menuDisplayed, button, openMenu, closeMenu] = useContextMenu();
@@ -122,6 +123,12 @@ export default class MessageActionBar extends React.PureComponent {
         if (this.props.mxEvent.status && this.props.mxEvent.status !== EventStatus.SENT) {
             this.props.mxEvent.on("Event.status", this.onSent);
         }
+
+        if (this.props.mxEvent.shouldAttemptDecryption()) {
+            const client = MatrixClientPeg.get();
+            this.props.mxEvent.attemptDecryption(client._crypto);
+        }
+
         if (this.props.mxEvent.isBeingDecrypted()) {
             this.props.mxEvent.once("Event.decrypted", this.onDecrypted);
         }
diff --git a/src/components/views/messages/ViewSourceEvent.js b/src/components/views/messages/ViewSourceEvent.js
index adc7a248cd..9a110a8826 100644
--- a/src/components/views/messages/ViewSourceEvent.js
+++ b/src/components/views/messages/ViewSourceEvent.js
@@ -18,6 +18,7 @@ import React from 'react';
 import PropTypes from 'prop-types';
 import classNames from 'classnames';
 import {replaceableComponent} from "../../../utils/replaceableComponent";
+import { MatrixClientPeg } from "../../../MatrixClientPeg";
 
 @replaceableComponent("views.messages.ViewSourceEvent")
 export default class ViewSourceEvent extends React.PureComponent {
@@ -36,6 +37,12 @@ export default class ViewSourceEvent extends React.PureComponent {
 
     componentDidMount() {
         const {mxEvent} = this.props;
+
+        const client = MatrixClientPeg.get();
+        if (mxEvent.shouldAttemptDecryption()) {
+            mxEvent.attemptDecryption(client._client._crypto);
+        }
+
         if (mxEvent.isBeingDecrypted()) {
             mxEvent.once("Event.decrypted", () => this.forceUpdate());
         }
diff --git a/src/indexing/EventIndex.js b/src/indexing/EventIndex.js
index 857dc5b248..df46d800b1 100644
--- a/src/indexing/EventIndex.js
+++ b/src/indexing/EventIndex.js
@@ -187,6 +187,10 @@ export default class EventIndex extends EventEmitter {
             return;
         }
 
+        if (ev.shouldAttemptDecryption()) {
+            ev.attemptDecryption(room._client._crypto);
+        }
+
         if (ev.isBeingDecrypted()) {
             // XXX: Private member access
             await ev._decryptionPromise;