add caching MessageBody in BaseTextMessageTile,use in EncryptedEventTile

missing body in EncryptedEventTile was what caused the bug
This commit is contained in:
Bruno Windels 2021-05-17 11:27:30 +02:00
parent 01b8b397b6
commit 054c51b82f
4 changed files with 56 additions and 21 deletions

View file

@ -0,0 +1,47 @@
/*
Copyright 2020 Bruno Windels <bruno@windels.cloud>
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;
}
}

View file

@ -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") {

View file

@ -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;
}

View file

@ -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);
}
}