FEATURE: Add `solved_topics_auto_close_hours` site setting.

https://meta.discourse.org/t/automatically-close-solved-topics-after-n-hours/58383
This commit is contained in:
Guo Xiang Tan 2017-05-22 17:09:40 +08:00
parent 4fc6844cc0
commit ed8b0103f6
4 changed files with 51 additions and 8 deletions

View File

@ -5,6 +5,7 @@ en:
accept_all_solutions_trust_level: "Minimum trust level required to accept solutions on any topic (even when not OP)"
empty_box_on_unsolved: "Display an empty box next to unsolved topics"
solved_quote_length: "Number of characters to quote when displaying the solution under the first post"
solved_topics_auto_close_hours: "Auto close topic (n) hours after the last reply once the topic has been marked as solved"
reports:
accepted_solutions:
title: "Accepted solutions"

View File

@ -14,4 +14,5 @@ plugins:
solved_quote_length:
default: 300
client: false
solved_topics_auto_close_hours:
default: 0

View File

@ -65,10 +65,11 @@ SQL
limit_accepts
post = Post.find(params[:id].to_i)
topic = post.topic
guardian.ensure_can_accept_answer!(post.topic)
guardian.ensure_can_accept_answer!(topic)
accepted_id = post.topic.custom_fields["accepted_answer_post_id"].to_i
accepted_id = topic.custom_fields["accepted_answer_post_id"].to_i
if accepted_id > 0
if p2 = Post.find_by(id: accepted_id)
p2.custom_fields["is_accepted_answer"] = nil
@ -81,8 +82,8 @@ SQL
end
post.custom_fields["is_accepted_answer"] = "true"
post.topic.custom_fields["accepted_answer_post_id"] = post.id
post.topic.save!
topic.custom_fields["accepted_answer_post_id"] = post.id
topic.save!
post.save!
if defined?(UserAction::SOLVED)
@ -102,13 +103,22 @@ SQL
data: {
message: 'solved.accepted_notification',
display_username: current_user.username,
topic_title: post.topic.title
topic_title: topic.title
}.to_json
)
end
DiscourseEvent.trigger(:accepted_solution, post)
if (auto_close_hours = SiteSetting.solved_topics_auto_close_hours) > 0
topic.set_or_create_timer(
TopicTimer.types[:close],
auto_close_hours,
based_on_last_post: true
)
MessageBus.publish("/topic/#{topic.id}", reload_topic: true)
end
DiscourseEvent.trigger(:accepted_solution, post)
render json: success_json
end

View File

@ -0,0 +1,31 @@
require 'rails_helper'
RSpec.describe "Managing Posts solved status" do
let(:topic) { Fabricate(:topic) }
let(:user) { Fabricate(:trust_level_4) }
let(:p1) { Fabricate(:post, topic: topic) }
before do
SiteSetting.allow_solved_on_all_topics = true
end
describe 'accepting a post as the answer' do
before do
sign_in(user)
SiteSetting.solved_topics_auto_close_hours = 2
end
it 'can mark a post as the accepted answer correctly' do
xhr :post, "/solution/accept", id: p1.id
expect(p1.reload.custom_fields["is_accepted_answer"]).to eq("true")
expect(topic.public_topic_timer.status_type).to eq(TopicTimer.types[:close])
expect(topic.public_topic_timer.execute_at)
.to be_within(1.second).of(Time.zone.now + 2.hours)
expect(topic.public_topic_timer.based_on_last_post).to eq(true)
end
end
end