Skip to content

Commit

Permalink
Merge pull request #1489 from nextcloud/fix/avatar-cleanup
Browse files Browse the repository at this point in the history
Cleanup avatar fetching methods
  • Loading branch information
skjnldsv authored Oct 19, 2020
2 parents 141a716 + 988eb3e commit 1283661
Showing 1 changed file with 53 additions and 30 deletions.
83 changes: 53 additions & 30 deletions src/components/Avatar/Avatar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,10 @@ export default {
}
this.isMenuLoaded = true
},
/**
* Handle avatar loading if user or url defined
*/
loadAvatarUrl() {
this.isAvatarLoaded = false
Expand All @@ -459,44 +463,63 @@ export default {
return
}
const urlGenerator = (user, size) => {
let url = '/avatar/{user}/{size}'
if (this.isGuest) {
url = '/avatar/guest/{user}/{size}'
}
// Directly use the url if defined
if (this.isUrlDefined) {
this.updateImageIfValid(this.url)
return
}
let avatarUrl = generateUrl(
url,
{
user,
size,
})
const avatarUrl = this.avatarUrlGenerator(this.user, this.size)
const srcset = [
avatarUrl + ' 1x',
this.avatarUrlGenerator(this.user, this.size * 2) + ' 2x',
this.avatarUrlGenerator(this.user, this.size * 4) + ' 4x',
].join(', ')
// eslint-disable-next-line camelcase
if (user === getCurrentUser()?.uid && typeof oc_userconfig !== 'undefined') {
avatarUrl += '?v=' + oc_userconfig.avatar.version
}
this.updateImageIfValid(avatarUrl, srcset)
},
return avatarUrl
/**
* Generate an avatar url from the server's avatar endpoint
*
* @param {string} user the user id
* @param {number} size the desired size
* @returns {string}
*/
avatarUrlGenerator(user, size) {
let url = '/avatar/{user}/{size}'
if (this.isGuest) {
url = '/avatar/guest/{user}/{size}'
}
let avatarUrl = urlGenerator(this.user, this.size)
if (this.isUrlDefined) {
avatarUrl = this.url
let avatarUrl = generateUrl(
url,
{
user,
size,
})
// eslint-disable-next-line camelcase
if (user === getCurrentUser()?.uid && typeof oc_userconfig !== 'undefined') {
avatarUrl += '?v=' + oc_userconfig.avatar.version
}
const srcset = [
avatarUrl + ' 1x',
urlGenerator(this.user, this.size * 2) + ' 2x',
urlGenerator(this.user, this.size * 4) + ' 4x',
].join(', ')
return avatarUrl
},
/**
* Check if the provided url is valid and update Avatar if so
*
* @param {string} url the avatar url
* @param {array} srcset the avatar srcset
*/
updateImageIfValid(url, srcset = null) {
// skip loading
const userHasAvatar = getUserHasAvatar(this.user)
if (typeof userHasAvatar === 'boolean') {
this.isAvatarLoaded = true
this.avatarUrlLoaded = avatarUrl
if (!this.isUrlDefined) {
this.avatarUrlLoaded = url
if (srcset) {
this.avatarSrcSetLoaded = srcset
}
if (userHasAvatar === false) {
Expand All @@ -507,8 +530,8 @@ export default {
const img = new Image()
img.onload = () => {
this.avatarUrlLoaded = avatarUrl
if (!this.isUrlDefined) {
this.avatarUrlLoaded = url
if (srcset) {
this.avatarSrcSetLoaded = srcset
}
this.isAvatarLoaded = true
Expand All @@ -521,10 +544,10 @@ export default {
setUserHasAvatar(this.user, false)
}
if (!this.isUrlDefined) {
if (srcset) {
img.srcset = srcset
}
img.src = avatarUrl
img.src = url
},
},
}
Expand Down

0 comments on commit 1283661

Please sign in to comment.