From 9974a26c47d1563be2c09fa9a84e73256466e553 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 3 Mar 2015 16:16:04 -0500 Subject: [PATCH] FEATURE: Can specify a list of `default_avatars` urls. These new avatars will be used by new users until they upload their own avatars. If left blank, letter avatars are used. --- .../discourse/helpers/application_helpers.js | 5 ++- .../discourse/helpers/user-avatar.js.es6 | 4 +-- .../discourse/lib/avatar-template.js.es6 | 34 ++++++++++++++++++ .../discourse/models/{user.js => user.js.es6} | 36 ++++--------------- app/assets/javascripts/main_include.js | 1 + config/site_settings.yml | 1 + .../lib/avatar-template-test.js.es6 | 16 +++++++++ test/javascripts/models/user-test.js.es6 | 12 ------- 8 files changed, 62 insertions(+), 47 deletions(-) create mode 100644 app/assets/javascripts/discourse/lib/avatar-template.js.es6 rename app/assets/javascripts/discourse/models/{user.js => user.js.es6} (95%) create mode 100644 test/javascripts/lib/avatar-template-test.js.es6 diff --git a/app/assets/javascripts/discourse/helpers/application_helpers.js b/app/assets/javascripts/discourse/helpers/application_helpers.js index 2df3135d9b8..9f94b4db32b 100644 --- a/app/assets/javascripts/discourse/helpers/application_helpers.js +++ b/app/assets/javascripts/discourse/helpers/application_helpers.js @@ -2,6 +2,7 @@ var safe = Handlebars.SafeString; // TODO: Remove me when ES6ified var registerUnbound = require('discourse/helpers/register-unbound', null, null, true).default; +var avatarTemplate = require('discourse/lib/avatar-template', null, null, true).default; /** Bound avatar helper. @@ -19,11 +20,9 @@ Em.Handlebars.helper('bound-avatar', function(user, size, uploadId) { uploadId = Em.get(user, 'uploaded_avatar_id'); } - var avatarTemplate = Discourse.User.avatarTemplate(username, uploadId); - return new safe(Discourse.Utilities.avatarImg({ size: size, - avatarTemplate: avatarTemplate + avatarTemplate: avatarTemplate(username, uploadId) })); }, 'username', 'uploaded_avatar_id'); diff --git a/app/assets/javascripts/discourse/helpers/user-avatar.js.es6 b/app/assets/javascripts/discourse/helpers/user-avatar.js.es6 index 230e97f504b..1ab668ffc03 100644 --- a/app/assets/javascripts/discourse/helpers/user-avatar.js.es6 +++ b/app/assets/javascripts/discourse/helpers/user-avatar.js.es6 @@ -1,4 +1,5 @@ import registerUnbound from 'discourse/helpers/register-unbound'; +import avatarTemplate from 'discourse/lib/avatar-template'; function renderAvatar(user, options) { options = options || {}; @@ -28,13 +29,12 @@ function renderAvatar(user, options) { // this is simply done to ensure we cache images correctly var uploadedAvatarId = Em.get(user, 'uploaded_avatar_id') || Em.get(user, 'user.uploaded_avatar_id'); - var avatarTemplate = Discourse.User.avatarTemplate(username,uploadedAvatarId); return Discourse.Utilities.avatarImg({ size: options.imageSize, extraClasses: Em.get(user, 'extras') || options.extraClasses, title: title || username, - avatarTemplate: avatarTemplate + avatarTemplate: avatarTemplate(username, uploadedAvatarId) }); } else { return ''; diff --git a/app/assets/javascripts/discourse/lib/avatar-template.js.es6 b/app/assets/javascripts/discourse/lib/avatar-template.js.es6 new file mode 100644 index 00000000000..28032d2a483 --- /dev/null +++ b/app/assets/javascripts/discourse/lib/avatar-template.js.es6 @@ -0,0 +1,34 @@ +let _splitAvatars; + +function defaultAvatar(username) { + const defaultAvatars = Discourse.SiteSettings.default_avatars; + if (defaultAvatars && defaultAvatars.length) { + _splitAvatars = _splitAvatars || defaultAvatars.split("\n"); + + if (_splitAvatars.length) { + let hash = 0; + for (let i = 0; i