From d011846c66beabc101b782fdd711ba5f33ca4222 Mon Sep 17 00:00:00 2001
From: RMidhunSuresh <hi@midhun.dev>
Date: Tue, 10 May 2022 12:09:22 +0530
Subject: [PATCH] Support E2EE

---
 public/config.json                    |  4 ++--
 src/types/IChatterboxConfig.ts        |  2 ++
 src/viewmodels/ChatterboxViewModel.ts |  2 +-
 src/viewmodels/tiles.ts               | 12 +++++++++++-
 4 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/public/config.json b/public/config.json
index 29999d4..b8aca5f 100644
--- a/public/config.json
+++ b/public/config.json
@@ -3,9 +3,9 @@
     "auto_join_room": "!XOUozZcpOvneVFsrGi:matrix.midhun.dev",
     "username_prefix": "foobar",
     "header": {
-        "title": "Chatterbox Test Room",
         "avatar": "https://i.imgur.com/KkSYCKB.png"
     },
     "token": "k9SL1R~GcdbjiFjC",
-    "invite_user": "@botuser:matrix.midhun.dev"
+    "invite_user": "@botuser:matrix.midhun.dev",
+    "encrypt_room": true 
 }
diff --git a/src/types/IChatterboxConfig.ts b/src/types/IChatterboxConfig.ts
index a3d8d2b..6fab8ee 100644
--- a/src/types/IChatterboxConfig.ts
+++ b/src/types/IChatterboxConfig.ts
@@ -7,6 +7,8 @@ export interface IChatterboxConfig {
     // If specified, chatterbox will create a dm with this user
     // This option takes precedence over 'auto_join_room'
     invite_user: string;
+    // If set to true, the room created for DM is encrypted
+    encrypt_room: boolean;
     // Configurations for header on chatterbox (containing title, avatar, minimize button)
     header: IHeader;
     // Token needed for token-authenticated registration
diff --git a/src/viewmodels/ChatterboxViewModel.ts b/src/viewmodels/ChatterboxViewModel.ts
index 1cea7c1..af2de40 100644
--- a/src/viewmodels/ChatterboxViewModel.ts
+++ b/src/viewmodels/ChatterboxViewModel.ts
@@ -46,7 +46,7 @@ export class ChatterboxViewModel extends ViewModel {
             type: 1, //todo: use enum from hydrogen-sdk here
             name: undefined,
             topic: undefined,
-            isEncrypted: false,
+            isEncrypted: this._options.config["encrypt_room"] ?? false,
             isFederationDisabled: false,
             alias: undefined,
             avatar: undefined,
diff --git a/src/viewmodels/tiles.ts b/src/viewmodels/tiles.ts
index aad096e..b91ae02 100644
--- a/src/viewmodels/tiles.ts
+++ b/src/viewmodels/tiles.ts
@@ -1,5 +1,6 @@
-import { TextTile, ImageTile, VideoTile, FileTile, LocationTile, RedactedTile, tileClassForEntry } from "hydrogen-view-sdk";
+import { TextTile, ImageTile, VideoTile, FileTile, LocationTile, RedactedTile, EncryptionEnabledTile, tileClassForEntry } from "hydrogen-view-sdk";
 
+// Override all the message-tiles to show the display name as "me"
 class ChatterboxTextTile extends TextTile {
     get displayName() {
         return this.isOwn? "me" : super.displayName;
@@ -36,6 +37,13 @@ class ChatterboxRedactedTile extends RedactedTile {
     }
 }
 
+// We don't want to show the (long and random) user-id in this announcement! 
+class ChatterboxEncryptionEnabledTile extends EncryptionEnabledTile {
+    get announcement() {
+        return this.i18n`This room is end-to-end encrypted 🔒`;
+    }
+}
+
 export function createCustomTileClassForEntry(ownUserId: string) {
     return function customTileClassForEntry(entry) {
         switch (entry.eventType) {
@@ -69,6 +77,8 @@ export function createCustomTileClassForEntry(ownUserId: string) {
                 else {
                     return undefined;
                 }
+            case "m.room.encryption":
+                return ChatterboxEncryptionEnabledTile;
             default:
                 return tileClassForEntry(entry);
 
-- 
GitLab