handle matrix.to links correctly. add partial support for #/user URLs

This commit is contained in:
Matthew Hodgson 2016-08-28 01:55:42 +01:00
parent 50d09f73f5
commit ad873c2b60
3 changed files with 92 additions and 44 deletions

View file

@ -95,20 +95,25 @@ function matrixLinkify(linkify) {
S_AT_NAME_COLON_DOMAIN_DOT.on(TT.TLD, S_USERID);
}
matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); };
matrixLinkify.onAliasClick = function(e, roomAlias) { e.preventDefault(); };
// stubs, overwritten in MatrixChat's componentDidMount
// matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); };
// matrixLinkify.onAliasClick = function(e, roomAlias) { e.preventDefault(); };
var escapeRegExp = function(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
};
// we only recognise URLs which match our current URL as being the same app
// as if someone explicitly links to vector.im/develop and we're on vector.im/beta
// they may well be trying to get us to explicitly go to develop.
// FIXME: intercept matrix.to URLs as well.
matrixLinkify.VECTOR_URL_PATTERN = "^(https?:\/\/)?" + escapeRegExp(window.location.host + window.location.pathname);
// Recognise URLs from both our local vector and official vector as vector.
// anyone else really should be using matrix.to.
matrixLinkify.VECTOR_URL_PATTERN = "^(?:https?:\/\/)?(?:"
+ escapeRegExp(window.location.host + window.location.pathname) + "|"
+ "(?:www\\.)?vector\\.im/(?:beta|staging|develop)/"
+ ")(#.*)";
matrixLinkify.MATRIXTO_URL_PATTERN = "^(?:https?:\/\/)?(?:www\\.)?matrix\\.to/#/((#|@).*)";
matrixLinkify.options = {
/*
events: function (href, type) {
switch (type) {
case "userid":
@ -125,14 +130,31 @@ matrixLinkify.options = {
};
}
},
*/
formatHref: function (href, type) {
switch (type) {
case 'roomalias':
return '#/room/' + href;
case 'userid':
return '#';
return '#/user/' + href;
default:
var m;
// FIXME: horrible duplication with HtmlUtils' transform tags
m = href.match(matrixLinkify.VECTOR_URL_PATTERN);
if (m) {
return m[1];
}
m = href.match(matrixLinkify.MATRIXTO_URL_PATTERN);
if (m) {
var entity = m[1];
if (entity[0] === '@') {
return '#'; // TODO
}
else if (entity[0] === '#') {
return '#/room/' + entity;
}
}
return href;
}
},
@ -143,7 +165,9 @@ matrixLinkify.options = {
target: function(href, type) {
if (type === 'url') {
if (href.match(matrixLinkify.VECTOR_URL_PATTERN)) {
if (href.match(matrixLinkify.VECTOR_URL_PATTERN) ||
href.match(matrixLinkify.MATRIXTO_URL_PATTERN))
{
return null;
}
else {