From db875508d8379befb0f4d0fa1464e757570a1d72 Mon Sep 17 00:00:00 2001 From: Jaiwanth Date: Tue, 22 Jun 2021 12:50:15 +0530 Subject: [PATCH] Add text for room avatar events and skip empty text events --- src/TextForEvent.ts | 13 +++++++++++-- src/i18n/strings/en_EN.json | 2 ++ src/utils/exportUtils/Exporter.ts | 7 +++++-- src/utils/exportUtils/PlainTextExport.ts | 3 ++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/TextForEvent.ts b/src/TextForEvent.ts index 6975550154..de65733be8 100644 --- a/src/TextForEvent.ts +++ b/src/TextForEvent.ts @@ -116,6 +116,11 @@ function textForTopicEvent(ev): () => string | null { }); } +function textForRoomAvatarEvent(ev: MatrixEvent): () => string | null { + const senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender(); + return () => _t('%(senderDisplayName)s changed the room avatar.', {senderDisplayName}); +} + function textForRoomNameEvent(ev): () => string | null { const senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender(); @@ -242,10 +247,12 @@ function textForMessageEvent(ev: MatrixEvent): () => string | null { const isRedacted = ev.isRedacted(); let message = ev.getContent().body; if (isRedacted) message = _t("Message Deleted"); - else if (ev.getContent().msgtype === "m.emote") { + if (ev.getContent().msgtype === "m.emote") { message = "* " + senderDisplayName + " " + message; } else if (ev.getContent().msgtype === "m.image") { message = _t('%(senderDisplayName)s sent an image.', {senderDisplayName}); + } else if (ev.getType() == "m.sticker") { + message = _t('%(senderDisplayName)s sent a sticker.', {senderDisplayName}); } else message = senderDisplayName + ': ' + message; return message; }; @@ -603,6 +610,7 @@ interface IHandlers { const handlers: IHandlers = { 'm.room.message': textForMessageEvent, 'm.call.invite': textForCallInviteEvent, + 'm.sticker': textForMessageEvent, 'm.call.answer': textForCallAnswerEvent, 'm.call.hangup': textForCallHangupEvent, 'm.call.reject': textForCallRejectEvent, @@ -613,6 +621,7 @@ const stateHandlers: IHandlers = { 'm.room.name': textForRoomNameEvent, 'm.room.topic': textForTopicEvent, 'm.room.member': textForMemberEvent, + "m.room.avatar": textForRoomAvatarEvent, 'm.room.third_party_invite': textForThreePidInviteEvent, 'm.room.history_visibility': textForHistoryVisibilityEvent, 'm.room.power_levels': textForPowerEvent, @@ -638,7 +647,7 @@ export function hasText(ev): boolean { return Boolean(handler?.(ev)); } -export function textForEvent(ev): string { +export function textForEvent(ev: MatrixEvent): string { const handler = (ev.isState() ? stateHandlers : handlers)[ev.getType()]; return handler?.(ev)?.() || ''; } diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 5c9c81dcfe..2bad735745 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -509,6 +509,7 @@ "%(senderName)s withdrew %(targetName)s's invitation.": "%(senderName)s withdrew %(targetName)s's invitation.", "%(senderName)s kicked %(targetName)s.": "%(senderName)s kicked %(targetName)s.", "%(senderDisplayName)s changed the topic to \"%(topic)s\".": "%(senderDisplayName)s changed the topic to \"%(topic)s\".", + "%(senderDisplayName)s changed the room avatar.": "%(senderDisplayName)s changed the room avatar.", "%(senderDisplayName)s removed the room name.": "%(senderDisplayName)s removed the room name.", "%(senderDisplayName)s changed the room name from %(oldRoomName)s to %(newRoomName)s.": "%(senderDisplayName)s changed the room name from %(oldRoomName)s to %(newRoomName)s.", "%(senderDisplayName)s changed the room name to %(roomName)s.": "%(senderDisplayName)s changed the room name to %(roomName)s.", @@ -527,6 +528,7 @@ "🎉 All servers are banned from participating! This room can no longer be used.": "🎉 All servers are banned from participating! This room can no longer be used.", "Message Deleted": "Message Deleted", "%(senderDisplayName)s sent an image.": "%(senderDisplayName)s sent an image.", + "%(senderDisplayName)s sent a sticker.": "%(senderDisplayName)s sent a sticker.", "%(senderName)s set the main address for this room to %(address)s.": "%(senderName)s set the main address for this room to %(address)s.", "%(senderName)s removed the main address for this room.": "%(senderName)s removed the main address for this room.", "%(senderName)s added the alternative addresses %(addresses)s for this room.|other": "%(senderName)s added the alternative addresses %(addresses)s for this room.", diff --git a/src/utils/exportUtils/Exporter.ts b/src/utils/exportUtils/Exporter.ts index ba35c5904f..d5e1186b0f 100644 --- a/src/utils/exportUtils/Exporter.ts +++ b/src/utils/exportUtils/Exporter.ts @@ -140,8 +140,11 @@ export default abstract class Exporter { return fileDirectory + "/" + fileName + '-' + fileDate + fileExt; } - protected isReply(mxEvent) { - const relatesTo = mxEvent.getContent()["m.relates_to"]; + protected isReply(event: MatrixEvent) { + const isEncrypted = event.isEncrypted(); + // If encrypted, in_reply_to lies in event.event.content + const content = isEncrypted ? event.event.content : event.getContent(); + const relatesTo = content["m.relates_to"]; return !!(relatesTo && relatesTo["m.in_reply_to"]); } diff --git a/src/utils/exportUtils/PlainTextExport.ts b/src/utils/exportUtils/PlainTextExport.ts index 69c80122d6..d6e76b5968 100644 --- a/src/utils/exportUtils/PlainTextExport.ts +++ b/src/utils/exportUtils/PlainTextExport.ts @@ -71,7 +71,8 @@ export default class PlainTextExporter extends Exporter { let content = ""; for (const event of events) { if (!haveTileForEvent(event)) continue; - content += `${new Date(event.getTs()).toLocaleString()} - ${this._textForEvent(event)}\n`; + const textForEvent = this._textForEvent(event); + content += textForEvent && `${new Date(event.getTs()).toLocaleString()} - ${textForEvent}\n`; } return content; }