diff --git a/src/App.vue b/src/App.vue index db57be4..2489ad9 100644 --- a/src/App.vue +++ b/src/App.vue @@ -66,8 +66,7 @@ export default { openDrawer: false, }), mounted() { - this.$router.replace("/"); - + //this.$router.replace("/"); const version = require("!!raw-loader!./assets/version.txt").default; console.log("Version", version); this.buildVersion = version; @@ -90,11 +89,10 @@ export default { currentUser: { immediate: true, handler(ignorednewVal, ignoredoldVal) { - if (this.loggedIn) { - const self = this; + if (this.loggedIn()) { this.$matrix.getMatrixClient(this.currentUser) .then(() => { - self.$matrix.initClient(); + console.log("Matrix client ready"); }) .catch((error) => { console.log("Error creating client", error); diff --git a/src/components/Join.vue b/src/components/Join.vue new file mode 100644 index 0000000..c47419f --- /dev/null +++ b/src/components/Join.vue @@ -0,0 +1,80 @@ + + + + Join room + You have been invited to the room {{ roomId }} + + Join as guest + + {{ loadingMessage }} + + + + + + + \ No newline at end of file diff --git a/src/models/user.js b/src/models/user.js index ff6cd37..5e3f899 100644 --- a/src/models/user.js +++ b/src/models/user.js @@ -1,7 +1,8 @@ export default class User { - constructor(server, username, password) { + constructor(server, username, password, is_guest) { this.server = server; this.username = username; this.password = password; + this.is_guest = is_guest || false } } \ No newline at end of file diff --git a/src/router/index.js b/src/router/index.js index 7e87787..c540af0 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -16,14 +16,9 @@ const routes = [ component: Login }, { - path: '/join/:room?', - redirect: from => { - const room = from.hash; - if (room) { - return { name: 'Chat', params: { joinRoom: room }}; - } - return '/'; - } + path: '/join/', + component: () => import('../components/Join.vue'), + props: true }, ] @@ -33,7 +28,7 @@ const router = new VueRouter({ router.beforeEach((to, from, next) => { const publicPages = ['/login']; - const authRequired = !publicPages.includes(to.path); + const authRequired = !publicPages.includes(to.path) && !to.path.startsWith('/join/'); const loggedIn = localStorage.getItem('user'); // trying to access a restricted page + not logged in diff --git a/src/services/matrix.service.js b/src/services/matrix.service.js index 400dcda..37d7318 100644 --- a/src/services/matrix.service.js +++ b/src/services/matrix.service.js @@ -31,7 +31,7 @@ export default { computed: { ready() { - return this.matrixClient != null; + return this.matrixClient != null && this.matrixClientReady; }, currentUser() { @@ -41,32 +41,40 @@ export default { currentRoomId() { return this.$store.state.currentRoomId; }, - + currentRoom() { return this.getRoom(this.currentRoomId); }, - + }, methods: { login(user) { const tempMatrixClient = sdk.createClient(user.server); - var retUser; - return tempMatrixClient - .login("m.login.password", { user: user.username, password: user.password, type: "m.login.password" }) - .then((response) => { - localStorage.setItem('user', JSON.stringify(response)); - return response; - }) + var promiseLogin; + + if (user.is_guest) { + promiseLogin = tempMatrixClient + .registerGuest({}, undefined) + .then((response) => { + console.log("Response", response); + response.is_guest = true; + localStorage.setItem('user', JSON.stringify(response)); + return response; + }) + } else { + promiseLogin = tempMatrixClient + .login("m.login.password", { user: user.username, password: user.password, type: "m.login.password" }) + .then((response) => { + localStorage.setItem('user', JSON.stringify(response)); + return response; + }) + } + + return promiseLogin .then(user => { - retUser = user; return this.getMatrixClient(user); }) - .then(() => { - // Ready to use! Start by loading rooms. - this.initClient(); - return retUser; - }) }, logout() { @@ -87,6 +95,18 @@ export default { }, async getMatrixClient(user) { + if (this.matrixClientReady) { + return new Promise((resolve,ignoredreject) => { + resolve(user); + }) + } else if (this.matrixClient) { + return new Promise((resolve,ignoredreject) => { + this.matrixClient.once('Matrix.initialized', (ignoredclient) => { + resolve(user); + }); + }) + } + const matrixStore = new sdk.MemoryStore(window.localStorage); const webStorageSessionStore = new sdk.WebStorageSessionStore( window.localStorage @@ -104,9 +124,13 @@ export default { sessionStore: webStorageSessionStore, deviceId: user.device_id, accessToken: user.access_token, - timelineSupport: true + timelineSupport: true, + unstableClientRelationAggregation: true } this.matrixClient = sdk.createClient(opts); + if (user.is_guest) { + this.matrixClient.setGuest(true); + } return this.matrixClient .initCrypto() .then(() => { @@ -136,7 +160,12 @@ export default { ) }); } - }); + }) + .then(() => { + // Ready to use! Start by loading rooms. + this.initClient(); + return user; + }) }, addMatrixClientListeners(client) { @@ -159,7 +188,7 @@ export default { Vue.set(room, "topic", event.getContent().topic); } } - break; + break; case "m.room.avatar": { const room = this.matrixClient.getRoom(event.getRoomId()); @@ -167,7 +196,7 @@ export default { Vue.set(room, "avatar", room.getAvatarUrl(this.matrixClient.getHomeserverUrl(), 80, 80, "scale", true)); } } - break; + break; } },