FEATURE: add staff action log for 'restore topic'

This commit is contained in:
Arpit Jalan 2018-03-21 09:45:16 +05:30
parent 25284d2340
commit d96c1058a2
8 changed files with 54 additions and 27 deletions

View File

@ -355,7 +355,7 @@ const Topic = RestModel.extend({
'details.can_delete': false, 'details.can_delete': false,
'details.can_recover': true 'details.can_recover': true
}); });
return ajax("/t/" + this.get('id'), { return ajax(`/t/${this.get('id')}`, {
data: { context: window.location.pathname }, data: { context: window.location.pathname },
type: 'DELETE' type: 'DELETE'
}); });
@ -369,7 +369,10 @@ const Topic = RestModel.extend({
'details.can_delete': true, 'details.can_delete': true,
'details.can_recover': false 'details.can_recover': false
}); });
return ajax("/t/" + this.get('id') + "/recover", { type: 'PUT' }); return ajax(`/t/${this.get('id')}/recover`, {
data: { context: window.location.pathname },
type: 'PUT'
});
}, },
// Update our attributes from a JSON result // Update our attributes from a JSON result

View File

@ -450,7 +450,7 @@ class TopicsController < ApplicationController
guardian.ensure_can_recover_topic!(topic) guardian.ensure_can_recover_topic!(topic)
first_post = topic.posts.with_deleted.order(:post_number).first first_post = topic.posts.with_deleted.order(:post_number).first
PostDestroyer.new(current_user, first_post).recover PostDestroyer.new(current_user, first_post, context: params[:context]).recover
render body: nil render body: nil
end end

View File

@ -70,7 +70,8 @@ class UserHistory < ActiveRecord::Base
check_personal_message: 51, check_personal_message: 51,
disabled_second_factor: 52, disabled_second_factor: 52,
post_edit: 53, post_edit: 53,
topic_published: 54 topic_published: 54,
recover_topic: 55
) )
end end
@ -119,7 +120,8 @@ class UserHistory < ActiveRecord::Base
:check_personal_message, :check_personal_message,
:disabled_second_factor, :disabled_second_factor,
:post_edit, :post_edit,
:topic_published :topic_published,
:recover_topic
] ]
end end

View File

@ -59,24 +59,24 @@ class StaffActionLogger
details: details.join("\n"))) details: details.join("\n")))
end end
def log_topic_deletion(deleted_topic, opts = {}) def log_topic_delete_recover(topic, action = "delete_topic", opts = {})
raise Discourse::InvalidParameters.new(:deleted_topic) unless deleted_topic && deleted_topic.is_a?(Topic) raise Discourse::InvalidParameters.new(:topic) unless topic && topic.is_a?(Topic)
user = deleted_topic.user ? "#{deleted_topic.user.username} (#{deleted_topic.user.name})" : "(deleted user)" user = topic.user ? "#{topic.user.username} (#{topic.user.name})" : "(deleted user)"
details = [ details = [
"id: #{deleted_topic.id}", "id: #{topic.id}",
"created_at: #{deleted_topic.created_at}", "created_at: #{topic.created_at}",
"user: #{user}", "user: #{user}",
"title: #{deleted_topic.title}" "title: #{topic.title}"
] ]
if first_post = deleted_topic.ordered_posts.first if first_post = topic.ordered_posts.with_deleted.first
details << "raw: #{first_post.raw}" details << "raw: #{first_post.raw}"
end end
UserHistory.create(params(opts).merge(action: UserHistory.actions[:delete_topic], UserHistory.create(params(opts).merge(action: UserHistory.actions[action.to_sym],
topic_id: deleted_topic.id, topic_id: topic.id,
details: details.join("\n"))) details: details.join("\n")))
end end

View File

@ -3298,6 +3298,7 @@ en:
revoke_badge: "revoke badge" revoke_badge: "revoke badge"
check_email: "check email" check_email: "check email"
delete_topic: "delete topic" delete_topic: "delete topic"
recover_topic: "un-delete topic"
delete_post: "delete post" delete_post: "delete post"
impersonate: "impersonate" impersonate: "impersonate"
anonymize_user: "anonymize user" anonymize_user: "anonymize user"

View File

@ -69,7 +69,10 @@ class PostDestroyer
topic.update_statistics topic.update_statistics
recover_user_actions recover_user_actions
DiscourseEvent.trigger(:post_recovered, @post, @opts, @user) DiscourseEvent.trigger(:post_recovered, @post, @opts, @user)
DiscourseEvent.trigger(:topic_recovered, topic, @user) if @post.is_first_post? if @post.is_first_post?
DiscourseEvent.trigger(:topic_recovered, topic, @user)
StaffActionLogger.new(@user).log_topic_delete_recover(topic, "recover_topic", @opts.slice(:context)) if @user.id != @post.user_id
end
end end
def staff_recovered def staff_recovered
@ -115,7 +118,7 @@ class PostDestroyer
remove_associated_replies remove_associated_replies
remove_associated_notifications remove_associated_notifications
if @post.topic && @post.is_first_post? if @post.topic && @post.is_first_post?
StaffActionLogger.new(@user).log_topic_deletion(@post.topic, @opts.slice(:context)) if @user.id != @post.user_id StaffActionLogger.new(@user).log_topic_delete_recover(@post.topic, "delete_topic", @opts.slice(:context)) if @user.id != @post.user_id
@post.topic.trash!(@user) @post.topic.trash!(@user)
elsif @user.id != @post.user_id elsif @user.id != @post.user_id
StaffActionLogger.new(@user).log_post_deletion(@post, @opts.slice(:context)) StaffActionLogger.new(@user).log_post_deletion(@post, @opts.slice(:context))

View File

@ -12,7 +12,7 @@ describe Admin::StaffActionLogsController do
it 'generates logs' do it 'generates logs' do
topic = Fabricate(:topic) topic = Fabricate(:topic)
_record = StaffActionLogger.new(Discourse.system_user).log_topic_deletion(topic) _record = StaffActionLogger.new(Discourse.system_user).log_topic_delete_recover(topic, "delete_topic")
get :index, params: { action_id: UserHistory.actions[:delete_topic] }, format: :json get :index, params: { action_id: UserHistory.actions[:delete_topic] }, format: :json

View File

@ -72,21 +72,39 @@ describe StaffActionLogger do
end end
end end
describe 'log_topic_deletion' do describe 'log_topic_delete_recover' do
let(:deleted_topic) { Fabricate(:topic) } let(:topic) { Fabricate(:topic) }
subject(:log_topic_deletion) { described_class.new(admin).log_topic_deletion(deleted_topic) } context "when deleting topic" do
subject(:log_topic_delete_recover) { described_class.new(admin).log_topic_delete_recover(topic) }
it 'raises an error when topic is nil' do it 'raises an error when topic is nil' do
expect { logger.log_topic_deletion(nil) }.to raise_error(Discourse::InvalidParameters) expect { logger.log_topic_delete_recover(nil) }.to raise_error(Discourse::InvalidParameters)
end
it 'raises an error when topic is not a Topic' do
expect { logger.log_topic_delete_recover(1) }.to raise_error(Discourse::InvalidParameters)
end
it 'creates a new UserHistory record' do
expect { log_topic_delete_recover }.to change { UserHistory.count }.by(1)
end
end end
it 'raises an error when topic is not a Topic' do context "when recovering topic" do
expect { logger.log_topic_deletion(1) }.to raise_error(Discourse::InvalidParameters) subject(:log_topic_delete_recover) { described_class.new(admin).log_topic_delete_recover(topic, "recover_topic") }
end
it 'creates a new UserHistory record' do it 'raises an error when topic is nil' do
expect { log_topic_deletion }.to change { UserHistory.count }.by(1) expect { logger.log_topic_delete_recover(nil, "recover_topic") }.to raise_error(Discourse::InvalidParameters)
end
it 'raises an error when topic is not a Topic' do
expect { logger.log_topic_delete_recover(1, "recover_topic") }.to raise_error(Discourse::InvalidParameters)
end
it 'creates a new UserHistory record' do
expect { log_topic_delete_recover }.to change { UserHistory.count }.by(1)
end
end end
end end