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,54 +106,35 @@
});
}
// 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.
img.onload = function() {
$div.text('');
$div.append(img);
$div.clearimageplaceholder();
// If the new image loads successfully set it.
img.onload = function() {
$div.text('');
$div.append(img);
$div.clearimageplaceholder();
if(typeof callback === 'function') {
callback();
}
};
if(typeof callback === 'function') {
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.show();
img.width = size;
img.height = size;
img.src = url;
}
$div.addClass('icon-loading');
$div.show();
img.width = size;
img.height = size;
img.src = url;
};
}(jQuery));