Skip to content

Commit 9c48ff8

Browse files
committed
Add matrix-URI parsing support using matrix-uri-parser
1 parent 7d22a57 commit 9c48ff8

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,8 @@
3333
"rollup-plugin-terser": "^7.0.2",
3434
"serve-static": "^1.14.1",
3535
"xxhashjs": "^0.2.2"
36+
},
37+
"dependencies": {
38+
"matrix-uri-parser": "https://github.com/matrix-org/matrix-uri-parser-js"
3639
}
3740
}

src/Link.js

+24
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
import {createEnum} from "./utils/enum.js";
1818
import {orderedUnique} from "./utils/unique.js";
19+
import {MatrixURL} from "matrix-uri-parser";
1920

2021
const ROOMALIAS_PATTERN = /^#([^:]*):(.+)$/;
2122
const ROOMID_PATTERN = /^!([^:]*):(.+)$/;
@@ -111,6 +112,29 @@ export class Link {
111112
return null;
112113
}
113114
linkStr = linkStr.substr(2);
115+
116+
const linkStrMatrixUrl = fragment.substr(2).startsWith("matrix:") ? fragment.substr(2) : "matrix:"+fragment.substr(2);
117+
118+
try {
119+
const parsedUrl = new MatrixURL(linkStrMatrixUrl); // Try to parse URL as matrix-URL
120+
121+
let [localPart, server] = parsedUrl.id.split(":"); // Split ID into local and server part
122+
123+
if(!server) server = parsedUrl.authority; // If no server part is present, try to use authority
124+
if(!server) return null; // Reject if we can't figure out the server
125+
126+
const webInstances = getWebInstanceMap(parsedUrl.unknownParams);
127+
128+
let clientId;
129+
130+
const clientParam = parsedUrl.unknownParams.find(([key]) => key === "client");
131+
if(clientParam) clientId = clientParam[1];
132+
133+
const link = new Link(clientId, parsedUrl.via, parsedUrl.kind, localPart, server, webInstances, parsedUrl.eventId);
134+
135+
return link;
136+
} catch(e) {} // The fragment was no matrix-URL.
137+
114138
const [identifier, eventId] = linkStr.split("/");
115139

116140
let viaServers = [];

yarn.lock

+4
Original file line numberDiff line numberDiff line change
@@ -1418,6 +1418,10 @@ matched@^5.0.0:
14181418
glob "^7.1.6"
14191419
picomatch "^2.2.1"
14201420

1421+
"matrix-uri-parser@https://github.com/matrix-org/matrix-uri-parser-js":
1422+
version "0.1.0"
1423+
resolved "https://github.com/matrix-org/matrix-uri-parser-js#506c2545dae5e0d05633bc905a937731a1e69104"
1424+
14211425
mdn-polyfills@^5.20.0:
14221426
version "5.20.0"
14231427
resolved "https://registry.yarnpkg.com/mdn-polyfills/-/mdn-polyfills-5.20.0.tgz#ca8247edf20a4f60dec6804372229812b348260b"

0 commit comments

Comments
 (0)