From c2f6a802445f475100ca46022d01c23c5c0c8cc0 Mon Sep 17 00:00:00 2001 From: Dylan <36567925+Airyzz@users.noreply.github.com> Date: Sun, 28 Aug 2022 19:38:57 +0930 Subject: [PATCH 1/8] Created Link handlers for matrix.to --- package-lock.json | 39 +++++++++----- package.json | 1 + src/app/organisms/join-alias/JoinAlias.jsx | 47 ++++++++++++++++ src/util/linkHandlers.jsx | 39 ++++++++++++++ src/util/twemojify.jsx | 62 +++++++++++++++------- 5 files changed, 157 insertions(+), 31 deletions(-) create mode 100644 src/util/linkHandlers.jsx diff --git a/package-lock.json b/package-lock.json index 5c2db9d38e..2b013b359c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,6 +13,7 @@ "@fontsource/roboto": "^4.5.8", "@khanacademy/simple-markdown": "^0.8.3", "@matrix-org/olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.12.tgz", + "@tauri-apps/api": "^1.0.2", "@tippyjs/react": "^4.2.6", "babel-polyfill": "^6.26.0", "blurhash": "^1.1.5", @@ -2496,6 +2497,20 @@ "resolved": "https://registry.npmjs.org/@react-dnd/shallowequal/-/shallowequal-3.0.1.tgz", "integrity": "sha512-XjDVbs3ZU16CO1h5Q3Ew2RPJqmZBDE/EVf1LYp6ePEffs3V/MX9ZbL5bJr8qiK5SbGmUMuDoaFgyKacYz8prRA==" }, + "node_modules/@tauri-apps/api": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@tauri-apps/api/-/api-1.0.2.tgz", + "integrity": "sha512-yuNW0oeJ1/ZA7wNF1KgxhHrSu5viPVzY/UgUczzN5ptLM8dH15Juy5rEGkoHfeXGju90Y/l22hi3BtIrp/za+w==", + "engines": { + "node": ">= 12.22.0", + "npm": ">= 6.6.0", + "yarn": ">= 1.19.1" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/tauri" + } + }, "node_modules/@tippyjs/react": { "version": "4.2.6", "resolved": "https://registry.npmjs.org/@tippyjs/react/-/react-4.2.6.tgz", @@ -3025,8 +3040,6 @@ "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.9.0.tgz", "integrity": "sha512-qOKJyNj/h+OWx7s5DePL6Zu1KeM9jPZhwBqs+7DzP6bGOvqzVCSf0xueYmVuaC/oQ/VtS2zLMLHdQFbkka+XDQ==", "dev": true, - "optional": true, - "peer": true, "dependencies": { "fast-deep-equal": "^3.1.1", "json-schema-traverse": "^1.0.0", @@ -3042,9 +3055,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", - "dev": true, - "optional": true, - "peer": true + "dev": true }, "node_modules/ajv-keywords": { "version": "3.5.2", @@ -15347,6 +15358,11 @@ "resolved": "https://registry.npmjs.org/@react-dnd/shallowequal/-/shallowequal-3.0.1.tgz", "integrity": "sha512-XjDVbs3ZU16CO1h5Q3Ew2RPJqmZBDE/EVf1LYp6ePEffs3V/MX9ZbL5bJr8qiK5SbGmUMuDoaFgyKacYz8prRA==" }, + "@tauri-apps/api": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/@tauri-apps/api/-/api-1.0.2.tgz", + "integrity": "sha512-yuNW0oeJ1/ZA7wNF1KgxhHrSu5viPVzY/UgUczzN5ptLM8dH15Juy5rEGkoHfeXGju90Y/l22hi3BtIrp/za+w==" + }, "@tippyjs/react": { "version": "4.2.6", "resolved": "https://registry.npmjs.org/@tippyjs/react/-/react-4.2.6.tgz", @@ -15823,14 +15839,15 @@ "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-2.1.1.tgz", "integrity": "sha512-Wx0Kx52hxE7C18hkMEggYlEifqWZtYaRgouJor+WMdPnQyEK13vgEWyVNup7SoeeoLMsr4kf5h6dOW11I15MUA==", "dev": true, - "requires": {}, + "requires": { + "ajv": "^8.0.0" + }, "dependencies": { "ajv": { - "version": "https://registry.npmjs.org/ajv/-/ajv-8.9.0.tgz", + "version": "8.9.0", + "resolved": "https://registry.npmjs.org/ajv/-/ajv-8.9.0.tgz", "integrity": "sha512-qOKJyNj/h+OWx7s5DePL6Zu1KeM9jPZhwBqs+7DzP6bGOvqzVCSf0xueYmVuaC/oQ/VtS2zLMLHdQFbkka+XDQ==", "dev": true, - "optional": true, - "peer": true, "requires": { "fast-deep-equal": "^3.1.1", "json-schema-traverse": "^1.0.0", @@ -15842,9 +15859,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-1.0.0.tgz", "integrity": "sha512-NM8/P9n3XjXhIZn1lLhkFaACTOURQXjWhV4BA/RnOv8xvgqtqpAX9IO4mRQxSx1Rlo4tqzeqb0sOlruaOy3dug==", - "dev": true, - "optional": true, - "peer": true + "dev": true } } }, diff --git a/package.json b/package.json index 63e46a64b6..8d3269a44d 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "@fontsource/roboto": "^4.5.8", "@khanacademy/simple-markdown": "^0.8.3", "@matrix-org/olm": "https://gitlab.matrix.org/api/v4/projects/27/packages/npm/@matrix-org/olm/-/@matrix-org/olm-3.2.12.tgz", + "@tauri-apps/api": "^1.0.2", "@tippyjs/react": "^4.2.6", "babel-polyfill": "^6.26.0", "blurhash": "^1.1.5", diff --git a/src/app/organisms/join-alias/JoinAlias.jsx b/src/app/organisms/join-alias/JoinAlias.jsx index bb90bf3545..5f8020332e 100644 --- a/src/app/organisms/join-alias/JoinAlias.jsx +++ b/src/app/organisms/join-alias/JoinAlias.jsx @@ -18,6 +18,9 @@ import Dialog from '../../molecules/dialog/Dialog'; import CrossIC from '../../../../public/res/ic/outlined/cross.svg'; import { useStore } from '../../hooks/useStore'; +import RoomTile from '../../molecules/room-tile/RoomTile'; + +import { Debounce } from '../../../util/common'; const ALIAS_OR_ID_REG = /^[#|!].+:.+\..+$/; @@ -25,9 +28,13 @@ function JoinAliasContent({ term, requestClose }) { const [process, setProcess] = useState(false); const [error, setError] = useState(undefined); const [lastJoinId, setLastJoinId] = useState(undefined); + const [preview, setPreview] = useState(undefined); + const [loadingPreview, setLoadingPreview] = useState(undefined) + const [debounce] = useState(new Debounce()); const mx = initMatrix.matrixClient; const mountStore = useStore(); + let roomData = null; const openRoom = (roomId) => { const room = mx.getRoom(roomId); @@ -48,6 +55,33 @@ function JoinAliasContent({ term, requestClose }) { }; }, [lastJoinId]); + const loadPreview = async (text) => { + setLoadingPreview(true); + setPreview(undefined); + debounce._(async () => { + try { + let room_id = text; + + if(text.startsWith('#')){ + const room = await mx.resolveRoomAlias(text); + room_id = room.room_id; + } + + roomData = await mx.getRoom(room_id) + setLoadingPreview(false) + setPreview(roomData) + } + catch{ + setLoadingPreview(false) + setPreview(undefined) + } + + }, 500)(); + } + + if(loadingPreview === undefined && term) + loadPreview(term) + const handleSubmit = async (e) => { e.preventDefault(); mountStore.setItem(true); @@ -85,6 +119,9 @@ function JoinAliasContent({ term, requestClose }) { } }; + if(term && loadingPreview === undefined) + loadPreview(term); + return (
(loadPreview(e.target.value))} /> {error && {error}} + { loadingPreview ? : <> } + { preview ? : <> + }
{ process @@ -137,6 +183,7 @@ function useWindowToggle() { function JoinAlias() { const [data, requestClose] = useWindowToggle(); + const [loaded, setLoaded] = useState(null); return ( { + openJoinAlias(url.hash.slice(2)); + }, +}; + +export default function handleLink(e) { + const url = new URL(e.target.href); + const handler = handlers[url.hostname]; + + if (handler) { + handler(url); + e.preventDefault(); + return; + } + + // if running inside tauri, check if the tauri backend has a custom handler for this link + // useful so we can open urls in a specified app + if (window.__TAURI__) { + invoke('open_link', { url: e.target.href }) + .then(() => e.preventDefault()) + .catch((error) => console.error(error)); + } + else { + //open in new tab + window.open(e.target.href); + e.preventDefault(); + } +} diff --git a/src/util/twemojify.jsx b/src/util/twemojify.jsx index 0a4fede732..1e4d1777e6 100644 --- a/src/util/twemojify.jsx +++ b/src/util/twemojify.jsx @@ -5,27 +5,48 @@ import linkifyHtml from 'linkify-html'; import parse from 'html-react-parser'; import twemoji from 'twemoji'; import { sanitizeText } from './sanitize'; +import handleLink from './linkHandlers'; const Math = lazy(() => import('../app/atoms/math/Math')); -const mathOptions = { - replace: (node) => { - const maths = node.attribs?.['data-mx-maths']; - if (maths) { - return ( - {maths}}> - - - ); - } - return null; - }, -}; +function replaceMath(node) { + const maths = node.attribs?.['data-mx-maths']; + + return ( + {maths}}> + + + ); +} + +function replaceLink(node) { + if (node.children?.[0]?.type !== 'text') { return null; } + + return ( + handleLink(e)}> + { node.children[0].data } + + ); +} + +function parseOptions(maths = false) { + return { + replace: (node) => { + if (maths) { + if (node.attribs?.['data-mx-maths']) { return replaceMath(node); } + } + + if (node.name === 'a') { return replaceLink(node); } + + return null; + }, + }; +} /** * @param {string} text - text to twemojify @@ -49,5 +70,8 @@ export function twemojify(text, opts, linkify = false, sanitize = true, maths = rel: 'noreferrer noopener', }); } - return parse(content, maths ? mathOptions : null); + + const elements = parse(content, parseOptions(maths)); + + return elements; } From 459987b2b37be85ed3bd7ce6b5095f1cf5acc3c8 Mon Sep 17 00:00:00 2001 From: Dylan <36567925+Airyzz@users.noreply.github.com> Date: Sun, 28 Aug 2022 19:46:26 +0930 Subject: [PATCH 2/8] Remove unnecessary imports --- src/util/linkHandlers.jsx | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/util/linkHandlers.jsx b/src/util/linkHandlers.jsx index 037f8463b9..bbc9029dbd 100644 --- a/src/util/linkHandlers.jsx +++ b/src/util/linkHandlers.jsx @@ -1,15 +1,8 @@ -import React, { useState } from 'react'; import { invoke } from '@tauri-apps/api/tauri'; -import { openReusableDialog } from '../client/action/navigation'; -import Text from '../app/atoms/text/Text'; -import RoomTile from '../app/molecules/room-tile/RoomTile'; -import initMatrix from '../client/initMatrix'; -import PopupWindow from '../app/molecules/popup-window/PopupWindow'; -import Spinner from '../app/atoms/spinner/Spinner'; import { openJoinAlias } from '../client/action/navigation'; const handlers = { - 'matrix.to': (url) => { + 'matrix.to': (url) => { openJoinAlias(url.hash.slice(2)); }, }; @@ -30,9 +23,8 @@ export default function handleLink(e) { invoke('open_link', { url: e.target.href }) .then(() => e.preventDefault()) .catch((error) => console.error(error)); - } - else { - //open in new tab + } else { + // open in new tab window.open(e.target.href); e.preventDefault(); } From 4ff1aa3813d528f79835126f85ed85a1d9a728b1 Mon Sep 17 00:00:00 2001 From: Dylan <36567925+Airyzz@users.noreply.github.com> Date: Tue, 30 Aug 2022 18:37:25 +0930 Subject: [PATCH 3/8] Fix code style --- src/app/organisms/join-alias/JoinAlias.jsx | 50 +++++++++++----------- 1 file changed, 25 insertions(+), 25 deletions(-) diff --git a/src/app/organisms/join-alias/JoinAlias.jsx b/src/app/organisms/join-alias/JoinAlias.jsx index 5f8020332e..7a267e67e1 100644 --- a/src/app/organisms/join-alias/JoinAlias.jsx +++ b/src/app/organisms/join-alias/JoinAlias.jsx @@ -29,7 +29,7 @@ function JoinAliasContent({ term, requestClose }) { const [error, setError] = useState(undefined); const [lastJoinId, setLastJoinId] = useState(undefined); const [preview, setPreview] = useState(undefined); - const [loadingPreview, setLoadingPreview] = useState(undefined) + const [loadingPreview, setLoadingPreview] = useState(undefined); const [debounce] = useState(new Debounce()); const mx = initMatrix.matrixClient; @@ -60,27 +60,26 @@ function JoinAliasContent({ term, requestClose }) { setPreview(undefined); debounce._(async () => { try { - let room_id = text; + let roomId = text; - if(text.startsWith('#')){ + if (text.startsWith('#')) { const room = await mx.resolveRoomAlias(text); - room_id = room.room_id; + roomId = room.room_id; } - roomData = await mx.getRoom(room_id) - setLoadingPreview(false) - setPreview(roomData) + roomData = await mx.getRoom(roomId); + setLoadingPreview(false); + setPreview(roomData); + } catch { + setLoadingPreview(false); + setPreview(undefined); } - catch{ - setLoadingPreview(false) - setPreview(undefined) - } - }, 500)(); - } + }; - if(loadingPreview === undefined && term) - loadPreview(term) + if (loadingPreview === undefined && term) { + loadPreview(term); + } const handleSubmit = async (e) => { e.preventDefault(); @@ -119,8 +118,9 @@ function JoinAliasContent({ term, requestClose }) { } }; - if(term && loadingPreview === undefined) + if (term && loadingPreview === undefined) { loadPreview(term); + } return ( @@ -132,14 +132,15 @@ function JoinAliasContent({ term, requestClose }) { onChange={(e) => (loadPreview(e.target.value))} /> {error && {error}} - { loadingPreview ? : <> } - { preview ? : <> - } + { loadingPreview ? : null } + { preview ? ( + + ) : null }
{ process @@ -183,7 +184,6 @@ function useWindowToggle() { function JoinAlias() { const [data, requestClose] = useWindowToggle(); - const [loaded, setLoaded] = useState(null); return ( Date: Tue, 30 Aug 2022 19:20:31 +0930 Subject: [PATCH 4/8] Removed room preview I realised it didnt work for rooms you weren't already in, rendering it useless --- src/app/organisms/join-alias/JoinAlias.jsx | 47 ---------------------- 1 file changed, 47 deletions(-) diff --git a/src/app/organisms/join-alias/JoinAlias.jsx b/src/app/organisms/join-alias/JoinAlias.jsx index 7a267e67e1..bb90bf3545 100644 --- a/src/app/organisms/join-alias/JoinAlias.jsx +++ b/src/app/organisms/join-alias/JoinAlias.jsx @@ -18,9 +18,6 @@ import Dialog from '../../molecules/dialog/Dialog'; import CrossIC from '../../../../public/res/ic/outlined/cross.svg'; import { useStore } from '../../hooks/useStore'; -import RoomTile from '../../molecules/room-tile/RoomTile'; - -import { Debounce } from '../../../util/common'; const ALIAS_OR_ID_REG = /^[#|!].+:.+\..+$/; @@ -28,13 +25,9 @@ function JoinAliasContent({ term, requestClose }) { const [process, setProcess] = useState(false); const [error, setError] = useState(undefined); const [lastJoinId, setLastJoinId] = useState(undefined); - const [preview, setPreview] = useState(undefined); - const [loadingPreview, setLoadingPreview] = useState(undefined); - const [debounce] = useState(new Debounce()); const mx = initMatrix.matrixClient; const mountStore = useStore(); - let roomData = null; const openRoom = (roomId) => { const room = mx.getRoom(roomId); @@ -55,32 +48,6 @@ function JoinAliasContent({ term, requestClose }) { }; }, [lastJoinId]); - const loadPreview = async (text) => { - setLoadingPreview(true); - setPreview(undefined); - debounce._(async () => { - try { - let roomId = text; - - if (text.startsWith('#')) { - const room = await mx.resolveRoomAlias(text); - roomId = room.room_id; - } - - roomData = await mx.getRoom(roomId); - setLoadingPreview(false); - setPreview(roomData); - } catch { - setLoadingPreview(false); - setPreview(undefined); - } - }, 500)(); - }; - - if (loadingPreview === undefined && term) { - loadPreview(term); - } - const handleSubmit = async (e) => { e.preventDefault(); mountStore.setItem(true); @@ -118,10 +85,6 @@ function JoinAliasContent({ term, requestClose }) { } }; - if (term && loadingPreview === undefined) { - loadPreview(term); - } - return ( (loadPreview(e.target.value))} /> {error && {error}} - { loadingPreview ? : null } - { preview ? ( - - ) : null }
{ process From 80e3f1be330f9d4dbcc96d83875f63724597c4e3 Mon Sep 17 00:00:00 2001 From: Dylan <36567925+Airyzz@users.noreply.github.com> Date: Tue, 30 Aug 2022 19:48:40 +0930 Subject: [PATCH 5/8] make matrix.to navigate to room if already a member --- src/util/linkHandlers.jsx | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/src/util/linkHandlers.jsx b/src/util/linkHandlers.jsx index bbc9029dbd..db0464a6d7 100644 --- a/src/util/linkHandlers.jsx +++ b/src/util/linkHandlers.jsx @@ -1,10 +1,34 @@ import { invoke } from '@tauri-apps/api/tauri'; import { openJoinAlias } from '../client/action/navigation'; +import initMatrix from '../client/initMatrix'; +import { selectRoom, selectTab } from '../client/action/navigation'; + +function handleMatrix(url) { + const mx = initMatrix.matrixClient; + const rooms = mx.getRooms(); + + const roomName = url.hash.slice(2); + + let joinedRoom; + + rooms.forEach((room) => { + if (room.getCanonicalAlias() === roomName || roomName in room.getAltAliases()) { + joinedRoom = room; + } + }); + + if (joinedRoom) { + if (joinedRoom.isSpaceRoom()) selectTab(joinedRoom.roomId); + else selectRoom(joinedRoom.roomId); + + return; + } + + openJoinAlias(roomName); +} const handlers = { - 'matrix.to': (url) => { - openJoinAlias(url.hash.slice(2)); - }, + 'matrix.to': (url) => handleMatrix(url), }; export default function handleLink(e) { From e5449ab081277ea808a598dda7b5e512b47b7699 Mon Sep 17 00:00:00 2001 From: Dylan <36567925+Airyzz@users.noreply.github.com> Date: Wed, 31 Aug 2022 16:19:14 +0930 Subject: [PATCH 6/8] Update linkHandlers.jsx --- src/util/linkHandlers.jsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/util/linkHandlers.jsx b/src/util/linkHandlers.jsx index db0464a6d7..aaea3b6f52 100644 --- a/src/util/linkHandlers.jsx +++ b/src/util/linkHandlers.jsx @@ -1,7 +1,6 @@ import { invoke } from '@tauri-apps/api/tauri'; -import { openJoinAlias } from '../client/action/navigation'; import initMatrix from '../client/initMatrix'; -import { selectRoom, selectTab } from '../client/action/navigation'; +import { selectRoom, selectTab, openJoinAlias } from '../client/action/navigation'; function handleMatrix(url) { const mx = initMatrix.matrixClient; From 3f12454b21075817b7cfd0705490ebbdb6c33b37 Mon Sep 17 00:00:00 2001 From: Dylan <36567925+Airyzz@users.noreply.github.com> Date: Sun, 4 Sep 2022 12:16:14 +0930 Subject: [PATCH 7/8] Fix Tauri call + Matrix.to handler --- src/util/linkHandlers.jsx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/util/linkHandlers.jsx b/src/util/linkHandlers.jsx index aaea3b6f52..7de4e06d29 100644 --- a/src/util/linkHandlers.jsx +++ b/src/util/linkHandlers.jsx @@ -11,7 +11,9 @@ function handleMatrix(url) { let joinedRoom; rooms.forEach((room) => { - if (room.getCanonicalAlias() === roomName || roomName in room.getAltAliases()) { + if (room.roomId === roomName + || room.getCanonicalAlias() === roomName + || roomName in room.getAltAliases()) { joinedRoom = room; } }); @@ -43,11 +45,9 @@ export default function handleLink(e) { // if running inside tauri, check if the tauri backend has a custom handler for this link // useful so we can open urls in a specified app if (window.__TAURI__) { - invoke('open_link', { url: e.target.href }) - .then(() => e.preventDefault()) - .catch((error) => console.error(error)); + e.preventDefault(); + invoke('open_link', { url: e.target.href, bypassHandlers: false }); } else { - // open in new tab window.open(e.target.href); e.preventDefault(); } From 2488b01bdaee47a6d66745b350bd297bf81c770b Mon Sep 17 00:00:00 2001 From: Dylan <36567925+Airyzz@users.noreply.github.com> Date: Sun, 9 Oct 2022 18:00:23 +1030 Subject: [PATCH 8/8] Reformatting code --- src/util/linkHandlers.js | 27 +++++++++++++++ src/util/linkHandlers.jsx | 54 ------------------------------ src/util/linkHandlers/matrix.to.js | 30 +++++++++++++++++ 3 files changed, 57 insertions(+), 54 deletions(-) create mode 100644 src/util/linkHandlers.js delete mode 100644 src/util/linkHandlers.jsx create mode 100644 src/util/linkHandlers/matrix.to.js diff --git a/src/util/linkHandlers.js b/src/util/linkHandlers.js new file mode 100644 index 0000000000..256875d0f1 --- /dev/null +++ b/src/util/linkHandlers.js @@ -0,0 +1,27 @@ +import { invoke } from '@tauri-apps/api/tauri'; +import handleMatrix from './linkHandlers/matrix.to'; + +const handlers = { + 'matrix.to': (url) => handleMatrix(url), +}; + +export default function handleLink(e) { + const url = new URL(e.target.href); + const handler = handlers[url.hostname]; + + if (handler) { + handler(url); + e.preventDefault(); + return; + } + + // if running inside tauri, let the backend handle the opening of the url + // useful so we can open urls in the user's browser specified app or a specified app + if (window.__TAURI__) { + e.preventDefault(); + invoke('open_link', { url: e.target.href, bypassHandlers: false }); + } else { + window.open(e.target.href); + e.preventDefault(); + } +} diff --git a/src/util/linkHandlers.jsx b/src/util/linkHandlers.jsx deleted file mode 100644 index 7de4e06d29..0000000000 --- a/src/util/linkHandlers.jsx +++ /dev/null @@ -1,54 +0,0 @@ -import { invoke } from '@tauri-apps/api/tauri'; -import initMatrix from '../client/initMatrix'; -import { selectRoom, selectTab, openJoinAlias } from '../client/action/navigation'; - -function handleMatrix(url) { - const mx = initMatrix.matrixClient; - const rooms = mx.getRooms(); - - const roomName = url.hash.slice(2); - - let joinedRoom; - - rooms.forEach((room) => { - if (room.roomId === roomName - || room.getCanonicalAlias() === roomName - || roomName in room.getAltAliases()) { - joinedRoom = room; - } - }); - - if (joinedRoom) { - if (joinedRoom.isSpaceRoom()) selectTab(joinedRoom.roomId); - else selectRoom(joinedRoom.roomId); - - return; - } - - openJoinAlias(roomName); -} - -const handlers = { - 'matrix.to': (url) => handleMatrix(url), -}; - -export default function handleLink(e) { - const url = new URL(e.target.href); - const handler = handlers[url.hostname]; - - if (handler) { - handler(url); - e.preventDefault(); - return; - } - - // if running inside tauri, check if the tauri backend has a custom handler for this link - // useful so we can open urls in a specified app - if (window.__TAURI__) { - e.preventDefault(); - invoke('open_link', { url: e.target.href, bypassHandlers: false }); - } else { - window.open(e.target.href); - e.preventDefault(); - } -} diff --git a/src/util/linkHandlers/matrix.to.js b/src/util/linkHandlers/matrix.to.js new file mode 100644 index 0000000000..a6f9009c21 --- /dev/null +++ b/src/util/linkHandlers/matrix.to.js @@ -0,0 +1,30 @@ +import initMatrix from '../../client/initMatrix'; +import { selectRoom, selectTab, openJoinAlias } from '../../client/action/navigation'; + +function handleMatrix(url) { + const mx = initMatrix.matrixClient; + const rooms = mx.getRooms(); + + const roomName = url.hash.slice(2); + + let joinedRoom; + + rooms.forEach((room) => { + if (room.roomId === roomName + || room.getCanonicalAlias() === roomName + || roomName in room.getAltAliases()) { + joinedRoom = room; + } + }); + + if (joinedRoom) { + if (joinedRoom.isSpaceRoom()) selectTab(joinedRoom.roomId); + else selectRoom(joinedRoom.roomId); + + return; + } + + openJoinAlias(roomName); +} + +export default handleMatrix;