From b8c847bccef0001553cfc09724f38939dd8cd74c Mon Sep 17 00:00:00 2001 From: ansuz Date: Thu, 19 Aug 2021 22:25:51 +0530 Subject: [PATCH] prototype animal avatars for guests that haven't set a custom name --- www/common/inner/common-mediatag.js | 34 ++++++++++++++++++++++++++--- www/common/toolbar.js | 5 +++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/www/common/inner/common-mediatag.js b/www/common/inner/common-mediatag.js index 03d85fab0..2dc912e02 100644 --- a/www/common/inner/common-mediatag.js +++ b/www/common/inner/common-mediatag.js @@ -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 = $('', {'class': 'cp-avatar-default'}).text(text); + var $avatar = $('', {'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; diff --git a/www/common/toolbar.js b/www/common/toolbar.js index d103fe410..791857049 100644 --- a/www/common/toolbar.js +++ b/www/common/toolbar.js @@ -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); });