diff --git a/package.json b/package.json index 5a879c3..23b3178 100644 --- a/package.json +++ b/package.json @@ -33,5 +33,8 @@ "rollup-plugin-terser": "^7.0.2", "serve-static": "^1.14.1", "xxhashjs": "^0.2.2" + }, + "dependencies": { + "matrix-uri-parser": "https://github.com/matrix-org/matrix-uri-parser-js" } } diff --git a/src/Link.js b/src/Link.js index 4727077..119e6de 100644 --- a/src/Link.js +++ b/src/Link.js @@ -16,6 +16,7 @@ limitations under the License. import {createEnum} from "./utils/enum.js"; import {orderedUnique} from "./utils/unique.js"; +import {MatrixURL} from "matrix-uri-parser"; const ROOMALIAS_PATTERN = /^#([^:]*):(.+)$/; const ROOMID_PATTERN = /^!([^:]*):(.+)$/; @@ -111,6 +112,29 @@ export class Link { return null; } linkStr = linkStr.substr(2); + + const linkStrMatrixUrl = fragment.substr(2).startsWith("matrix:") ? fragment.substr(2) : "matrix:"+fragment.substr(2); + + try { + const parsedUrl = new MatrixURL(linkStrMatrixUrl); // Try to parse URL as matrix-URL + + let [localPart, server] = parsedUrl.id.split(":"); // Split ID into local and server part + + if(!server) server = parsedUrl.authority; // If no server part is present, try to use authority + if(!server) return null; // Reject if we can't figure out the server + + const webInstances = getWebInstanceMap(parsedUrl.unknownParams); + + let clientId; + + const clientParam = parsedUrl.unknownParams.find(([key]) => key === "client"); + if(clientParam) clientId = clientParam[1]; + + const link = new Link(clientId, parsedUrl.via, parsedUrl.kind, localPart, server, webInstances, parsedUrl.eventId); + + return link; + } catch(e) {} // The fragment was no matrix-URL. + const [identifier, eventId] = linkStr.split("/"); let viaServers = []; diff --git a/yarn.lock b/yarn.lock index e253b40..dd87f7f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1418,6 +1418,10 @@ matched@^5.0.0: glob "^7.1.6" picomatch "^2.2.1" +"matrix-uri-parser@https://github.com/matrix-org/matrix-uri-parser-js": + version "0.1.0" + resolved "https://github.com/matrix-org/matrix-uri-parser-js#506c2545dae5e0d05633bc905a937731a1e69104" + mdn-polyfills@^5.20.0: version "5.20.0" resolved "https://registry.yarnpkg.com/mdn-polyfills/-/mdn-polyfills-5.20.0.tgz#ca8247edf20a4f60dec6804372229812b348260b"