mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-23 15:48:43 -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
|
||||
every 1.hour
|
||||
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!
|
||||
end
|
||||
|
||||
# backfill in batches 1000 an hour
|
||||
# backfill in batches 5000 an hour
|
||||
User.where(uploaded_avatar_id: nil)
|
||||
.order("last_posted_at desc")
|
||||
.limit(1000).each do |u|
|
||||
.limit(5000).each do |u|
|
||||
u.refresh_avatar
|
||||
u.save
|
||||
end
|
||||
|
|
|
@ -2,6 +2,7 @@ require_dependency 'letter_avatar'
|
|||
|
||||
class UserAvatar < ActiveRecord::Base
|
||||
MAX_SIZE = 240
|
||||
SYSTEM_AVATAR_VERSION = 1
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :system_upload, class_name: 'Upload', dependent: :destroy
|
||||
|
@ -14,9 +15,21 @@ class UserAvatar < ActiveRecord::Base
|
|||
|
||||
# updates the letter based avatar
|
||||
def update_system_avatar!
|
||||
system_upload.destroy! if system_upload
|
||||
file = File.open(LetterAvatar.generate(user.username, MAX_SIZE), "r")
|
||||
old_id = nil
|
||||
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_avatar_version = SYSTEM_AVATAR_VERSION
|
||||
|
||||
if old_id == user.uploaded_avatar_id
|
||||
user.uploaded_avatar_id = system_upload_id
|
||||
user.save!
|
||||
end
|
||||
|
||||
save!
|
||||
end
|
||||
|
||||
|
@ -57,4 +70,9 @@ end
|
|||
# last_gravatar_download_attempt :datetime
|
||||
# created_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'
|
||||
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
|
||||
filename = cached_path(username, size)
|
||||
|
||||
if File.exists?(filename)
|
||||
if cache && File.exists?(filename)
|
||||
return filename
|
||||
end
|
||||
|
||||
fullsize = fullsize_path(username)
|
||||
if !File.exists?(fullsize)
|
||||
if !cache || !File.exists?(fullsize)
|
||||
generate_fullsize(username)
|
||||
end
|
||||
|
||||
|
@ -49,10 +52,10 @@ class LetterAvatar
|
|||
instructions = %W{
|
||||
-size 240x240
|
||||
xc:#{to_rgb(color)}
|
||||
-pointsize 240
|
||||
-pointsize 200
|
||||
-fill white
|
||||
-gravity Center
|
||||
-font 'Helvetica-Bold'
|
||||
-font 'Helvetica'
|
||||
-stroke #{to_rgb(stroke)}
|
||||
-strokewidth 2
|
||||
-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
|
||||
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
|
||||
temp = Tempfile.new('test')
|
||||
temp.binmode
|
||||
|
|
Loading…
Reference in a new issue