mirror of
https://github.com/discourse/discourse.git
synced 2025-01-30 17:08:30 +08:00
FIX: always update 'last_gravatar_download_attempt' when updating gravatar
This commit is contained in:
parent
53aa0344bf
commit
3973823a33
|
@ -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!
|
||||||
|
|
|
@ -8,62 +8,79 @@ 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) }
|
||||||
|
|
||||||
before do
|
describe "when working" do
|
||||||
temp.binmode
|
|
||||||
# tiny valid png
|
|
||||||
temp.write(Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw=="))
|
|
||||||
temp.rewind
|
|
||||||
FileHelper.expects(:download).returns(temp)
|
|
||||||
end
|
|
||||||
|
|
||||||
after do
|
before do
|
||||||
temp.unlink
|
temp.binmode
|
||||||
end
|
# tiny valid png
|
||||||
|
temp.write(Base64.decode64("R0lGODlhAQABALMAAAAAAIAAAACAAICAAAAAgIAAgACAgMDAwICAgP8AAAD/AP//AAAA//8A/wD//wBiZCH5BAEAAA8ALAAAAAABAAEAAAQC8EUAOw=="))
|
||||||
|
temp.rewind
|
||||||
|
FileHelper.expects(:download).returns(temp)
|
||||||
|
end
|
||||||
|
|
||||||
it 'can update gravatars' do
|
after do
|
||||||
expect do
|
temp.unlink
|
||||||
avatar.update_gravatar!
|
end
|
||||||
end.to change { Upload.count }.by(1)
|
|
||||||
|
|
||||||
upload = Upload.last
|
it 'can update gravatars' do
|
||||||
|
freeze_time Time.now
|
||||||
|
|
||||||
expect(avatar.gravatar_upload).to eq(upload)
|
expect { avatar.update_gravatar! }.to change { Upload.count }.by(1)
|
||||||
expect(user.reload.uploaded_avatar).to eq(nil)
|
|
||||||
end
|
|
||||||
|
|
||||||
describe 'when user has an existing custom upload' do
|
|
||||||
it "should not change the user's uploaded avatar" do
|
|
||||||
user.update!(uploaded_avatar: upload)
|
|
||||||
|
|
||||||
avatar.update!(
|
|
||||||
custom_upload: upload,
|
|
||||||
gravatar_upload: Fabricate(:upload, user: user)
|
|
||||||
)
|
|
||||||
|
|
||||||
avatar.update_gravatar!
|
|
||||||
|
|
||||||
expect(upload.reload).to eq(upload)
|
|
||||||
expect(user.reload.uploaded_avatar).to eq(upload)
|
|
||||||
expect(avatar.reload.custom_upload).to eq(upload)
|
|
||||||
expect(avatar.gravatar_upload).to eq(Upload.last)
|
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)
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when user has an existing custom upload' do
|
||||||
|
it "should not change the user's uploaded avatar" do
|
||||||
|
user.update!(uploaded_avatar: upload)
|
||||||
|
|
||||||
|
avatar.update!(
|
||||||
|
custom_upload: upload,
|
||||||
|
gravatar_upload: Fabricate(:upload, user: user)
|
||||||
|
)
|
||||||
|
|
||||||
|
avatar.update_gravatar!
|
||||||
|
|
||||||
|
expect(upload.reload).to eq(upload)
|
||||||
|
expect(user.reload.uploaded_avatar).to eq(upload)
|
||||||
|
expect(avatar.reload.custom_upload).to eq(upload)
|
||||||
|
expect(avatar.gravatar_upload).to eq(Upload.last)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe 'when user has an existing gravatar' do
|
||||||
|
it "should update the user's uploaded avatar correctly" do
|
||||||
|
user.update!(uploaded_avatar: upload)
|
||||||
|
avatar.update!(gravatar_upload: upload)
|
||||||
|
|
||||||
|
avatar.update_gravatar!
|
||||||
|
|
||||||
|
expect(Upload.find_by(id: upload.id)).to eq(nil)
|
||||||
|
|
||||||
|
new_upload = Upload.last
|
||||||
|
|
||||||
|
expect(user.reload.uploaded_avatar).to eq(new_upload)
|
||||||
|
expect(avatar.reload.gravatar_upload).to eq(new_upload)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'when user has an existing gravatar' do
|
describe "when failing" do
|
||||||
it "should update the user's uploaded avatar correctly" do
|
|
||||||
user.update!(uploaded_avatar: upload)
|
|
||||||
avatar.update!(gravatar_upload: upload)
|
|
||||||
|
|
||||||
avatar.update_gravatar!
|
it "always update 'last_gravatar_download_attempt'" do
|
||||||
|
freeze_time Time.now
|
||||||
|
|
||||||
expect(Upload.find_by(id: upload.id)).to eq(nil)
|
FileHelper.expects(:download).raises(SocketError)
|
||||||
|
|
||||||
new_upload = Upload.last
|
expect { avatar.update_gravatar! }.to_not change { Upload.count }
|
||||||
|
|
||||||
expect(user.reload.uploaded_avatar).to eq(new_upload)
|
expect(avatar.last_gravatar_download_attempt).to eq(Time.now)
|
||||||
expect(avatar.reload.gravatar_upload).to eq(new_upload)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
context '.import_url_for_user' do
|
context '.import_url_for_user' do
|
||||||
|
|
Loading…
Reference in New Issue
Block a user