Don't set avatar and display name if they haven't changed.

Related to issue #40.
This commit is contained in:
N-Pex 2021-01-29 21:41:43 +01:00
parent 8555436bc7
commit bdd6977728
7 changed files with 94 additions and 33 deletions

View file

@ -81,6 +81,20 @@ export default {
}, },
}, },
watch: { watch: {
'$route' (to, ignoredFrom) {
var title = "Keanu Weblite";
if (to.meta.title) {
title += " - " + to.meta.title;
}
if (to.meta.includeRoom) {
if (this.$matrix.currentRoom) {
title += " - " + (this.$matrix.currentRoom.summary.info.title || this.$matrix.currentRoom.roomId);
} else if (this.$matrix.currentRoomId) {
title += " - " + (this.$matrix.currentRoomId);
}
}
document.title = title;
},
currentUser: { currentUser: {
immediate: true, immediate: true,
handler(ignorednewVal, ignoredoldVal) { handler(ignorednewVal, ignoredoldVal) {

View file

@ -383,7 +383,7 @@ export default {
if (value && value == oldValue) { if (value && value == oldValue) {
return; // No change. return; // No change.
} }
console.log("Chat: Current room changed"); console.log("Chat: Current room changed to " + (value ? value : "null"));
// Clear old events // Clear old events
this.events = []; this.events = [];

View file

@ -25,7 +25,7 @@
<v-row> <v-row>
<v-col class="flex-grow-0 flex-shrink-0"> <v-col class="flex-grow-0 flex-shrink-0">
<v-avatar @click="showAvatarPicker = true"> <v-avatar @click="showAvatarPicker = true">
<v-img v-if="userAvatar" :src="userAvatar.image" /> <v-img v-if="avatar" :src="avatar.image" />
</v-avatar> </v-avatar>
</v-col> </v-col>
<v-col class="flex-shrink-1 flex-grow-1"> <v-col class="flex-shrink-1 flex-grow-1">
@ -33,7 +33,7 @@
v-if="!currentUser || currentUser" v-if="!currentUser || currentUser"
@update:search-input="updateDisplayName" @update:search-input="updateDisplayName"
:items="defaultDisplayNames" :items="defaultDisplayNames"
:value="displayName" :value="userDisplayName || displayName"
label="User name" label="User name"
outlined outlined
dense dense
@ -109,7 +109,7 @@
size="48" size="48"
@click="selectAvatar(avatar)" @click="selectAvatar(avatar)"
class="avatar-picker-avatar" class="avatar-picker-avatar"
:class="{ current: avatar === userAvatar }" :class="{ current: avatar === selectedAvatar }"
> >
<img :src="avatar.image" /> <img :src="avatar.image" />
</v-avatar> </v-avatar>
@ -138,17 +138,18 @@ export default {
loadingMessage: null, loadingMessage: null,
waitingForInfo: true, waitingForInfo: true,
waitingForMembership: false, waitingForMembership: false,
userAvatar: null, selectedAvatar: null,
displayName: null, displayName: null,
defaultDisplayNames: [], defaultDisplayNames: [],
availableAvatars: [], availableAvatars: [],
showAvatarPicker: false, showAvatarPicker: false,
shouldSetAvatar: false,
}; };
}, },
mounted() { mounted() {
this.$matrix.on("Room.myMembership", this.onMyMembership); this.$matrix.on("Room.myMembership", this.onMyMembership);
this.availableAvatars = util.getDefaultAvatars(); this.availableAvatars = util.getDefaultAvatars();
this.userAvatar = this.availableAvatars[0]; this.selectedAvatar = this.userAvatar || this.availableAvatars[0];
if (!this.currentUser || this.currentUser.is_guest) { if (!this.currentUser || this.currentUser.is_guest) {
var values = require("!!raw-loader!../assets/usernames.txt") var values = require("!!raw-loader!../assets/usernames.txt")
.default.split("\n") .default.split("\n")
@ -181,15 +182,30 @@ export default {
} }
return this.$matrix.currentRoomId; return this.$matrix.currentRoomId;
}, },
userDisplayName() {
return this.$matrix.userDisplayName;
},
userAvatar() {
if (!this.$matrix.userAvatar) {
return null;
}
return { id: 'user', image: this.$matrix.matrixClient.mxcUrlToHttp(this.$matrix.userAvatar, 80, 80, 'scale', true) };
},
avatar() {
if (!this.shouldSetAvatar) {
return this.userAvatar || this.selectedAvatar; // TODO - Use random
}
return this.selectedAvatar;
}
}, },
watch: { watch: {
roomId: { roomId: {
immediate: true, immediate: true,
handler(val, oldVal) { handler(value, oldVal) {
if (val && val == oldVal) { if (!value || (value && value == oldVal)) {
return; // No change. return; // No change.
} }
console.log("Join: Current room changed"); console.log("Join: Current room changed to " + (value ? value : "null"));
this.roomName = this.roomId; this.roomName = this.roomId;
if (this.currentUser) { if (this.currentUser) {
this.waitingForMembership = true; this.waitingForMembership = true;
@ -283,7 +299,7 @@ export default {
function (user) { function (user) {
if ( if (
(this.currentUser && !this.currentUser.is_guest) || (this.currentUser && !this.currentUser.is_guest) ||
!this.displayName !this.displayName || this.displayName == this.userDisplayName /* No change */
) { ) {
return Promise.resolve(user); return Promise.resolve(user);
} else { } else {
@ -298,13 +314,13 @@ export default {
function () { function () {
if ( if (
(this.currentUser && !this.currentUser.is_guest) || (this.currentUser && !this.currentUser.is_guest) ||
!this.userAvatar !this.shouldSetAvatar
) { ) {
return Promise.resolve("no avatar"); return Promise.resolve("no avatar");
} else { } else {
return util.setAvatar( return util.setAvatar(
this.$matrix.matrixClient, this.$matrix.matrixClient,
this.userAvatar.image, this.selectedAvatar.image,
function (progress) { function (progress) {
console.log("Progress: " + JSON.stringify(progress)); console.log("Progress: " + JSON.stringify(progress));
} }
@ -344,7 +360,10 @@ export default {
}, },
selectAvatar(avatar) { selectAvatar(avatar) {
this.userAvatar = avatar; if (avatar && (!this.userAvatar || this.userAvatar.image != avatar.image)) {
this.selectedAvatar = avatar;
this.shouldSetAvatar = true;
}
this.showAvatarPicker = false; this.showAvatarPicker = false;
}, },
}, },

View file

@ -405,7 +405,7 @@ class Util {
matrixClient.uploadContent(response.data, opts) matrixClient.uploadContent(response.data, opts)
.then((response) => { .then((response) => {
const uri = response.content_uri; const uri = response.content_uri;
return matrixClient.setProfileInfo('avatar_url', { avatar_url: uri }); return matrixClient.setAvatarUrl(uri);
}) })
.then(result => { .then(result => {
resolve(result); resolve(result);

View file

@ -18,18 +18,28 @@ const routes = [
{ {
path: '/room/:roomId?', path: '/room/:roomId?',
name: 'Chat', name: 'Chat',
component: Chat component: Chat,
meta: {
includeRoom: true
}
}, },
{ {
path: '/info', path: '/info',
name: 'RoomInfo', name: 'RoomInfo',
component: () => import('../components/RoomInfo.vue'), component: () => import('../components/RoomInfo.vue'),
props: true, props: true,
meta: {
title: 'Info',
includeRoom: true
}
}, },
{ {
path: '/profile', path: '/profile',
name: 'Profile', name: 'Profile',
component: Profile component: Profile,
meta: {
title: 'Profile'
}
}, },
{ {
path: '/login', path: '/login',

View file

@ -25,6 +25,8 @@ export default {
matrixClient: null, matrixClient: null,
matrixClientReady: false, matrixClientReady: false,
rooms: [], rooms: [],
userDisplayName: null,
userAvatar: null,
currentRoom: null, currentRoom: null,
} }
}, },
@ -149,6 +151,15 @@ export default {
this.reloadRooms(); this.reloadRooms();
this.matrixClientReady = true; this.matrixClientReady = true;
this.matrixClient.emit('Matrix.initialized', this.matrixClient); this.matrixClient.emit('Matrix.initialized', this.matrixClient);
this.matrixClient.getProfileInfo(this.currentUserId)
.then(info => {
console.log("Got user profile: " + JSON.stringify(info));
this.userDisplayName = info.displayname;
this.userAvatar = info.avatar_url;
})
.catch(err => {
console.log("Failed to get user profile: ", err);
})
}, },
async getMatrixClient(user) { async getMatrixClient(user) {
@ -266,6 +277,7 @@ export default {
}, },
onRoom(ignoredroom) { onRoom(ignoredroom) {
console.log("Got room: " + ignoredroom);
this.reloadRooms(); this.reloadRooms();
}, },
@ -300,8 +312,9 @@ export default {
Vue.set(room, "avatar", room.getAvatarUrl(this.matrixClient.getHomeserverUrl(), 80, 80, "scale", true)); Vue.set(room, "avatar", room.getAvatarUrl(this.matrixClient.getHomeserverUrl(), 80, 80, "scale", true));
} }
}); });
console.log("Reload rooms", updatedRooms);
Vue.set(this, "rooms", updatedRooms); Vue.set(this, "rooms", updatedRooms);
const currentRoom = this.getRoom(this.currentRoomId); const currentRoom = this.getRoom(this.$store.state.currentRoomId);
if (this.currentRoom != currentRoom) { if (this.currentRoom != currentRoom) {
this.currentRoom = currentRoom; this.currentRoom = currentRoom;
} }

View file

@ -13,13 +13,13 @@ export default {
}) })
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
if (this.nextRoutes) { if (nextRoutes) {
console.log("Nav: next routes set, going:", this.routes, this.nextRoutes); console.log("Nav: next routes set, going:", routes, nextRoutes);
this.routes = this.nextRoutes; routes = nextRoutes;
this.nextRoutes = null; nextRoutes = null;
if (this.routes.length > 0) { if (routes.length > 0) {
console.log("Redirecting to", this.routes.lastItem()); console.log("Redirecting to", routes[routes.length - 1]);
next(this.routes.lastItem()); next(routes[routes.length - 1]);
return; return;
} }
} }
@ -35,22 +35,28 @@ export default {
mode = 1; mode = 1;
} }
if (mode == -1) { if (mode == -1) {
const i = routes.length - 1;
nextRoutes = [route]; nextRoutes = [route];
if (i > 0) {
router.go(-i);
} else {
router.replace(route).catch((ignoredErr) => {});
}
} else if (mode == 0) { } else if (mode == 0) {
// Replace // Replace
nextRoutes = [...routes]; nextRoutes = [...routes];
nextRoutes.pop(); nextRoutes.pop();
nextRoutes.push(route); nextRoutes.push(route);
router.replace(route).catch((ignoredErr) => {});
} else { } else {
nextRoutes = [...routes]; nextRoutes = [...routes];
nextRoutes.push(route); nextRoutes.push(route);
}
const index = nextRoutes.length - routes.length;
const targetIndex = nextRoutes.length - 1;
console.log("Nav - index " + index + " Target " + targetIndex);
if (index < 0) {
console.log("Nav - go " + index);
router.go(index);
} else if (index == 0) {
console.log("Nav - replace");
router.replace(route).catch((ignoredErr) => {});
} else {
console.log("Nav - push");
router.push(route).catch((ignoredErr) => {}); router.push(route).catch((ignoredErr) => {});
} }
}, },
@ -63,8 +69,7 @@ export default {
}, },
pop() { pop() {
nextRoutes = [...routes]; routes.pop();
nextRoutes.pop();
router.go(-1); router.go(-1);
} }
} }