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.
This commit is contained in:
Robin Ward 2015-03-03 16:16:04 -05:00
parent 84b84a9d7c
commit 9974a26c47
8 changed files with 62 additions and 47 deletions

View File

@ -2,6 +2,7 @@ var safe = Handlebars.SafeString;
// TODO: Remove me when ES6ified // TODO: Remove me when ES6ified
var registerUnbound = require('discourse/helpers/register-unbound', null, null, true).default; 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. Bound avatar helper.
@ -19,11 +20,9 @@ Em.Handlebars.helper('bound-avatar', function(user, size, uploadId) {
uploadId = Em.get(user, 'uploaded_avatar_id'); uploadId = Em.get(user, 'uploaded_avatar_id');
} }
var avatarTemplate = Discourse.User.avatarTemplate(username, uploadId);
return new safe(Discourse.Utilities.avatarImg({ return new safe(Discourse.Utilities.avatarImg({
size: size, size: size,
avatarTemplate: avatarTemplate avatarTemplate: avatarTemplate(username, uploadId)
})); }));
}, 'username', 'uploaded_avatar_id'); }, 'username', 'uploaded_avatar_id');

View File

@ -1,4 +1,5 @@
import registerUnbound from 'discourse/helpers/register-unbound'; import registerUnbound from 'discourse/helpers/register-unbound';
import avatarTemplate from 'discourse/lib/avatar-template';
function renderAvatar(user, options) { function renderAvatar(user, options) {
options = options || {}; options = options || {};
@ -28,13 +29,12 @@ function renderAvatar(user, options) {
// this is simply done to ensure we cache images correctly // 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 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({ return Discourse.Utilities.avatarImg({
size: options.imageSize, size: options.imageSize,
extraClasses: Em.get(user, 'extras') || options.extraClasses, extraClasses: Em.get(user, 'extras') || options.extraClasses,
title: title || username, title: title || username,
avatarTemplate: avatarTemplate avatarTemplate: avatarTemplate(username, uploadedAvatarId)
}); });
} else { } else {
return ''; return '';

View File

@ -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<username.length; i++) {
hash = ((hash<<5)-hash) + username.charCodeAt(i);
hash |= 0;
}
return _splitAvatars[Math.abs(hash) % _splitAvatars.length];
}
}
return Discourse.getURLWithCDN("/letter_avatar/" +
username.toLowerCase() +
"/{size}/" +
Discourse.LetterAvatarVersion + ".png");
}
export default function(username, uploadedAvatarId) {
if (uploadedAvatarId) {
return Discourse.getURLWithCDN("/user_avatar/" +
Discourse.BaseUrl +
"/" +
username.toLowerCase() +
"/{size}/" +
uploadedAvatarId + ".png");
}
return defaultAvatar(username);
}

View File

@ -1,12 +1,6 @@
/** import avatarTemplate from 'discourse/lib/avatar-template';
A data model representing a user on Discourse
@class User const User = Discourse.Model.extend({
@extends Discourse.Model
@namespace Discourse
@module Discourse
**/
Discourse.User = Discourse.Model.extend({
hasPMs: Em.computed.gt("private_messages_stats.all", 0), hasPMs: Em.computed.gt("private_messages_stats.all", 0),
hasStartedPMs: Em.computed.gt("private_messages_stats.mine", 0), hasStartedPMs: Em.computed.gt("private_messages_stats.mine", 0),
@ -344,7 +338,7 @@ Discourse.User = Discourse.Model.extend({
}, },
avatarTemplate: function() { avatarTemplate: function() {
return Discourse.User.avatarTemplate(this.get('username'), this.get('uploaded_avatar_id')); return avatarTemplate(this.get('username'), this.get('uploaded_avatar_id'));
}.property('uploaded_avatar_id', 'username'), }.property('uploaded_avatar_id', 'username'),
/* /*
@ -440,27 +434,7 @@ Discourse.User = Discourse.Model.extend({
}); });
Discourse.User.reopenClass(Discourse.Singleton, { User.reopenClass(Discourse.Singleton, {
avatarTemplate: function(username, uploadedAvatarId) {
var url;
if (uploadedAvatarId) {
url = "/user_avatar/" +
Discourse.BaseUrl +
"/" +
username.toLowerCase() +
"/{size}/" +
uploadedAvatarId + ".png";
} else {
url = "/letter_avatar/" +
username.toLowerCase() +
"/{size}/" +
Discourse.LetterAvatarVersion + ".png";
}
return Discourse.getURLWithCDN(url);
},
/** /**
Find a `Discourse.User` for a given username. Find a `Discourse.User` for a given username.
@ -565,3 +539,5 @@ Discourse.User.reopenClass(Discourse.Singleton, {
}); });
} }
}); });
export default User;

View File

@ -25,6 +25,7 @@
//= require ./discourse/lib/autocomplete //= require ./discourse/lib/autocomplete
//= require ./discourse/lib/after-transition //= require ./discourse/lib/after-transition
//= require ./discourse/lib/debounce //= require ./discourse/lib/debounce
//= require ./discourse/lib/avatar-template
//= require_tree ./discourse/adapters //= require_tree ./discourse/adapters
//= require ./discourse/models/model //= require ./discourse/models/model
//= require ./discourse/models/user_action //= require ./discourse/models/user_action

View File

@ -519,6 +519,7 @@ avatars:
default_avatars: default_avatars:
default: '' default: ''
type: url_list type: url_list
client: true
trust: trust:
default_trust_level: default_trust_level:

View File

@ -0,0 +1,16 @@
import avatarTemplate from 'discourse/lib/avatar-template';
module('avatarTemplate');
test("avatarTemplate", function(){
var oldCDN = Discourse.CDN;
var oldBase = Discourse.BaseUrl;
Discourse.BaseUrl = "frogs.com";
equal(avatarTemplate("sam", 1), "/user_avatar/frogs.com/sam/{size}/1.png");
Discourse.CDN = "http://awesome.cdn.com";
equal(avatarTemplate("sam", 1), "http://awesome.cdn.com/user_avatar/frogs.com/sam/{size}/1.png");
Discourse.CDN = oldCDN;
Discourse.BaseUrl = oldBase;
});

View File

@ -25,15 +25,3 @@ test("isAllowedToUploadAFile", function() {
user.setProperties({ admin: false, moderator: true }); user.setProperties({ admin: false, moderator: true });
ok(user.isAllowedToUploadAFile("image"), "moderator can always upload a file"); ok(user.isAllowedToUploadAFile("image"), "moderator can always upload a file");
}); });
test("avatarTemplate", function(){
var oldCDN = Discourse.CDN;
var oldBase = Discourse.BaseUrl;
Discourse.BaseUrl = "frogs.com";
equal(Discourse.User.avatarTemplate("sam", 1), "/user_avatar/frogs.com/sam/{size}/1.png");
Discourse.CDN = "http://awesome.cdn.com";
equal(Discourse.User.avatarTemplate("sam", 1), "http://awesome.cdn.com/user_avatar/frogs.com/sam/{size}/1.png");
Discourse.CDN = oldCDN;
Discourse.BaseUrl = oldBase;
});