Skip to content

Commit

Permalink
FEATURE: solved topic auto close setting per category (#233)
Browse files Browse the repository at this point in the history
* FEATURE: solved topic auto close setting per category

This commit adds per category "solved topics auto close hours" setting. The plugin would use the existing "solved topics auto close hours" setting, except if there was a setting for the relevant category in which case that would take precedence.

* minor changes per feedback
  • Loading branch information
Arpit Jalan committed Apr 19, 2023
1 parent 4ae1841 commit bffc468
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
@@ -1,5 +1,6 @@
<h3>{{i18n "solved.title"}}</h3>

{{#unless siteSettings.allow_solved_on_all_topics}}
<h3>{{i18n "solved.title"}}</h3>
<section class="field">
<div class="enable-accepted-answer">
<label class="checkbox-label">
Expand All @@ -12,4 +13,16 @@
</label>
</div>
</section>
{{/unless}}
{{/unless}}

<section class="field auto-close-solved-topics">
<label for="auto-close-solved-topics">
{{i18n "solved.solved_topics_auto_close_hours"}}
</label>
<NumberField
@number={{this.category.custom_fields.solved_topics_auto_close_hours}}
@id="auto-close-solved-topics"
@type="number"
@min="0"
/>
</section>
1 change: 1 addition & 0 deletions config/locales/client.en.yml
Expand Up @@ -9,6 +9,7 @@ en:
solved:
title: "Solved"
allow_accepted_answers: "Allow topic owner and staff to mark a reply as the solution"
solved_topics_auto_close_hours: "Auto close topic (n) hours after the last reply once the topic has been marked as solved."
accept_answer: "Select if this reply solves the problem"
accepted_description: "This is the accepted solution to this topic"
has_no_accepted_answer: "This topic has no solution"
Expand Down
8 changes: 7 additions & 1 deletion plugin.rb
Expand Up @@ -133,7 +133,13 @@ def self.accept_answer!(post, acting_user, topic: nil)
)
end

auto_close_hours = SiteSetting.solved_topics_auto_close_hours
auto_close_hours = 0
if topic&.category.present?
auto_close_hours = topic.category.custom_fields["solved_topics_auto_close_hours"].to_i
auto_close_hours = 175_200 if auto_close_hours > 175_200 # 20 years
end

auto_close_hours = SiteSetting.solved_topics_auto_close_hours if auto_close_hours == 0

if (auto_close_hours > 0) && !topic.closed
topic_timer =
Expand Down
26 changes: 26 additions & 0 deletions spec/integration/solved_spec.rb
Expand Up @@ -61,6 +61,32 @@
expect(topic.public_topic_timer.based_on_last_post).to eq(true)
end

it "gives priority to category's solved_topics_auto_close_hours setting" do
freeze_time
custom_auto_close_category = Fabricate(:category)
topic_2 = Fabricate(:topic, category: custom_auto_close_category)
post_2 = Fabricate(:post, topic: topic_2)
custom_auto_close_category.custom_fields["solved_topics_auto_close_hours"] = 4
custom_auto_close_category.save_custom_fields

post "/solution/accept.json", params: { id: post_2.id }

expect(response.status).to eq(200)
expect(post_2.reload.custom_fields["is_accepted_answer"]).to eq("true")

topic_2.reload

expect(topic_2.public_topic_timer.status_type).to eq(TopicTimer.types[:silent_close])

expect(
topic_2.custom_fields[DiscourseSolved::AUTO_CLOSE_TOPIC_TIMER_CUSTOM_FIELD].to_i,
).to eq(topic_2.public_topic_timer.id)

expect(topic_2.public_topic_timer.execute_at).to eq_time(Time.zone.now + 4.hours)

expect(topic_2.public_topic_timer.based_on_last_post).to eq(true)
end

it "sends notifications to correct users" do
SiteSetting.notify_on_staff_accept_solved = true
user = Fabricate(:user)
Expand Down

0 comments on commit bffc468

Please sign in to comment.