A platform for community discussion. Free, open, simple.
FEATURE: Let staff remove an avatar from the review queue (#42724)
Régis Hanol04a39d90ec0d
diff --git a/app/models/reviewable_user.rb b/app/models/reviewable_user.rbindex ec49e34177f..096175c4426 100644--- a/app/models/reviewable_user.rb+++ b/app/models/reviewable_user.rb@@ -31,6 +31,10 @@ class ReviewableUser < Reviewabledelete_user_actions(actions, bundle, require_reject_reason: false)end+ if status == "pending" && target&.uploaded_avatar_id.present?+ build_action(actions, :remove_avatar, icon: "user-xmark")+ end+if guardian.can_approve?(target)actions.add(:approve_user, bundle: nil) do |a|a.icon = "user-plus"@@ -54,6 +58,12 @@ class ReviewableUser < Reviewablesuperend+ def perform_remove_avatar(performed_by, args)+ target.remove_avatar!(performed_by)++ create_result(:success)+ end+def perform_approve_user(performed_by, args)ReviewableUser.set_approved_fields!(target, performed_by)target.save!diff --git a/app/models/user.rb b/app/models/user.rbindex 9bdbba8622b..f491445a66c 100644--- a/app/models/user.rb+++ b/app/models/user.rb@@ -1304,6 +1304,17 @@ class User < ActiveRecord::Baseendend+ def remove_avatar!(actor)+ return if uploaded_avatar_id.blank?++ self.class.transaction do+ update!(uploaded_avatar_id: nil)+ user_avatar&.update!(custom_upload_id: nil, gravatar_upload_id: nil)+ end++ StaffActionLogger.new(actor).log_removed_avatar(self)+ end+# The following count methods are somewhat slow - definitely don't use them in a loop.# They might need to be denormalizeddef like_countdiff --git a/app/models/user_history.rb b/app/models/user_history.rbindex 8049cf46561..f3eea77b9a1 100644--- a/app/models/user_history.rb+++ b/app/models/user_history.rb@@ -173,6 +173,7 @@ class UserHistory < ActiveRecord::Baseadmin_onboarding_step_completed: 128,admin_onboarding_completed: 129,admin_onboarding_dismissed: 130,+ removed_avatar: 131,)end@@ -307,6 +308,7 @@ class UserHistory < ActiveRecord::Baseadmin_onboarding_step_completedadmin_onboarding_completedadmin_onboarding_dismissed+ removed_avatar]enddiff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rbindex 700496704b6..b8beff62a2b 100644--- a/app/services/staff_action_logger.rb+++ b/app/services/staff_action_logger.rb@@ -915,6 +915,13 @@ class StaffActionLogger)end+ def log_removed_avatar(user, opts = {})+ raise Discourse::InvalidParameters.new(:user) unless user+ UserHistory.create!(+ params(opts).merge(action: UserHistory.actions[:removed_avatar], target_user_id: user.id),+ )+ end+def log_user_deactivate(user, reason, opts = {})raise Discourse::InvalidParameters.new(:user) unless userUserHistory.create!(diff --git a/config/locales/client.en.yml b/config/locales/client.en.ymlindex a7c80dd04e9..02adf30e594 100644--- a/config/locales/client.en.yml+++ b/config/locales/client.en.yml@@ -9146,6 +9146,7 @@ en:change_name: "change name"topic_timestamps_changed: "topic timestamps changed"approve_user: "approved user"+ removed_avatar: "removed avatar"web_hook_create: "webhook create"web_hook_update: "webhook update"web_hook_destroy: "webhook destroy"diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 92196135bfc..8e2589d6c4d 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -6300,6 +6300,10 @@ en:reject_post:title: "Reject Post"complete: "Post rejected."+ remove_avatar:+ title: "Remove avatar"+ description: "Reset this user's profile picture to the system default"+ complete: "Avatar removed."approve_user:title: "Yes"complete: "User approved."diff --git a/spec/models/reviewable_user_spec.rb b/spec/models/reviewable_user_spec.rbindex 8b8f32585f2..2d14b33dac5 100644--- a/spec/models/reviewable_user_spec.rb+++ b/spec/models/reviewable_user_spec.rb@@ -193,6 +193,41 @@ RSpec.describe ReviewableUser, type: :model dodescribe "#perform" dofab!(:reviewable)+ context "when removing an avatar" do+ fab!(:avatar, :upload)++ before do+ reviewable.target.update!(uploaded_avatar_id: avatar.id)+ reviewable.target.user_avatar.update!(custom_upload_id: avatar.id)+ end++ it "resets the avatar and leaves the reviewable pending" do+ result = reviewable.perform(moderator, :remove_avatar)++ expect(result.success?).to eq(true)+ expect(reviewable.reload).to be_pending+ expect(reviewable.target.reload.uploaded_avatar_id).to be_nil+ expect(reviewable.target.user_avatar.reload.custom_upload_id).to be_nil+ end++ it "logs a staff action" do+ expect { reviewable.perform(moderator, :remove_avatar) }.to change {+ UserHistory.where(+ action: UserHistory.actions[:removed_avatar],+ target_user_id: reviewable.target_id,+ ).count+ }.by(1)+ end++ it "is offered only while the user still has an avatar" do+ expect(reviewable.actions_for(Guardian.new(moderator)).has?(:remove_avatar)).to eq(true)++ reviewable.target.remove_avatar!(moderator)++ expect(reviewable.actions_for(Guardian.new(moderator)).has?(:remove_avatar)).to eq(false)+ end+ end+context "when approving" doit "allows us to approve a user" doresult = reviewable.perform(moderator, :approve_user)diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rbindex d2cd3cbca98..e381b426393 100644--- a/spec/models/user_spec.rb+++ b/spec/models/user_spec.rb@@ -1970,6 +1970,43 @@ RSpec.describe User doendend+ describe "#remove_avatar!" do+ fab!(:avatar, :upload)+ fab!(:gravatar, :upload)+ fab!(:actor, :admin)++ it "drops the stored uploads so the image cannot be re-selected" do+ user = Fabricate(:user, uploaded_avatar_id: avatar.id)+ user.user_avatar.update!(custom_upload_id: avatar.id, gravatar_upload_id: gravatar.id)++ user.remove_avatar!(actor)++ expect(user.reload.uploaded_avatar_id).to be_nil+ expect(user.user_avatar.reload.custom_upload_id).to be_nil+ expect(user.user_avatar.gravatar_upload_id).to be_nil+ expect(+ UserHistory.exists?(action: UserHistory.actions[:removed_avatar], target_user_id: user.id),+ ).to eq(true)+ end++ it "does nothing when there is no avatar to remove" do+ user = Fabricate(:user)++ expect { user.remove_avatar!(actor) }.not_to change {+ UserHistory.where(action: UserHistory.actions[:removed_avatar]).count+ }+ end++ it "works for a user that never had a user_avatar row" do+ user = Fabricate(:user, uploaded_avatar_id: avatar.id)+ user.user_avatar&.destroy+ user.reload++ expect { user.remove_avatar!(actor) }.not_to raise_error+ expect(user.reload.uploaded_avatar_id).to be_nil+ end+ end+describe "#avatar_template" doit "uses the small logo if the user is the system user" dologo_small_url = Discourse.store.cdn_url(SiteSetting.logo_small.url)