prototype animal avatars for guests that haven't set a custom name

pull/1/head
ansuz 3 years ago
parent 82101bcb9b
commit b8c847bcce

@ -79,21 +79,48 @@ define([
});
};
MT.displayAvatar = function (common, $container, href, name, _cb) {
// https://emojipedia.org/nature/
var ANIMALS = [ '🙈', '🦀', '🐞', '🦋', '🐬', '🐋', '🐢', '🦉', '🦆', '🐧', '🦡', '🦘', '🦨', '🦦', '🦥', '🐼', '🐻', '🦝', '🦄', '🐄', '🐷', '🐐', '🦙', '🦒', '🐘', '🦏', '🐁', '🐹', '🐰', '🦫', '🦔', '🐨'];
var getRandomAnimal = function () {
return ANIMALS[Math.floor(Math.random() * ANIMALS.length)];
};
var getPseudorandomAnimal = function (seed) {
if (typeof(seed) !== 'string') { return getRandomAnimal(); }
seed = seed.replace(/\D/g, '').slice(0, 10);
seed = parseInt(seed);
if (!seed) { return getRandomAnimal(); }
return ANIMALS[seed % ANIMALS.length];
};
MT.displayAvatar = function (common, $container, href, name, _cb, uid) {
var cb = Util.once(Util.mkAsync(_cb || function () {}));
var displayDefault = function () {
if (avatars[uid]) {
var nodes = $.parseHTML(avatars[uid]);
var $el = $(nodes[0]);
$container.append($el);
return void cb($el);
}
var animal = false;
name = (name || "").trim() || Messages.anonymous;
var parts = name.split(/\s+/);
var text;
if (parts.length > 1) {
if (name === Messages.anonymous) {
text = getPseudorandomAnimal(uid);
animal = true;
} else if (parts.length > 1) {
text = parts.slice(0, 2).map(Util.getFirstCharacter).join('');
} else {
text = Util.getFirstCharacter(name);
text += Util.getFirstCharacter(name.replace(text, ''));
}
var $avatar = $('<span>', {'class': 'cp-avatar-default'}).text(text);
var $avatar = $('<span>', {'class': 'cp-avatar-default' + (animal? ' animal': '')}).text(text);
$container.append($avatar);
avatars[uid] = $avatar[0].outerHTML;
if (cb) { cb(); }
};
if (!window.Symbol) { return void displayDefault(); } // IE doesn't have Symbol
@ -106,6 +133,7 @@ define([
return void cb($el);
}
var centerImage = function ($img, $image) {
var img = $image[0];
var w = img.width;

@ -361,9 +361,10 @@ MessengerUI, Messages, Pages) {
Common.openURL(origin+'/profile/#' + data.profile);
});
}
Common.displayAvatar($span, data.avatar, name, function () {
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
$span.append($rightCol);
});
}, data.uid);
$span.data('uid', data.uid);
$editUsersList.append($span);
});

Loading…
Cancel
Save