diff --git a/src/components/GetLink.vue b/src/components/GetLink.vue index c08a99d..2c68cf8 100644 --- a/src/components/GetLink.vue +++ b/src/components/GetLink.vue @@ -109,7 +109,7 @@ export default { return this.$navigation && this.$navigation.canPop(); }, directMessageLink() { - return `${window.location.origin + window.location.pathname}#/user/${encodeURIComponent(this.$matrix.currentUser.user_id)}` + return this.$router.getDMLink(this.$matrix.currentUser, this.$config); }, shareSupported() { return !!navigator.share; diff --git a/src/components/Profile.vue b/src/components/Profile.vue index 838fb04..e38df87 100644 --- a/src/components/Profile.vue +++ b/src/components/Profile.vue @@ -243,7 +243,7 @@ export default { return this.$matrix.currentUser.user_id }, directMessageLink() { - return `${window.location.origin + window.location.pathname}#/user/${this.currentUserId}` + return this.$router.getDMLink(this.$matrix.currentUser, this.$config); }, passwordsMatch() { return ( diff --git a/src/models/user.js b/src/models/user.js index 1f423d2..9b46687 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -29,4 +29,12 @@ export default class User { } return user_id; } + + // Get the domain out of the home_server, so if that one is e.g. + // "https://yourdomain.com:8008" then we return "yourdomain.com" + static serverDomain(home_server) { + const parts = home_server.split("://"); + const serverAndPort = parts[parts.length - 1].split(/:|\//); + return serverAndPort[0]; + } } \ No newline at end of file diff --git a/src/plugins/utils.js b/src/plugins/utils.js index c49b1ca..6c12e37 100644 --- a/src/plugins/utils.js +++ b/src/plugins/utils.js @@ -574,7 +574,7 @@ class Util { } sanitizeUserId(userId) { - if (userId && userId.match(/^@.+$/)) { + if (userId && userId.match(/^([0-9a-z-.=_/]+|@[0-9a-z-.=_/]+:.+)$/)) { return userId; } return null; diff --git a/src/router/index.js b/src/router/index.js index 877d816..0ccdae6 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -7,7 +7,7 @@ import Login from '../components/Login.vue' import Profile from '../components/Profile.vue' import CreateRoom from '../components/CreateRoom.vue' import GetLink from '../components/GetLink.vue' - +import User from '../models/user' import util from '../plugins/utils' Vue.use(VueRouter) @@ -126,7 +126,13 @@ router.beforeEach((to, from, next) => { } } else if (to.name == 'User') { if (to.params.userId) { - const roomId = util.sanitizeUserId(to.params.userId); + let roomId = util.sanitizeUserId(to.params.userId); + if (roomId && !roomId.startsWith("@")) { + // Not a full username. Assume local name on this server. + const user = new User(router.app.$config.defaultServer, roomId, ""); + user.normalize(); + roomId = "@" + roomId + ":" + User.serverDomain(user.home_server); + } router.app.$matrix.setCurrentRoomId(roomId); authRequired = false; } @@ -166,4 +172,13 @@ router.getRoomLink = function (alias, roomId, roomName, mode) { return window.location.origin + window.location.pathname + "#/room/" + encodeURIComponent(util.sanitizeRoomId(alias || roomId)); } +router.getDMLink = function (user, config) { + let userId = user.user_id; + if (user.home_server === config.defaultServer) { + // Using default server, don't include it in the link + userId = User.localPart(user.user_id); + } + return `${window.location.origin + window.location.pathname}#/user/${encodeURIComponent(userId)}` +} + export default router