From e97b9c4dd8cea795dcb49c4fb0f1c05c86e36af2 Mon Sep 17 00:00:00 2001 From: Aaron Raimist Date: Wed, 2 Feb 2022 06:05:24 +0000 Subject: [PATCH 1/3] Allow opening external Matrix URIs Signed-off-by: Aaron Raimist --- src/components/structures/MatrixChat.tsx | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx index e6b10aaea8d..53e2a822db3 100644 --- a/src/components/structures/MatrixChat.tsx +++ b/src/components/structures/MatrixChat.tsx @@ -108,6 +108,7 @@ import PerformanceMonitor, { PerformanceEntryNames } from "../../performance"; import UIStore, { UI_EVENTS } from "../../stores/UIStore"; import SoftLogout from './auth/SoftLogout'; import { makeRoomPermalink } from "../../utils/permalinks/Permalinks"; +import MatrixSchemePermalinkConstructor from "../../utils/permalinks/MatrixSchemePermalinkConstructor"; import { copyPlaintext } from "../../utils/strings"; import { PosthogAnalytics } from '../../PosthogAnalytics'; import { initSentry } from "../../sentry"; @@ -1354,6 +1355,8 @@ export default class MatrixChat extends React.PureComponent { StorageManager.tryPersistStorage(); + navigator.registerProtocolHandler("matrix", "/#/%s"); + if (PosthogAnalytics.instance.isEnabled() && SettingsStore.isLevelSupported(SettingLevel.ACCOUNT)) { this.initPosthogAnalyticsToast(); } else if (Analytics.canEnable() || CountlyAnalytics.instance.canEnable()) { @@ -1918,6 +1921,39 @@ export default class MatrixChat extends React.PureComponent { action: 'view_group', group_id: groupId, }); + } else if (screen.indexOf('matrix:') === 0) { + try { + const permalink = new MatrixSchemePermalinkConstructor().parsePermalink(screen); + + if (permalink.userId) { + dis.dispatch({ + action: 'view_user_info', + userId: permalink.userId, + }); + } else if (permalink.roomIdOrAlias) { + const viewRoomPayload = { + action: Action.ViewRoom, + event_id: permalink.eventId, + via_servers: permalink.viaServers, + // If an event ID is given in the URL hash, notify RoomViewStore to mark + // it as highlighted, which will propagate to RoomView and highlight the + // associated EventTile. + highlighted: Boolean(permalink.eventId), + room_alias: undefined, + room_id: undefined, + }; + if (permalink.roomIdOrAlias[0] === '#') { + viewRoomPayload.room_alias = permalink.roomIdOrAlias; + } else { + viewRoomPayload.room_id = permalink.roomIdOrAlias; + } + + dis.dispatch(viewRoomPayload); + } + } catch { + logger.error("Invalid Matrix URI scheme permalink:", screen); + dis.dispatch({ action: 'view_last_screen' }); + } } else { logger.info("Ignoring showScreen for '%s'", screen); } From 2277ec4d92601fa8d7509ded5714e475fa28a476 Mon Sep 17 00:00:00 2001 From: Aaron Raimist Date: Fri, 4 Feb 2022 00:19:51 +0000 Subject: [PATCH 2/3] Use window.navigator Signed-off-by: Aaron Raimist --- src/components/structures/MatrixChat.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx index 39ae7699bcf..f25dfd6a769 100644 --- a/src/components/structures/MatrixChat.tsx +++ b/src/components/structures/MatrixChat.tsx @@ -1389,7 +1389,7 @@ export default class MatrixChat extends React.PureComponent { StorageManager.tryPersistStorage(); - navigator.registerProtocolHandler("matrix", "/#/%s"); + window.navigator.registerProtocolHandler("matrix", "/#/%s"); if (PosthogAnalytics.instance.isEnabled() && SettingsStore.isLevelSupported(SettingLevel.ACCOUNT)) { this.initPosthogAnalyticsToast(); From 584495829927258099c1da7b38440576ab6d5589 Mon Sep 17 00:00:00 2001 From: Aaron Raimist Date: Wed, 9 Feb 2022 07:53:33 +0000 Subject: [PATCH 3/3] Revert "Use window.navigator" This reverts commit 2277ec4d92601fa8d7509ded5714e475fa28a476. Signed-off-by: Aaron Raimist --- src/components/structures/MatrixChat.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/structures/MatrixChat.tsx b/src/components/structures/MatrixChat.tsx index 9a746324a1b..811e2b91bd5 100644 --- a/src/components/structures/MatrixChat.tsx +++ b/src/components/structures/MatrixChat.tsx @@ -1387,7 +1387,7 @@ export default class MatrixChat extends React.PureComponent { StorageManager.tryPersistStorage(); - window.navigator.registerProtocolHandler("matrix", "/#/%s"); + navigator.registerProtocolHandler("matrix", "/#/%s"); if (PosthogAnalytics.instance.isEnabled() && SettingsStore.isLevelSupported(SettingLevel.ACCOUNT)) { this.initPosthogAnalyticsToast();