mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-12-17 19:12:37 -05:00
FEATURE: ability to regenerate system avatars as needed
BUGFIX: made system letter avatar more thinner for less block look
This commit is contained in:
parent
b1d5f4440b
commit
2791852bd8
6 changed files with 73 additions and 10 deletions
|
@ -2,14 +2,16 @@ module Jobs
|
||||||
class CreateMissingAvatars < Jobs::Scheduled
|
class CreateMissingAvatars < Jobs::Scheduled
|
||||||
every 1.hour
|
every 1.hour
|
||||||
def execute(args)
|
def execute(args)
|
||||||
UserAvatar.where(system_upload_id: nil).find_each do |a|
|
UserAvatar
|
||||||
|
.where("system_upload_id IS NULL OR system_avatar_version != ?", UserAvatar::SYSTEM_AVATAR_VERSION)
|
||||||
|
.find_each do |a|
|
||||||
a.update_system_avatar!
|
a.update_system_avatar!
|
||||||
end
|
end
|
||||||
|
|
||||||
# backfill in batches 1000 an hour
|
# backfill in batches 5000 an hour
|
||||||
User.where(uploaded_avatar_id: nil)
|
User.where(uploaded_avatar_id: nil)
|
||||||
.order("last_posted_at desc")
|
.order("last_posted_at desc")
|
||||||
.limit(1000).each do |u|
|
.limit(5000).each do |u|
|
||||||
u.refresh_avatar
|
u.refresh_avatar
|
||||||
u.save
|
u.save
|
||||||
end
|
end
|
||||||
|
|
|
@ -2,6 +2,7 @@ require_dependency 'letter_avatar'
|
||||||
|
|
||||||
class UserAvatar < ActiveRecord::Base
|
class UserAvatar < ActiveRecord::Base
|
||||||
MAX_SIZE = 240
|
MAX_SIZE = 240
|
||||||
|
SYSTEM_AVATAR_VERSION = 1
|
||||||
|
|
||||||
belongs_to :user
|
belongs_to :user
|
||||||
belongs_to :system_upload, class_name: 'Upload', dependent: :destroy
|
belongs_to :system_upload, class_name: 'Upload', dependent: :destroy
|
||||||
|
@ -14,9 +15,21 @@ class UserAvatar < ActiveRecord::Base
|
||||||
|
|
||||||
# updates the letter based avatar
|
# updates the letter based avatar
|
||||||
def update_system_avatar!
|
def update_system_avatar!
|
||||||
system_upload.destroy! if system_upload
|
old_id = nil
|
||||||
file = File.open(LetterAvatar.generate(user.username, MAX_SIZE), "r")
|
if system_upload
|
||||||
|
old_id = system_upload_id
|
||||||
|
system_upload.destroy!
|
||||||
|
end
|
||||||
|
|
||||||
|
file = File.open(LetterAvatar.generate(user.username, MAX_SIZE, cache: false), "r")
|
||||||
self.system_upload = Upload.create_for(user_id, file, "avatar.png", file.size)
|
self.system_upload = Upload.create_for(user_id, file, "avatar.png", file.size)
|
||||||
|
self.system_avatar_version = SYSTEM_AVATAR_VERSION
|
||||||
|
|
||||||
|
if old_id == user.uploaded_avatar_id
|
||||||
|
user.uploaded_avatar_id = system_upload_id
|
||||||
|
user.save!
|
||||||
|
end
|
||||||
|
|
||||||
save!
|
save!
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -57,4 +70,9 @@ end
|
||||||
# last_gravatar_download_attempt :datetime
|
# last_gravatar_download_attempt :datetime
|
||||||
# created_at :datetime
|
# created_at :datetime
|
||||||
# updated_at :datetime
|
# updated_at :datetime
|
||||||
|
# system_avatar_version :integer default(0)
|
||||||
|
#
|
||||||
|
# Indexes
|
||||||
|
#
|
||||||
|
# index_user_avatars_on_user_id (user_id)
|
||||||
#
|
#
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
class AddSystemSavatarVersionToUserAvatars < ActiveRecord::Migration
|
||||||
|
def change
|
||||||
|
add_column :user_avatars, :system_avatar_version, :integer, default: 0
|
||||||
|
end
|
||||||
|
end
|
|
@ -7,17 +7,20 @@ class LetterAvatar
|
||||||
'tmp/letter_avatars'
|
'tmp/letter_avatars'
|
||||||
end
|
end
|
||||||
|
|
||||||
def generate(username, size)
|
def generate(username, size, opts = nil)
|
||||||
|
|
||||||
|
cache = true
|
||||||
|
cache = false if opts && opts[:cache] == false
|
||||||
|
|
||||||
size = FULLSIZE if size > FULLSIZE
|
size = FULLSIZE if size > FULLSIZE
|
||||||
filename = cached_path(username, size)
|
filename = cached_path(username, size)
|
||||||
|
|
||||||
if File.exists?(filename)
|
if cache && File.exists?(filename)
|
||||||
return filename
|
return filename
|
||||||
end
|
end
|
||||||
|
|
||||||
fullsize = fullsize_path(username)
|
fullsize = fullsize_path(username)
|
||||||
if !File.exists?(fullsize)
|
if !cache || !File.exists?(fullsize)
|
||||||
generate_fullsize(username)
|
generate_fullsize(username)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -49,10 +52,10 @@ class LetterAvatar
|
||||||
instructions = %W{
|
instructions = %W{
|
||||||
-size 240x240
|
-size 240x240
|
||||||
xc:#{to_rgb(color)}
|
xc:#{to_rgb(color)}
|
||||||
-pointsize 240
|
-pointsize 200
|
||||||
-fill white
|
-fill white
|
||||||
-gravity Center
|
-gravity Center
|
||||||
-font 'Helvetica-Bold'
|
-font 'Helvetica'
|
||||||
-stroke #{to_rgb(stroke)}
|
-stroke #{to_rgb(stroke)}
|
||||||
-strokewidth 2
|
-strokewidth 2
|
||||||
-annotate -5+25 '#{username[0].upcase}'
|
-annotate -5+25 '#{username[0].upcase}'
|
||||||
|
|
24
lib/tasks/avatars.rake
Normal file
24
lib/tasks/avatars.rake
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
desc "Rebuild all system avatars"
|
||||||
|
task "avatars:rebuild_system" => :environment do
|
||||||
|
i = 0
|
||||||
|
puts "Regenerating system avatars"
|
||||||
|
puts
|
||||||
|
UserAvatar.find_each do |a|
|
||||||
|
a.update_system_avatar!
|
||||||
|
putc "." if (i+=1)%10 == 0
|
||||||
|
end
|
||||||
|
puts
|
||||||
|
end
|
||||||
|
|
||||||
|
desc "Refresh all avatars (download missing gravatars, refresh system)"
|
||||||
|
task "avatars:refresh" => :environment do
|
||||||
|
i = 0
|
||||||
|
puts "Refreshing avatars"
|
||||||
|
puts
|
||||||
|
User.find_each do |user|
|
||||||
|
user.refresh_avatar
|
||||||
|
user.user_avatar.update_system_avatar!
|
||||||
|
putc "." if (i+=1)%10 == 0
|
||||||
|
end
|
||||||
|
puts
|
||||||
|
end
|
|
@ -11,6 +11,17 @@ describe UserAvatar do
|
||||||
avatar.system_upload.should_not be_nil
|
avatar.system_upload.should_not be_nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'corrects old system avatars on refresh' do
|
||||||
|
upload = Fabricate(:upload)
|
||||||
|
user = Fabricate(:user, uploaded_avatar_id: upload.id)
|
||||||
|
avatar = UserAvatar.create!(user_id: user.id, system_upload_id: upload.id)
|
||||||
|
avatar.update_system_avatar!
|
||||||
|
|
||||||
|
user.reload
|
||||||
|
user.uploaded_avatar_id.should_not == upload.id
|
||||||
|
avatar.system_upload_id.should == user.uploaded_avatar_id
|
||||||
|
end
|
||||||
|
|
||||||
it 'can update gravatars' do
|
it 'can update gravatars' do
|
||||||
temp = Tempfile.new('test')
|
temp = Tempfile.new('test')
|
||||||
temp.binmode
|
temp.binmode
|
||||||
|
|
Loading…
Reference in a new issue