keanu-weblite/src/components/Join.vue

424 lines
12 KiB
Vue
Raw Normal View History

2020-11-25 10:02:24 +01:00
<template>
2021-12-19 16:04:19 +02:00
<div class="join-room" v-if="!waitingForInfo && !waitingForMembership">
<div class="join-enter-room">
<div>
<div class="text-center">
<v-avatar class="join-avatar">
<v-img v-if="roomAvatar" :src="roomAvatar" />
<span v-else class="white--text headline">
{{roomName.substring(0, 1).toUpperCase() }}
</span>
</v-avatar>
<div class="join-welcome font-weight-bold">
{{ $t("join.title") }}
</div>
<div class="join-title">
{{ roomName }}
</div>
</div>
<hr class="my-10 join-line">
<div class="font-weight-black mb-2" v-if="!currentUser"> Choose a name to use.</div>
2020-11-25 10:02:24 +01:00
<v-row v-if="canEditProfile">
2021-12-19 16:04:19 +02:00
<v-col cols="10" sm="7">
<v-text-field
solo
@click.native.stop="{}"
v-model="selectedProfile.name"
></v-text-field>
<v-checkbox id="chk-remember-me" class="mt-0" v-model="rememberMe" :label="$t('join.remember_me')" />
2021-12-19 16:04:19 +02:00
</v-col>
<v-col cols="2" sm="5">
<v-avatar @click="showAvatarPickerList">
<v-img v-if="selectedProfile" :src="selectedProfile.image" />
2021-01-20 09:42:13 +01:00
</v-avatar>
</v-col>
</v-row>
2021-12-19 16:04:19 +02:00
<v-row v-else class="text-center">
<v-col>
{{ $t("join.joining_as") }}
<div style="display: inline-block">
<v-avatar color="#e0e0e0" style="">
<v-img v-if="userAvatar" :src="userAvatar" />
<span v-else class="white--text headline">{{
userAvatarLetter
}}</span>
</v-avatar>
</div>
{{ userDisplayName }}
</v-col>
</v-row>
2020-12-16 16:36:23 +01:00
2021-12-19 16:04:19 +02:00
<v-btn
id="btn-join"
2021-12-19 16:04:19 +02:00
class="btn-dark"
large
@click.stop="handleJoin"
:loading="loading"
v-if="!currentUser"
>{{ $t("join.enter_room") }}</v-btn
>
<v-btn
id="btn-join"
2021-12-19 16:04:19 +02:00
class="btn-dark"
large
block
@click.stop="handleJoin"
:loading="loading"
v-else
>{{ $t("join.join") }}</v-btn
>
2020-12-16 15:57:44 +01:00
2021-12-19 16:04:19 +02:00
<div v-if="loadingMessage" class="text-center">{{ loadingMessage }}</div>
</div>
</div>
2020-11-25 10:02:24 +01:00
2021-12-19 16:04:19 +02:00
<div class="join-lang">
<h3 class="mb-2">{{ $t("profile.select_language") }}</h3>
<v-row class="align-center">
<v-col v-for="lang in activeLanguage" :key="lang.value">
<span class="active">{{ lang.text }}</span>
</v-col>
<v-col v-for="(lang, key) in getDisplayLanguage.slice(0,3)" :key="key">
<button class="inactive" @click="updateLanguage(lang.value)">{{ lang.text }}</button>
2021-12-19 16:04:19 +02:00
</v-col>
<v-col>
<v-btn
id="btn-select-language"
2021-12-19 16:04:19 +02:00
class="language-more"
@click="showSelectLanguageDialog = true"
>
<v-icon >language</v-icon><v-icon >more_horiz</v-icon>
</v-btn>
</v-col>
<SelectLanguageDialog
v-model="showSelectLanguageDialog"
v-on:close="showSelectLanguageDialog = false"
/>
</v-row>
</div>
2020-12-16 15:57:44 +01:00
2021-12-19 16:04:19 +02:00
<div class="d-flex justify-center align-center mt-9">
<div class="mr-2">
<img
src="@/assets/logo.svg"
width="32"
height="32"
class="d-inline"
/>
</div>
<div>
<strong>{{ $t("project.name") }}</strong>
{{ $t("project.tag_line") }}
</div>
2020-11-25 10:02:24 +01:00
</div>
</div>
</template>
<script>
import util from "../plugins/utils";
2021-12-19 16:04:19 +02:00
import LanguageMixin from "./languageMixin";
import SelectLanguageDialog from "./SelectLanguageDialog.vue";
2020-11-25 10:02:24 +01:00
export default {
name: "Join",
2021-12-19 16:04:19 +02:00
mixins: [LanguageMixin],
components: {
SelectLanguageDialog,
},
2020-11-25 10:02:24 +01:00
data() {
return {
2020-12-16 15:57:44 +01:00
roomName: null,
roomAvatar: null,
2020-11-25 10:02:24 +01:00
loading: false,
loadingMessage: null,
2021-01-08 11:07:02 +01:00
waitingForInfo: true,
waitingForMembership: false,
2021-01-20 09:42:13 +01:00
availableAvatars: [],
selectedProfile: null,
2021-12-19 16:04:19 +02:00
showEditDisplaynameDialog: false,
showSelectLanguageDialog: false,
2020-11-25 10:02:24 +01:00
};
},
computed: {
currentUser() {
return this.$store.state.auth.user;
},
2021-01-28 22:13:08 +01:00
room() {
return this.$matrix.currentRoom;
},
roomId() {
if (this.room) {
return this.room.roomId;
}
return this.$matrix.currentRoomId;
},
roomAliasOrId() {
if (this.room) {
return this.room.getCanonicalAlias() || this.room.roomId;
}
return this.$matrix.currentRoomId;
},
canEditProfile() {
// If we have an account already, we can't edit profile here (need to go into profile view)
if (this.currentUser) {
return false;
}
return true;
},
userDisplayName() {
return this.$matrix.currentUserDisplayName;
},
userAvatar() {
if (!this.$matrix.userAvatar) {
return null;
}
return this.$matrix.matrixClient.mxcUrlToHttp(
this.$matrix.userAvatar,
80,
80,
"scale",
true
);
},
userAvatarLetter() {
2021-03-10 13:40:32 +01:00
if (!this.currentUser || !this.currentUser.userId) {
return null;
}
return (
this.currentUserDisplayName || this.currentUser.userId.substring(1)
)
.substring(0, 1)
.toUpperCase();
},
2021-12-19 16:04:19 +02:00
rememberMe: {
get: function () {
return this.$store.state.useLocalStorage;
},
2021-12-19 16:04:19 +02:00
set: function (rememberMe) {
this.$store.commit("setUseLocalStorage", rememberMe);
},
},
getDisplayLanguage() {
let displayLanguages = [...this.getLanguages()];
return displayLanguages.filter(lang => lang.display && lang.value !== this.$i18n.locale);
},
activeLanguage() {
let activeLanguages = [...this.getLanguages()]
return activeLanguages.filter(lang => lang.value === this.$i18n.locale);
2021-12-19 16:04:19 +02:00
}
2020-11-25 10:02:24 +01:00
},
2021-01-28 22:13:08 +01:00
watch: {
roomId: {
immediate: true,
handler(value, oldVal) {
if (!value || (value && value == oldVal)) {
2021-01-28 22:13:08 +01:00
return; // No change.
}
console.log(
"Join: Current room changed to " + (value ? value : "null")
);
2021-01-28 22:13:08 +01:00
this.roomName = this.roomId;
this.waitingForInfo = true;
const self = this;
this.waitingForMembership = true;
if (this.currentUser) {
this.$matrix
.getLoginPromise()
.then(() => {
self.$matrix.setCurrentRoomId(self.roomAliasOrId); // Go to this room, now or when joined.
const room = self.$matrix.getRoom(self.roomAliasOrId);
2021-01-28 22:13:08 +01:00
// Already joined?
if (
room &&
room.hasMembershipState(self.currentUser.user_id, "join")
) {
// Yes, go to room
self.$navigation.push(
{
name: "Chat",
params: { roomId: util.sanitizeRoomId(this.roomAliasOrId) },
},
-1
);
return;
}
})
.catch((err) => {
console.log("Error logging in: ", err);
})
.finally(() => {
this.waitingForMembership = false;
this.getRoomInfo();
});
2021-01-28 22:13:08 +01:00
} else {
this.waitingForMembership = false;
this.getRoomInfo();
2021-01-28 22:13:08 +01:00
}
},
2021-01-28 22:13:08 +01:00
},
},
2020-11-25 10:02:24 +01:00
methods: {
getRoomInfo() {
if (this.roomId.startsWith("#")) {
this.$matrix
.getPublicRoomInfo(this.roomId)
.then((room) => {
console.log("Found room:", room);
this.roomName = room.name;
this.roomAvatar = room.avatar;
})
.catch((err) => {
console.log("Could not find room info", err);
})
.finally(() => {
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;
}
},
2021-01-12 20:28:23 +01:00
onMyMembership(room, membership, ignoredprevMembership) {
if (room && room.roomId == this.roomId && membership == "join") {
2021-01-28 22:13:08 +01:00
this.$nextTick(() => {
this.$navigation.push(
{
name: "Chat",
params: { roomId: util.sanitizeRoomId(this.roomAliasOrId) },
},
-1
);
});
2021-01-12 20:28:23 +01:00
}
},
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 = this.$t("join.status_logging_in");
const hasUser = this.currentUser ? true : false;
var setProfileData = false;
return this.$matrix
.getLoginPromise()
2021-01-20 09:42:13 +01:00
.then(
function (user) {
if (user.is_guest && !hasUser) {
// Newly created account, joining first room.
// Set avatar and display name to either the randomly chosen ones, or the
// ones the users has changed to.
setProfileData = true;
// Set display name and avatar directly on the matrix object.
if (
this.selectedProfile.name &&
this.selectedProfile.name.length > 0
) {
this.$matrix.userDisplayName = this.selectedProfile.name;
}
}
if (
!setProfileData ||
!this.selectedProfile.name ||
this.selectedProfile.name.length == 0
) {
2021-01-20 09:42:13 +01:00
return Promise.resolve(user);
} else {
console.log(
"Join: Set display name to: " + this.selectedProfile.name
);
2021-01-20 09:42:13 +01:00
return this.$matrix.matrixClient.setDisplayName(
this.selectedProfile.name,
2021-01-20 09:42:13 +01:00
undefined
);
}
}.bind(this)
)
.then(
function () {
if (!setProfileData || !this.selectedProfile.image) {
console.log("Join: No avatar change");
2021-01-20 09:42:13 +01:00
return Promise.resolve("no avatar");
} else {
console.log("Join: Updating avatar");
2021-01-20 09:42:13 +01:00
return util.setAvatar(
this.$matrix,
this.selectedProfile.image,
2021-01-20 09:42:13 +01:00
function (progress) {
console.log("Progress: " + JSON.stringify(progress));
}
);
}
}.bind(this)
)
.then(
function (ignoreduser) {
console.log("Join: joining room");
this.loadingMessage = this.$t("join.status_joining");
2021-01-20 09:42:13 +01:00
return this.$matrix.matrixClient.joinRoom(this.roomId);
}.bind(this)
)
2021-01-28 22:13:08 +01:00
.then((ignoredRoom) => {
2020-11-25 14:42:50 +01:00
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(
2021-01-28 22:13:08 +01:00
{
name: "Chat",
params: { roomId: util.sanitizeRoomId(this.roomAliasOrId) },
},
2021-01-12 20:28:23 +01:00
-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 = this.$t("join.join_failed");
2020-11-25 14:42:50 +01:00
});
2020-11-25 10:02:24 +01:00
},
selectAvatar(value) {
this.selectedProfile = Object.assign({}, value); // Make a copy, so editing does not destroy data
2021-01-20 09:42:13 +01:00
},
showAvatarPickerList() {
this.$refs.avatar.$refs.input.click();
},
2020-11-25 10:02:24 +01:00
},
2021-12-19 16:04:19 +02:00
mounted() {
this.$matrix.on("Room.myMembership", this.onMyMembership);
this.availableAvatars = util.getDefaultAvatars();
this.selectAvatar(
this.availableAvatars[
Math.floor(Math.random() * this.availableAvatars.length)
]
);
},
destroyed() {
this.$matrix.off("Room.myMembership", this.onMyMembership);
}
2020-11-25 10:02:24 +01:00
};
</script>
<style lang="scss">
2021-12-19 16:04:19 +02:00
@import "@/assets/css/join.scss";
2020-11-25 10:02:24 +01:00
</style>