MOBILE-3797 user: Fix infinite requests loop

main
Noel De Martin 2021-07-12 16:02:27 +02:00
parent 7faf348042
commit 996e4db71b
1 changed files with 39 additions and 2 deletions

View File

@ -169,7 +169,7 @@ export class CoreUserProfilePage implements OnInit, OnDestroy {
return;
}
if (this.userId != this.site.getUserId() || this.user.profileimageurl == this.site.getInfo()!.userpictureurl) {
if (this.userId != this.site.getUserId() || !this.isUserAvatarDirty()) {
// Not current user or hasn't changed.
return;
}
@ -186,7 +186,7 @@ export class CoreUserProfilePage implements OnInit, OnDestroy {
}, this.site.getId());
}
if (this.user.profileimageurl != this.site.getInfo()!.userpictureurl) {
if (this.isUserAvatarDirty()) {
// The image is still different, this means that the good one is the one in site info.
await this.refreshUser();
} else {
@ -285,4 +285,41 @@ export class CoreUserProfilePage implements OnInit, OnDestroy {
this.obsProfileRefreshed.off();
}
/**
* Check whether the user avatar is not up to date with site info.
*
* @return Whether the user avatar differs from site info cache.
*/
private isUserAvatarDirty(): boolean {
if (!this.user || !this.site) {
return false;
}
const courseAvatarUrl = this.normalizeAvatarUrl(this.user.profileimageurl);
const siteAvatarUrl = this.normalizeAvatarUrl(this.site.getInfo()?.userpictureurl);
return courseAvatarUrl !== siteAvatarUrl;
}
/**
* Normalize an avatar url regardless of theme.
*
* Given that the default image is the only one that can be changed per theme, any other url will stay the same. Note that
* the values returned by this function may not be valid urls, given that they are intended for string comparison.
*
* @param avatarUrl Avatar url.
* @return Normalized avatar string (may not be a valid url).
*/
private normalizeAvatarUrl(avatarUrl?: string): string {
if (!avatarUrl) {
return 'undefined';
}
if (avatarUrl.startsWith(`${this.site?.siteUrl}/theme/image.php`)) {
return 'default';
}
return avatarUrl;
}
}