Fix fallback when image loading fails

With the new avatar endpoint there is no difference between unknown
users and errors when generating the placeholder avatar. Therefore the
avatar function will now show the old placeholder if both a user and
displayname was given as parameters.

In case there is no displayname provided we cannot build the proper
placeholder so the unknown user placeholder is shown.

The displayname is not required for the avatar anymore, so we can
get rid of the old code path for placeholders.

Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
This commit is contained in:
Julius Härtl 2017-12-16 14:13:22 +01:00
parent 6b5e3a9365
commit d497f79625
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
1 changed files with 27 additions and 46 deletions

View File

@ -106,36 +106,6 @@
}); });
} }
// If the displayname is not defined we use the old code path
if (typeof(displayname) === 'undefined') {
$.get(url).always(function(result, status) {
// if there is an error or an object returned (contains user information):
// -> show the fallback placeholder
if (typeof(result) === 'object' || status === 'error') {
if (!hidedefault) {
if (result.data && result.data.displayname) {
$div.imageplaceholder(user, result.data.displayname);
} else {
// User does not exist
setAvatarForUnknownUser($div);
}
} else {
$div.hide();
}
// else an image is transferred and should be shown
} else {
$div.show();
if (ie8fix === true) {
$div.html('<img width="' + size + '" height="' + size + '" src="'+url+'#'+Math.floor(Math.random()*1000)+'" alt="">');
} else {
$div.html('<img width="' + size + '" height="' + size + '" src="'+url+'" alt="">');
}
}
if(typeof callback === 'function') {
callback();
}
});
} else {
var img = new Image(); var img = new Image();
// If the new image loads successfully set it. // If the new image loads successfully set it.
@ -148,12 +118,23 @@
callback(); callback();
} }
}; };
// Fallback when avatar loading fails:
// Use old placeholder when a displayname attribute is defined,
// otherwise show the unknown user placeholder.
img.onerror = function () {
$div.clearimageplaceholder();
if (typeof(displayname) !== 'undefined') {
$div.imageplaceholder(user, displayname);
} else {
setAvatarForUnknownUser($div);
$div.removeClass('icon-loading');
}
};
$div.addClass('icon-loading'); $div.addClass('icon-loading');
$div.show(); $div.show();
img.width = size; img.width = size;
img.height = size; img.height = size;
img.src = url; img.src = url;
}
}; };
}(jQuery)); }(jQuery));