From 054c51b82f5663b5cc7c10d08e6ef0a21a3eeb9c Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Mon, 17 May 2021 11:27:30 +0200 Subject: [PATCH] add caching MessageBody in BaseTextMessageTile,use in EncryptedEventTile missing body in EncryptedEventTile was what caused the bug --- .../timeline/tiles/BaseTextMessageTile.js | 47 +++++++++++++++++++ .../room/timeline/tiles/EncryptedEventTile.js | 6 +-- .../room/timeline/tiles/MessageTile.js | 4 -- .../session/room/timeline/tiles/TextTile.js | 20 +++----- 4 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 src/domain/session/room/timeline/tiles/BaseTextMessageTile.js diff --git a/src/domain/session/room/timeline/tiles/BaseTextMessageTile.js b/src/domain/session/room/timeline/tiles/BaseTextMessageTile.js new file mode 100644 index 00000000..e92e326a --- /dev/null +++ b/src/domain/session/room/timeline/tiles/BaseTextMessageTile.js @@ -0,0 +1,47 @@ +/* +Copyright 2020 Bruno Windels + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +import {MessageTile} from "./MessageTile.js"; +import {stringAsBody} from "../MessageBody.js"; + +export class BaseTextMessageTile extends MessageTile { + constructor(options) { + super(options); + this._messageBody = null; + } + + get shape() { + return "message"; + } + + _parseBody(bodyString) { + return stringAsBody(bodyString); + } + + get body() { + const body = this._getBodyAsString(); + // body is a string, so we can check for difference by just + // doing an equality check + if (!this._messageBody || this._messageBody.sourceString !== body) { + // body with markup is an array of parts, + // so we should not recreate it for the same body string, + // or else the equality check in the binding will always fail. + // So cache it here. + this._messageBody = this._parseBody(body); + } + return this._messageBody; + } +} diff --git a/src/domain/session/room/timeline/tiles/EncryptedEventTile.js b/src/domain/session/room/timeline/tiles/EncryptedEventTile.js index 23476ebb..ceb771c2 100644 --- a/src/domain/session/room/timeline/tiles/EncryptedEventTile.js +++ b/src/domain/session/room/timeline/tiles/EncryptedEventTile.js @@ -14,10 +14,10 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {MessageTile} from "./MessageTile.js"; +import {BaseTextMessageTile} from "./BaseTextMessageTile.js"; import {UpdateAction} from "../UpdateAction.js"; -export class EncryptedEventTile extends MessageTile { +export class EncryptedEventTile extends BaseTextMessageTile { updateEntry(entry, params) { const parentResult = super.updateEntry(entry, params); // event got decrypted, recreate the tile and replace this one with it @@ -33,7 +33,7 @@ export class EncryptedEventTile extends MessageTile { return "message-status" } - get text() { + _getBodyAsString() { const decryptionError = this._entry.decryptionError; const code = decryptionError?.code; if (code === "MEGOLM_NO_SESSION") { diff --git a/src/domain/session/room/timeline/tiles/MessageTile.js b/src/domain/session/room/timeline/tiles/MessageTile.js index f85f0fca..1769748d 100644 --- a/src/domain/session/room/timeline/tiles/MessageTile.js +++ b/src/domain/session/room/timeline/tiles/MessageTile.js @@ -33,10 +33,6 @@ export class MessageTile extends SimpleTile { return this._room.mediaRepository; } - get shape() { - return "message"; - } - get displayName() { return this._entry.displayName || this.sender; } diff --git a/src/domain/session/room/timeline/tiles/TextTile.js b/src/domain/session/room/timeline/tiles/TextTile.js index 88e281d2..fdfdd32f 100644 --- a/src/domain/session/room/timeline/tiles/TextTile.js +++ b/src/domain/session/room/timeline/tiles/TextTile.js @@ -14,12 +14,11 @@ See the License for the specific language governing permissions and limitations under the License. */ -import {MessageTile} from "./MessageTile.js"; -import { MessageBodyBuilder } from "../MessageBodyBuilder.js"; +import {BaseTextMessageTile} from "./BaseTextMessageTile.js"; +import {parsePlainBody} from "../MessageBody.js"; -export class TextTile extends MessageTile { - - get _contentBody() { +export class TextTile extends BaseTextMessageTile { + _getBodyAsString() { const content = this._getContent(); let body = content?.body || ""; if (content.msgtype === "m.emote") { @@ -28,14 +27,7 @@ export class TextTile extends MessageTile { return body; } - get body() { - const body = this._contentBody; - if (body === this._body) { - return this._message; - } - const message = new MessageBodyBuilder(); - message.fromText(body); - [this._body, this._message] = [body, message]; - return message; + _parseBody(bodyString) { + return parsePlainBody(bodyString); } }