keanu-weblite/src/components/Join.vue

199 lines
5.1 KiB
Vue
Raw Normal View History

2020-11-25 10:02:24 +01:00
<template>
2020-12-16 15:57:44 +01:00
<div class="join-root">
2021-01-08 11:07:02 +01:00
<div v-if="!waitingForInfo && !waitingForMembership" class="text-center">
2020-12-16 16:36:23 +01:00
<v-btn
class="btn-login"
text
small
@click.stop="handleLogin"
:loading="loading"
2021-01-11 17:42:58 +01:00
v-if="!currentUser"
2020-12-16 16:36:23 +01:00
>Login</v-btn
>
2020-11-25 10:02:24 +01:00
2020-12-16 15:57:44 +01:00
<v-avatar class="join-avatar">
<v-img v-if="roomAvatar" :src="roomAvatar" />
<span v-else class="white--text headline">{{
roomName.substring(0, 1).toUpperCase()
2020-12-16 16:36:23 +01:00
}}</span>
2020-12-16 15:57:44 +01:00
</v-avatar>
<div class="join-title">Welcome to {{ roomName }}</div>
2020-12-16 16:36:23 +01:00
<div class="join-message">
<!-- Join the group chat in a web browser or with the Keanu app. -->
2020-12-16 16:36:23 +01:00
</div>
<!--<v-btn
2020-12-16 16:36:23 +01:00
class="btn-light"
large
block
@click.stop="handleOpenApp"
:loading="loading"
2020-12-16 15:57:44 +01:00
>Open Keanu app</v-btn
>
2020-12-16 16:36:23 +01:00
<div class="join-or-divider">OR</div> -->
2020-12-16 15:57:44 +01:00
2020-12-16 16:36:23 +01:00
<v-btn
class="btn-dark"
large
block
@click.stop="handleJoin"
:loading="loading"
2021-01-11 17:42:58 +01:00
v-if="!currentUser"
2020-11-25 10:02:24 +01:00
>Join as guest</v-btn
>
2021-01-11 17:42:58 +01:00
<v-btn
class="btn-dark"
large
block
@click.stop="handleJoin"
:loading="loading"
v-else
>Join room</v-btn
>
2020-11-25 10:02:24 +01:00
2020-12-16 16:36:23 +01:00
<div class="join-privacy">
Enhance your physical privacy. <a href="#">Learn how</a>
</div>
2020-12-16 15:57:44 +01:00
2020-11-25 10:02:24 +01:00
<div v-if="loadingMessage">{{ loadingMessage }}</div>
</div>
</div>
</template>
<script>
import User from "../models/user";
import util from "../plugins/utils";
2020-11-25 10:02:24 +01:00
export default {
name: "Join",
data() {
return {
roomId: null,
2020-12-16 15:57:44 +01:00
roomName: null,
roomAvatar: null,
2020-11-25 10:02:24 +01:00
guestUser: new User("https://neo.keanu.im", "", "", true),
loading: false,
loadingMessage: null,
2021-01-08 11:07:02 +01:00
waitingForInfo: true,
waitingForMembership: false,
2020-11-25 10:02:24 +01:00
};
},
mounted() {
2021-01-12 20:28:23 +01:00
this.$matrix.on("Room.myMembership", this.onMyMembership);
this.roomId = this.$matrix.currentRoomId;
2020-12-16 15:57:44 +01:00
this.roomName = this.roomId;
if (this.currentUser) {
2021-01-08 11:07:02 +01:00
this.waitingForMembership = true;
2020-12-16 16:36:23 +01:00
const self = this;
this.$matrix
.getMatrixClient(this.currentUser)
.then(() => {
2021-01-12 20:28:23 +01:00
self.$matrix.setCurrentRoomId(self.roomId); // Go to this room, now or when joined.
2021-01-12 20:28:23 +01:00
// Already joined?
const room = self.$matrix.getRoom(self.roomId);
if (
room &&
room.hasMembershipState(self.currentUser.user_id, "join")
) {
// Yes, go to room
self.$navigation.push(
{
name: "Chat",
params: { roomId: util.sanitizeRoomId(self.roomId) },
},
-1
);
return;
2021-01-08 11:07:02 +01:00
}
this.waitingForMembership = false;
})
2020-12-16 16:36:23 +01:00
.catch((ignoredErr) => {
2021-01-08 11:07:02 +01:00
this.waitingForMembership = false;
});
2020-12-16 16:36:23 +01:00
}
2021-01-12 20:28:23 +01:00
if (this.roomId.startsWith("#")) {
this.$matrix
.getPublicRoomInfo(this.roomId)
.then((room) => {
console.log("Found room:", room);
this.roomName = room.name;
this.roomAvatar = room.avatar;
this.waitingForInfo = false;
})
.catch((err) => {
console.log("Could not find room info", err);
this.waitingForInfo = false;
});
} else {
// Private room, try to get name
const room = this.$matrix.getRoom(this.roomId);
if (room) {
this.roomName = room.name || this.roomName;
}
this.waitingForInfo = false;
}
},
destroyed() {
this.$matrix.off("Room.myMembership", this.onMyMembership);
2020-11-25 10:02:24 +01:00
},
computed: {
currentUser() {
return this.$store.state.auth.user;
},
},
methods: {
2021-01-12 20:28:23 +01:00
onMyMembership(room, membership, ignoredprevMembership) {
if (room && room.roomId == this.roomId && membership == "join") {
this.$navigation.push({ name: "Chat", params: { roomId: this.roomId } },-1);
}
},
2020-12-16 15:57:44 +01:00
handleLogin() {
2021-01-11 17:42:58 +01:00
this.$navigation.push({ name: "Login" }, 1);
2020-12-16 15:57:44 +01:00
},
handleOpenApp() {
console.log("Open app..."); //TODO
},
2020-11-25 10:02:24 +01:00
handleJoin() {
this.loading = true;
this.loadingMessage = "Logging in...";
2020-11-25 14:42:50 +01:00
var clientPromise;
2020-11-25 10:02:24 +01:00
if (this.currentUser) {
2020-11-25 14:42:50 +01:00
clientPromise = this.$matrix.getMatrixClient(this.currentUser);
2020-11-25 10:02:24 +01:00
} else {
2020-11-25 14:42:50 +01:00
clientPromise = this.$store.dispatch("auth/login", this.guestUser);
2020-11-25 10:02:24 +01:00
}
2020-11-25 14:42:50 +01:00
return clientPromise
.then((ignoreduser) => {
this.loadingMessage = "Joining room...";
return this.$matrix.matrixClient.joinRoom(this.roomId);
})
.then((room) => {
this.loading = false;
this.loadingMessage = null;
2021-01-11 17:42:58 +01:00
this.$nextTick(() => {
2021-01-12 20:28:23 +01:00
this.$navigation.push(
{ name: "Chat", params: { roomId: room.roomId } },
-1
);
2021-01-11 17:42:58 +01:00
});
2020-11-25 14:42:50 +01:00
})
.catch((err) => {
// TODO - handle error
console.log("Failed to join room", err);
this.loading = false;
this.loadingMessage = err.toString();
});
2020-11-25 10:02:24 +01:00
},
},
};
</script>
<style lang="scss">
2020-12-16 15:57:44 +01:00
@import "@/assets/css/join.scss";
2020-11-25 10:02:24 +01:00
</style>