diff --git a/app/jobs/scheduled/create_missing_avatars.rb b/app/jobs/scheduled/create_missing_avatars.rb index a13dd520222..c5f45823ca2 100644 --- a/app/jobs/scheduled/create_missing_avatars.rb +++ b/app/jobs/scheduled/create_missing_avatars.rb @@ -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 diff --git a/app/models/user_avatar.rb b/app/models/user_avatar.rb index 1034f1469cf..cf3739c7ad5 100644 --- a/app/models/user_avatar.rb +++ b/app/models/user_avatar.rb @@ -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) # diff --git a/db/migrate/20140527233225_add_system_savatar_version_to_user_avatars.rb b/db/migrate/20140527233225_add_system_savatar_version_to_user_avatars.rb new file mode 100644 index 00000000000..9fe87c9a80b --- /dev/null +++ b/db/migrate/20140527233225_add_system_savatar_version_to_user_avatars.rb @@ -0,0 +1,5 @@ +class AddSystemSavatarVersionToUserAvatars < ActiveRecord::Migration + def change + add_column :user_avatars, :system_avatar_version, :integer, default: 0 + end +end diff --git a/lib/letter_avatar.rb b/lib/letter_avatar.rb index 7bf34197523..88861166c6d 100644 --- a/lib/letter_avatar.rb +++ b/lib/letter_avatar.rb @@ -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}' diff --git a/lib/tasks/avatars.rake b/lib/tasks/avatars.rake new file mode 100644 index 00000000000..fdeaba8c857 --- /dev/null +++ b/lib/tasks/avatars.rake @@ -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 diff --git a/spec/models/user_avatar_spec.rb b/spec/models/user_avatar_spec.rb index 93fc9a9cec0..7c3f20ea37b 100644 --- a/spec/models/user_avatar_spec.rb +++ b/spec/models/user_avatar_spec.rb @@ -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