diff --git a/app/controllers/admin/users_controller.rb b/app/controllers/admin/users_controller.rb index 13aa89d3ca5..d948d5f5f09 100644 --- a/app/controllers/admin/users_controller.rb +++ b/app/controllers/admin/users_controller.rb @@ -289,7 +289,8 @@ class Admin::UsersController < Admin::AdminController silenced_till: params[:silenced_till], reason: params[:reason], message_body: message, - keep_posts: true + keep_posts: true, + post_id: params[:post_id] ) if silencer.silence && message.present? Jobs.enqueue( diff --git a/app/services/staff_action_logger.rb b/app/services/staff_action_logger.rb index 6261e655809..b5a64c3f2f2 100644 --- a/app/services/staff_action_logger.rb +++ b/app/services/staff_action_logger.rb @@ -289,13 +289,14 @@ class StaffActionLogger def log_silence_user(user, opts = {}) raise Discourse::InvalidParameters.new(:user) unless user - UserHistory.create( - params(opts).merge( - action: UserHistory.actions[:silence_user], - target_user_id: user.id, - details: opts[:details] - ) + create_args = params(opts).merge( + action: UserHistory.actions[:silence_user], + target_user_id: user.id, + details: opts[:details] ) + create_args[:post_id] = opts[:post_id] if opts[:post_id] + + UserHistory.create(create_args) end def log_unsilence_user(user, opts = {}) diff --git a/app/services/user_silencer.rb b/app/services/user_silencer.rb index 5db360637b8..a9fff88dd0c 100644 --- a/app/services/user_silencer.rb +++ b/app/services/user_silencer.rb @@ -33,10 +33,12 @@ class UserSilencer SystemMessage.create(@user, message_type) if @by_user + log_params = { context: context, details: details } + log_params[:post_id] = @opts[:post_id].to_i if @opts[:post_id] + @user_history = StaffActionLogger.new(@by_user).log_silence_user( @user, - context: context, - details: details + log_params ) end diff --git a/spec/controllers/admin/users_controller_spec.rb b/spec/controllers/admin/users_controller_spec.rb index e56fa93b3e6..1936233f701 100644 --- a/spec/controllers/admin/users_controller_spec.rb +++ b/spec/controllers/admin/users_controller_spec.rb @@ -615,6 +615,31 @@ describe Admin::UsersController do expect(@reg_user).to be_silenced end + it "can have an associated post" do + silence_post = Fabricate(:post, user: @reg_user) + + put :silence, params: { + user_id: @reg_user.id, + post_id: silence_post.id, + post_action: 'edit', + post_edit: "this is the new contents for the post" + }, format: :json + expect(response).to be_success + + silence_post.reload + expect(silence_post.raw).to eq("this is the new contents for the post") + + log = UserHistory.where( + target_user_id: @reg_user.id, + action: UserHistory.actions[:silence_user] + ).first + expect(log).to be_present + expect(log.post_id).to eq(silence_post.id) + + @reg_user.reload + expect(@reg_user).to be_silenced + end + it "will set a length of time if provided" do future_date = 1.month.from_now.to_date put( diff --git a/spec/services/user_silencer_spec.rb b/spec/services/user_silencer_spec.rb index 188bd0435fd..73bab2e19ff 100644 --- a/spec/services/user_silencer_spec.rb +++ b/spec/services/user_silencer_spec.rb @@ -60,7 +60,7 @@ describe UserSilencer do end it "logs it with context" do - SystemMessage.stubs(:create).returns(Fabricate.build(:post)) + SystemMessage.stubs(:create) expect { UserSilencer.silence(user, Fabricate(:admin)) }.to change { UserHistory.count }.by(1)