Merge branch '524-don-t-display-homeserver-in-links-if-not-needed' into 'dev'

Don't include server in DM links if not needed

See merge request keanuapp/keanuapp-weblite!240
This commit is contained in:
N Pex 2023-10-09 17:12:41 +00:00
commit c46d3a698f
5 changed files with 28 additions and 5 deletions

View file

@ -109,7 +109,7 @@ export default {
return this.$navigation && this.$navigation.canPop(); return this.$navigation && this.$navigation.canPop();
}, },
directMessageLink() { 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() { shareSupported() {
return !!navigator.share; return !!navigator.share;

View file

@ -243,7 +243,7 @@ export default {
return this.$matrix.currentUser.user_id return this.$matrix.currentUser.user_id
}, },
directMessageLink() { directMessageLink() {
return `${window.location.origin + window.location.pathname}#/user/${this.currentUserId}` return this.$router.getDMLink(this.$matrix.currentUser, this.$config);
}, },
passwordsMatch() { passwordsMatch() {
return ( return (

View file

@ -29,4 +29,12 @@ export default class User {
} }
return user_id; 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];
}
} }

View file

@ -574,7 +574,7 @@ class Util {
} }
sanitizeUserId(userId) { sanitizeUserId(userId) {
if (userId && userId.match(/^@.+$/)) { if (userId && userId.match(/^([0-9a-z-.=_/]+|@[0-9a-z-.=_/]+:.+)$/)) {
return userId; return userId;
} }
return null; return null;

View file

@ -7,7 +7,7 @@ import Login from '../components/Login.vue'
import Profile from '../components/Profile.vue' import Profile from '../components/Profile.vue'
import CreateRoom from '../components/CreateRoom.vue' import CreateRoom from '../components/CreateRoom.vue'
import GetLink from '../components/GetLink.vue' import GetLink from '../components/GetLink.vue'
import User from '../models/user'
import util from '../plugins/utils' import util from '../plugins/utils'
Vue.use(VueRouter) Vue.use(VueRouter)
@ -126,7 +126,13 @@ router.beforeEach((to, from, next) => {
} }
} else if (to.name == 'User') { } else if (to.name == 'User') {
if (to.params.userId) { 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); router.app.$matrix.setCurrentRoomId(roomId);
authRequired = false; 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)); 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 export default router