handle single-character usernames when deriving initials from usernames

and use emoji avatar in user admin button
pull/1/head
ansuz 3 years ago
parent 46e545a976
commit 385cd4e947

@ -1993,9 +1993,11 @@ define([
var loadingAvatar; var loadingAvatar;
var to; var to;
var oldUrl = ''; var oldUrl = '';
var oldUid = undefined;
var updateButton = function () { var updateButton = function () {
var myData = metadataMgr.getUserData(); var myData = metadataMgr.getUserData();
var privateData = metadataMgr.getPrivateData(); var privateData = metadataMgr.getPrivateData();
var uid = myData.uid;
if (!priv.plan && privateData.plan) { if (!priv.plan && privateData.plan) {
config.$initBlock.empty(); config.$initBlock.empty();
metadataMgr.off('change', updateButton); metadataMgr.off('change', updateButton);
@ -2013,15 +2015,16 @@ define([
var newName = myData.name; var newName = myData.name;
var url = myData.avatar; var url = myData.avatar;
$displayName.text(newName || Messages.anonymous); $displayName.text(newName || Messages.anonymous);
if (accountName && oldUrl !== url) { if ((accountName && oldUrl !== url) || !accountName && uid !== oldUid) {
$avatar.html(''); $avatar.html('');
Common.displayAvatar($avatar, url, Common.displayAvatar($avatar, url,
newName || Messages.anonymous, function ($img) { newName || Messages.anonymous, function ($img) {
oldUrl = url; oldUrl = url;
oldUid = uid;
$userAdmin.find('> button').removeClass('cp-avatar'); $userAdmin.find('> button').removeClass('cp-avatar');
if ($img) { $userAdmin.find('> button').addClass('cp-avatar'); } if ($img) { $userAdmin.find('> button').addClass('cp-avatar'); }
loadingAvatar = false; loadingAvatar = false;
}); }, uid);
return; return;
} }
loadingAvatar = false; loadingAvatar = false;

@ -94,6 +94,21 @@ define([
return ANIMALS[seed % ANIMALS.length]; return ANIMALS[seed % ANIMALS.length];
}; };
var getPrettyInitials = function (name) {
var parts = name.split(/\s+/);
var text;
if (parts.length > 1) {
text = parts.slice(0, 2).map(Util.getFirstCharacter).join('');
} else {
text = Util.getFirstCharacter(name);
var second = Util.getFirstCharacter(name.replace(text, ''));
if (second && second !== '?') {
text += second;
}
}
return text;
};
var animal_avatars = {}; var animal_avatars = {};
MT.displayAvatar = function (common, $container, href, name, _cb, uid) { MT.displayAvatar = function (common, $container, href, name, _cb, uid) {
var cb = Util.once(Util.mkAsync(_cb || function () {})); var cb = Util.once(Util.mkAsync(_cb || function () {}));
@ -105,20 +120,16 @@ define([
var animal = false; var animal = false;
name = (name || "").trim() || Messages.anonymous; name = (name || "").trim() || Messages.anonymous;
var parts = name.split(/\s+/);
var text; var text;
if (name === Messages.anonymous) { if (name === Messages.anonymous && uid) {
if (animal_avatar) { if (animal_avatar) {
text = animal_avatar; text = animal_avatar;
} else { } else {
text = animal_avatar = getPseudorandomAnimal(uid); text = animal_avatar = getPseudorandomAnimal(uid);
} }
animal = true; animal = true;
} else if (parts.length > 1) {
text = parts.slice(0, 2).map(Util.getFirstCharacter).join('');
} else { } else {
text = Util.getFirstCharacter(name); text = getPrettyInitials(name);
text += Util.getFirstCharacter(name.replace(text, ''));
} }
var $avatar = $('<span>', {'class': 'cp-avatar-default' + (animal? ' animal': '')}).text(text); var $avatar = $('<span>', {'class': 'cp-avatar-default' + (animal? ' animal': '')}).text(text);

@ -362,7 +362,7 @@ MessengerUI, Messages, Pages) {
}); });
} }
console.error("AVATAR", $span, data.uid); console.error("AVATAR", $span, data.uid);
Common.displayAvatar($span, data.avatar, name, function () { // XXX pass a little more info so we can display better (pseudo-random) defaults Common.displayAvatar($span, data.avatar, name, function () {
$span.append($rightCol); $span.append($rightCol);
}, data.uid); }, data.uid);
$span.data('uid', data.uid); $span.data('uid', data.uid);

Loading…
Cancel
Save