FIX: always update 'last_gravatar_download_attempt' when updating gravatar

This commit is contained in:
Régis Hanol 2018-10-18 11:02:54 +02:00
parent 53aa0344bf
commit 3973823a33
2 changed files with 62 additions and 49 deletions

View File

@ -13,12 +13,10 @@ class UserAvatar < ActiveRecord::Base
def update_gravatar! def update_gravatar!
DistributedMutex.synchronize("update_gravatar_#{user_id}") do DistributedMutex.synchronize("update_gravatar_#{user_id}") do
begin begin
# special logic for our system user self.update_columns(last_gravatar_download_attempt: Time.now)
email_hash = user_id == Discourse::SYSTEM_USER_ID ? User.email_hash("info@discourse.org") : user.email_hash
self.last_gravatar_download_attempt = Time.new
max = Discourse.avatar_sizes.max max = Discourse.avatar_sizes.max
email_hash = user_id == Discourse::SYSTEM_USER_ID ? User.email_hash("info@discourse.org") : user.email_hash
gravatar_url = "https://www.gravatar.com/avatar/#{email_hash}.png?s=#{max}&d=404" gravatar_url = "https://www.gravatar.com/avatar/#{email_hash}.png?s=#{max}&d=404"
# follow redirects in case gravatar change rules on us # follow redirects in case gravatar change rules on us
@ -42,12 +40,10 @@ class UserAvatar < ActiveRecord::Base
type: "avatar" type: "avatar"
).create_for(user_id) ).create_for(user_id)
upload_id = upload.id if gravatar_upload_id != upload.id
if gravatar_upload_id != upload_id
User.transaction do User.transaction do
if gravatar_upload_id && user.uploaded_avatar_id == gravatar_upload_id if gravatar_upload_id && user.uploaded_avatar_id == gravatar_upload_id
user.update!(uploaded_avatar_id: upload_id) user.update!(uploaded_avatar_id: upload.id)
end end
gravatar_upload&.destroy! gravatar_upload&.destroy!

View File

@ -8,6 +8,8 @@ describe UserAvatar do
let(:temp) { Tempfile.new('test') } let(:temp) { Tempfile.new('test') }
let(:upload) { Fabricate(:upload, user: user) } let(:upload) { Fabricate(:upload, user: user) }
describe "when working" do
before do before do
temp.binmode temp.binmode
# tiny valid png # tiny valid png
@ -21,13 +23,12 @@ describe UserAvatar do
end end
it 'can update gravatars' do it 'can update gravatars' do
expect do freeze_time Time.now
avatar.update_gravatar!
end.to change { Upload.count }.by(1)
upload = Upload.last expect { avatar.update_gravatar! }.to change { Upload.count }.by(1)
expect(avatar.gravatar_upload).to eq(upload) expect(avatar.gravatar_upload).to eq(Upload.last)
expect(avatar.last_gravatar_download_attempt).to eq(Time.now)
expect(user.reload.uploaded_avatar).to eq(nil) expect(user.reload.uploaded_avatar).to eq(nil)
end end
@ -66,6 +67,22 @@ describe UserAvatar do
end end
end end
describe "when failing" do
it "always update 'last_gravatar_download_attempt'" do
freeze_time Time.now
FileHelper.expects(:download).raises(SocketError)
expect { avatar.update_gravatar! }.to_not change { Upload.count }
expect(avatar.last_gravatar_download_attempt).to eq(Time.now)
end
end
end
context '.import_url_for_user' do context '.import_url_for_user' do
it 'creates user_avatar record if missing' do it 'creates user_avatar record if missing' do