diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index d39697ce0c7..d00bfccd837 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -314,7 +314,7 @@ class UsersController < ApplicationController user.use_uploaded_avatar = true user.save! - Jobs.enqueue(:generate_avatars, upload_id: upload.id) + Jobs.enqueue(:generate_avatars, user_id: user.id, upload_id: upload.id) render json: { url: upload.url, diff --git a/app/jobs/regular/generate_avatars.rb b/app/jobs/regular/generate_avatars.rb index eac7e739a64..a2a23d3b697 100644 --- a/app/jobs/regular/generate_avatars.rb +++ b/app/jobs/regular/generate_avatars.rb @@ -5,8 +5,15 @@ module Jobs class GenerateAvatars < Jobs::Base def execute(args) - upload = Upload.where(id: args[:upload_id]).first - return unless upload.present? + upload_id = args[:upload_id] + raise Discourse::InvalidParameters.new(:upload_id) unless upload_id.present? + + user_id = args[:user_id] + raise Discourse::InvalidParameters.new(:user_id) unless user_id.present? + + upload = Upload.where(id: upload_id).first + user = User.where(id: user_id).first + return unless upload.present? || user.present? external_copy = Discourse.store.download(upload) if Discourse.store.external? original_path = if Discourse.store.external? @@ -37,7 +44,7 @@ module Jobs # make sure we remove the cached copy from external stores external_copy.close! if Discourse.store.external? - user = User.where(id: upload.user_id).first + # attach the avatar to the user user.uploaded_avatar_template = Discourse.store.absolute_avatar_template(upload) user.save! diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index e308c2bbd52..c13ad5e0761 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -970,7 +970,7 @@ describe UsersController do upload = Fabricate(:upload) Upload.expects(:create_for).returns(upload) # enqueues the avatar generator job - Jobs.expects(:enqueue).with(:generate_avatars, { upload_id: upload.id }) + Jobs.expects(:enqueue).with(:generate_avatars, { user_id: user.id, upload_id: upload.id }) xhr :post, :upload_avatar, username: user.username, file: avatar user.reload # erase the previous template