FEATURE: add staff action log for post rejections

This commit is contained in:
Arpit Jalan 2018-06-01 19:42:31 +05:30
parent 41999b9949
commit 89eca87f16
6 changed files with 47 additions and 0 deletions

View File

@ -47,6 +47,7 @@ class QueuedPost < ActiveRecord::Base
def reject!(rejected_by)
change_to!(:rejected, rejected_by)
StaffActionLogger.new(rejected_by).log_post_rejected(self)
DiscourseEvent.trigger(:rejected_post, self)
end

View File

@ -80,6 +80,7 @@ class UserHistory < ActiveRecord::Base
removed_suspend_user: 61,
removed_unsilence_user: 62,
removed_unsuspend_user: 63,
post_rejected: 64
)
end
@ -138,6 +139,7 @@ class UserHistory < ActiveRecord::Base
:create_badge,
:change_badge,
:delete_badge,
:post_rejected
]
end

View File

@ -526,6 +526,28 @@ class StaffActionLogger
))
end
def log_post_rejected(rejected_post, opts = {})
raise Discourse::InvalidParameters.new(:rejected_post) unless rejected_post && rejected_post.is_a?(QueuedPost)
topic = rejected_post.topic || Topic.with_deleted.find_by(id: rejected_post.topic_id)
topic_title = topic&.title || "not found"
username = rejected_post.user&.username || "unknown"
name = rejected_post.user&.name || "unknown"
details = [
"created_at: #{rejected_post.created_at}",
"rejected_at: #{rejected_post.rejected_at}",
"user: #{username} (#{name})",
"topic: #{topic_title}",
"raw: #{rejected_post.raw}",
]
UserHistory.create!(params(opts).merge(
action: UserHistory.actions[:post_rejected],
details: details.join("\n")
))
end
private
def params(opts = nil)

View File

@ -3406,6 +3406,7 @@ en:
disabled_second_factor: "disable Two Factor Authentication"
topic_published: "topic published"
post_approved: "post approved"
post_rejected: "post rejected"
create_badge: "create badge"
change_badge: "change badge"
delete_badge: "delete badge"

View File

@ -86,6 +86,9 @@ describe QueuedPost do
# It removes the pending action
expect(UserAction.where(queued_post_id: qp.id).count).to eq(0)
# Logs staff action log for rejected post
expect(UserHistory.where(action: UserHistory.actions[:post_rejected]).count).to eq(1)
# We can't reject twice
expect(-> { qp.reject!(admin) }).to raise_error(QueuedPost::InvalidStateTransition)
end

View File

@ -488,4 +488,22 @@ describe StaffActionLogger do
expect { log_post_approved }.to change { UserHistory.count }.by(1)
end
end
describe 'log_post_rejected' do
let(:rejected_post) { Fabricate(:queued_post) }
subject(:log_post_rejected) { described_class.new(admin).log_post_rejected(rejected_post) }
it 'raises an error when post is nil' do
expect { logger.log_post_rejected(nil) }.to raise_error(Discourse::InvalidParameters)
end
it 'raises an error when post is not a QueuedPosts' do
expect { logger.log_post_rejected(1) }.to raise_error(Discourse::InvalidParameters)
end
it 'creates a new UserHistory record' do
expect { log_post_rejected }.to change { UserHistory.count }.by(1)
end
end
end