From dc8c1efa2dca39b4f479930657e99078242b2919 Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Wed, 11 May 2022 14:45:19 +0100
Subject: [PATCH 1/3] Remove unused state and fix canPeek

---
 src/components/structures/RoomView.tsx      | 20 +++++---------------
 src/components/structures/TimelinePanel.tsx |  2 ++
 2 files changed, 7 insertions(+), 15 deletions(-)

diff --git a/src/components/structures/RoomView.tsx b/src/components/structures/RoomView.tsx
index a7a4ec2a9e0..9b23450f996 100644
--- a/src/components/structures/RoomView.tsx
+++ b/src/components/structures/RoomView.tsx
@@ -36,6 +36,7 @@ import { MatrixError } from 'matrix-js-sdk/src/http-api';
 import { ClientEvent } from "matrix-js-sdk/src/client";
 import { CryptoEvent } from "matrix-js-sdk/src/crypto";
 import { THREAD_RELATION_TYPE } from 'matrix-js-sdk/src/models/thread';
+import { HistoryVisibility } from 'matrix-js-sdk/src/@types/partials';
 
 import shouldHideEvent from '../../shouldHideEvent';
 import { _t } from '../../languageHandler';
@@ -166,7 +167,6 @@ export interface IRoomState {
     searchHighlights?: string[];
     searchInProgress?: boolean;
     callState?: CallState;
-    guestsCanJoin: boolean;
     canPeek: boolean;
     showApps: boolean;
     isPeeking: boolean;
@@ -250,7 +250,6 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
             numUnreadMessages: 0,
             searchResults: null,
             callState: null,
-            guestsCanJoin: false,
             canPeek: false,
             showApps: false,
             isPeeking: false,
@@ -1054,19 +1053,10 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
     }
 
     private calculatePeekRules(room: Room) {
-        const guestAccessEvent = room.currentState.getStateEvents("m.room.guest_access", "");
-        if (guestAccessEvent && guestAccessEvent.getContent().guest_access === "can_join") {
-            this.setState({
-                guestsCanJoin: true,
-            });
-        }
-
-        const historyVisibility = room.currentState.getStateEvents("m.room.history_visibility", "");
-        if (historyVisibility && historyVisibility.getContent().history_visibility === "world_readable") {
-            this.setState({
-                canPeek: true,
-            });
-        }
+        const historyVisibility = room.currentState.getStateEvents(EventType.RoomHistoryVisibility, "");
+        this.setState({
+            canPeek: historyVisibility?.getContent().history_visibility === HistoryVisibility.WorldReadable,
+        });
     }
 
     private updatePreviewUrlVisibility({ roomId }: Room) {
diff --git a/src/components/structures/TimelinePanel.tsx b/src/components/structures/TimelinePanel.tsx
index c92836e1a7b..89dc9792603 100644
--- a/src/components/structures/TimelinePanel.tsx
+++ b/src/components/structures/TimelinePanel.tsx
@@ -213,6 +213,7 @@ interface IEventIndexOpts {
  */
 class TimelinePanel extends React.Component<IProps, IState> {
     static contextType = RoomContext;
+    public context!: React.ContextType<typeof RoomContext>;
 
     // a map from room id to read marker event timestamp
     static roomReadMarkerTsMap: Record<string, number> = {};
@@ -242,6 +243,7 @@ class TimelinePanel extends React.Component<IProps, IState> {
 
     constructor(props, context) {
         super(props, context);
+        this.context = context;
 
         debuglog("mounting");
 

From 82cd4ddda32ab16921cce065b4aa60851efc1cd1 Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Wed, 11 May 2022 14:57:04 +0100
Subject: [PATCH 2/3] Fix effect of URL preview toggle not updating live

---
 src/components/structures/RoomView.tsx | 22 ++++------------------
 1 file changed, 4 insertions(+), 18 deletions(-)

diff --git a/src/components/structures/RoomView.tsx b/src/components/structures/RoomView.tsx
index 9b23450f996..1539037cf89 100644
--- a/src/components/structures/RoomView.tsx
+++ b/src/components/structures/RoomView.tsx
@@ -283,11 +283,9 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
         context.on(ClientEvent.Room, this.onRoom);
         context.on(RoomEvent.Timeline, this.onRoomTimeline);
         context.on(RoomEvent.Name, this.onRoomName);
-        context.on(RoomEvent.AccountData, this.onRoomAccountData);
         context.on(RoomStateEvent.Events, this.onRoomStateEvents);
         context.on(RoomStateEvent.Update, this.onRoomStateUpdate);
         context.on(RoomEvent.MyMembership, this.onMyMembership);
-        context.on(ClientEvent.AccountData, this.onAccountData);
         context.on(CryptoEvent.KeyBackupStatus, this.onKeyBackupStatus);
         context.on(CryptoEvent.DeviceVerificationChanged, this.onDeviceVerificationChanged);
         context.on(CryptoEvent.UserTrustStatusChanged, this.onUserVerificationChanged);
@@ -325,6 +323,8 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
             SettingsStore.watchSetting("showHiddenEventsInTimeline", null, (...[,,, value]) =>
                 this.setState({ showHiddenEvents: value as boolean }),
             ),
+            SettingsStore.watchSetting("urlPreviewsEnabled", null, this.onUrlPreviewsEnabledChange),
+            SettingsStore.watchSetting("urlPreviewsEnabled_e2ee", null, this.onUrlPreviewsEnabledChange),
         ];
     }
 
@@ -721,11 +721,9 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
             this.context.removeListener(ClientEvent.Room, this.onRoom);
             this.context.removeListener(RoomEvent.Timeline, this.onRoomTimeline);
             this.context.removeListener(RoomEvent.Name, this.onRoomName);
-            this.context.removeListener(RoomEvent.AccountData, this.onRoomAccountData);
             this.context.removeListener(RoomStateEvent.Events, this.onRoomStateEvents);
             this.context.removeListener(RoomEvent.MyMembership, this.onMyMembership);
             this.context.removeListener(RoomStateEvent.Update, this.onRoomStateUpdate);
-            this.context.removeListener(ClientEvent.AccountData, this.onAccountData);
             this.context.removeListener(CryptoEvent.KeyBackupStatus, this.onKeyBackupStatus);
             this.context.removeListener(CryptoEvent.DeviceVerificationChanged, this.onDeviceVerificationChanged);
             this.context.removeListener(CryptoEvent.UserTrustStatusChanged, this.onUserVerificationChanged);
@@ -1126,24 +1124,12 @@ export class RoomView extends React.Component<IRoomProps, IRoomState> {
         this.setState({ e2eStatus });
     }
 
-    private onAccountData = (event: MatrixEvent) => {
-        const type = event.getType();
-        if ((type === "org.matrix.preview_urls" || type === "im.vector.web.settings") && this.state.room) {
-            // non-e2ee url previews are stored in legacy event type `org.matrix.room.preview_urls`
+    private onUrlPreviewsEnabledChange = () => {
+        if (this.state.room) {
             this.updatePreviewUrlVisibility(this.state.room);
         }
     };
 
-    private onRoomAccountData = (event: MatrixEvent, room: Room) => {
-        if (room.roomId == this.state.roomId) {
-            const type = event.getType();
-            if (type === "org.matrix.room.preview_urls" || type === "im.vector.web.settings") {
-                // non-e2ee url previews are stored in legacy event type `org.matrix.room.preview_urls`
-                this.updatePreviewUrlVisibility(room);
-            }
-        }
-    };
-
     private onRoomStateEvents = (ev: MatrixEvent, state: RoomState) => {
         // ignore if we don't have a room yet
         if (!this.state.room || this.state.room.roomId !== state.roomId) return;

From 964a171c077ead4736149defb7e32941821d8d15 Mon Sep 17 00:00:00 2001
From: Michael Telatynski <7t3chguy@gmail.com>
Date: Wed, 11 May 2022 15:02:39 +0100
Subject: [PATCH 3/3] Remove stale context fields

---
 src/contexts/RoomContext.ts                                 | 1 -
 test/components/views/rooms/MessageComposerButtons-test.tsx | 1 -
 2 files changed, 2 deletions(-)

diff --git a/src/contexts/RoomContext.ts b/src/contexts/RoomContext.ts
index cbf56e8a4a8..6dd705870a1 100644
--- a/src/contexts/RoomContext.ts
+++ b/src/contexts/RoomContext.ts
@@ -35,7 +35,6 @@ const RoomContext = createContext<IRoomState>({
     shouldPeek: true,
     membersLoaded: false,
     numUnreadMessages: 0,
-    guestsCanJoin: false,
     canPeek: false,
     showApps: false,
     isPeeking: false,
diff --git a/test/components/views/rooms/MessageComposerButtons-test.tsx b/test/components/views/rooms/MessageComposerButtons-test.tsx
index 1ec08e455d0..d9f867b67e4 100644
--- a/test/components/views/rooms/MessageComposerButtons-test.tsx
+++ b/test/components/views/rooms/MessageComposerButtons-test.tsx
@@ -209,7 +209,6 @@ function createRoomState(room: Room, narrow: boolean): IRoomState {
         shouldPeek: true,
         membersLoaded: false,
         numUnreadMessages: 0,
-        guestsCanJoin: false,
         canPeek: false,
         showApps: false,
         isPeeking: false,