BUGFIX: pick gravatar if it was just downloaded

BUGFIX: don't go rebaking unless all avatars are downloaded
This commit is contained in:
Sam 2014-05-28 16:54:21 +10:00
parent f6753d3d46
commit d9f51961c4
4 changed files with 42 additions and 9 deletions

View File

@ -27,8 +27,13 @@ module Jobs
# Automatically close stuff that we missed
Topic.auto_close
# Forces rebake of old posts where needed
Post.rebake_old(250)
# Forces rebake of old posts where needed, as long as no system avatars need updating
unless UserAvatar
.where("last_gravatar_download_attempt IS NULL OR system_upload_id IS NULL OR system_avatar_version != ?", UserAvatar::SYSTEM_AVATAR_VERSION)
.limit(1)
.first
Post.rebake_old(250)
end
end
end

View File

@ -619,21 +619,26 @@ class User < ActiveRecord::Base
end
def refresh_avatar
avatar = user_avatar || UserAvatar.create!(user_id: id)
avatar = user_avatar || create_user_avatar
gravatar_downloaded = false
if SiteSetting.automatically_download_gravatars?
avatar.update_gravatar! unless avatar.last_gravatar_download_attempt
if SiteSetting.automatically_download_gravatars? && !avatar.last_gravatar_download_attempt
avatar.update_gravatar!
gravatar_downloaded = avatar.gravatar_upload_id
end
if SiteSetting.enable_system_avatars?
avatar.update_system_avatar! if !avatar.system_upload_id || username_changed?
avatar.update_system_avatar! if !avatar.system_upload_id ||
username_changed? ||
avatar.system_avatar_version != UserAvatar::SYSTEM_AVATAR_VERSION
end
desired_avatar_id = avatar.gravatar_upload_id || avatar.system_upload_id
if !self.uploaded_avatar_id && desired_avatar_id
if (!self.uploaded_avatar_id || gravatar_downloaded) && desired_avatar_id
self.update_column(:uploaded_avatar_id, desired_avatar_id)
end
end
protected

View File

@ -26,8 +26,7 @@ class UserAvatar < ActiveRecord::Base
self.system_avatar_version = SYSTEM_AVATAR_VERSION
if old_id == user.uploaded_avatar_id
user.uploaded_avatar_id = system_upload_id
user.save!
user.update_column(:uploaded_avatar_id, system_upload_id)
end
save!

View File

@ -1169,4 +1169,28 @@ describe User do
end
end
describe "refresh_avatar" do
it "picks gravatar if system avatar is picked and gravatar was just downloaded" do
png = Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw==")
FakeWeb.register_uri( :get,
"http://www.gravatar.com/avatar/d10ca8d11301c2f4993ac2279ce4b930.png?s=500&d=404",
body: png )
user = User.create!(username: "bob", name: "bob", email: "a@a.com")
user.create_user_avatar
user.user_avatar.update_system_avatar!
user.save
user.reload
SiteSetting.automatically_download_gravatars = true
SiteSetting.enable_system_avatars = true
user.refresh_avatar
user.reload
user.user_avatar.gravatar_upload_id.should == user.uploaded_avatar_id
end
end
end