FIX: log for CleanUpTags job (#23964)

In previous [PR](https://github.com/discourse/discourse/pull/23864) we introduced setting to automatically delete unused tags. This action should be logged.
This commit is contained in:
Krzysztof Kotlarek 2023-10-18 14:24:14 +11:00 committed by GitHub
parent e91d8feab3
commit 8c355d9e99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View File

@ -8,7 +8,14 @@ module Jobs
def execute(args)
return unless SiteSetting.automatically_clean_unused_tags
Tag.unused.where("tags.created_at < ?", GRACE_PERIOD_MINUTES.minutes.ago).destroy_all
Tag.transaction do
tags = Tag.unused.where("tags.created_at < ?", GRACE_PERIOD_MINUTES.minutes.ago)
StaffActionLogger.new(Discourse.system_user).log_custom(
"deleted_unused_tags",
tags: tags.pluck(:name),
)
tags.destroy_all
end
end
end
end

View File

@ -43,6 +43,16 @@ describe Jobs::CleanUpTags do
created_at: 10.minutes.ago,
)
end
fab!(:unused_tag_2) do
Fabricate(
:tag,
name: "unused2",
staff_topic_count: 0,
public_topic_count: 0,
pm_topic_count: 0,
created_at: 10.minutes.ago,
)
end
fab!(:tag_in_group) do
Fabricate(
@ -57,8 +67,17 @@ describe Jobs::CleanUpTags do
it "deletes unused tags" do
SiteSetting.automatically_clean_unused_tags = true
expect { job.execute({}) }.to change { Tag.count }.by(-1)
expect { job.execute({}) }.to change { Tag.count }.by(-2)
expect { unused_tag.reload }.to raise_error(ActiveRecord::RecordNotFound)
expect { unused_tag_2.reload }.to raise_error(ActiveRecord::RecordNotFound)
end
it "logs action" do
SiteSetting.automatically_clean_unused_tags = true
expect { job.execute({}) }.to change { UserHistory.count }.by(1)
log = UserHistory.last
expect(log.acting_user_id).to eq(-1)
expect(log.details).to eq('tags: ["unused1", "unused2"]')
end
it "does nothing when site setting is disabled by default" do