Skip to content

Commit

Permalink
Add matrix-URI parsing support using matrix-uri-parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Unkn0wnCat committed Sep 28, 2021
1 parent 7d22a57 commit 9c48ff8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
24 changes: 24 additions & 0 deletions src/Link.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = /^!([^:]*):(.+)$/;
Expand Down Expand Up @@ -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 = [];
Expand Down
4 changes: 4 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 9c48ff8

Please sign in to comment.