Merge pull request #59 from discourse/add_topic_auto_close_on_solved

FEATURE: Add `solved_topics_auto_close_hours` site setting.
This commit is contained in:
Guo Xiang Tan 2017-05-26 15:30:50 +08:00 committed by GitHub
commit 53695c5762
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)" 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" 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_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: reports:
accepted_solutions: accepted_solutions:
title: "Accepted solutions" title: "Accepted solutions"

View File

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

View File

@ -65,10 +65,11 @@ SQL
limit_accepts limit_accepts
post = Post.find(params[:id].to_i) 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 accepted_id > 0
if p2 = Post.find_by(id: accepted_id) if p2 = Post.find_by(id: accepted_id)
p2.custom_fields["is_accepted_answer"] = nil p2.custom_fields["is_accepted_answer"] = nil
@ -81,8 +82,8 @@ SQL
end end
post.custom_fields["is_accepted_answer"] = "true" post.custom_fields["is_accepted_answer"] = "true"
post.topic.custom_fields["accepted_answer_post_id"] = post.id topic.custom_fields["accepted_answer_post_id"] = post.id
post.topic.save! topic.save!
post.save! post.save!
if defined?(UserAction::SOLVED) if defined?(UserAction::SOLVED)
@ -102,13 +103,22 @@ SQL
data: { data: {
message: 'solved.accepted_notification', message: 'solved.accepted_notification',
display_username: current_user.username, display_username: current_user.username,
topic_title: post.topic.title topic_title: topic.title
}.to_json }.to_json
) )
end 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 render json: success_json
end end
@ -236,7 +246,7 @@ SQL
.joins(:user) .joins(:user)
.pluck('post_number', 'username', 'cooked') .pluck('post_number', 'username', 'cooked')
.first .first
if postInfo if postInfo
postInfo[2] = PrettyText.excerpt(postInfo[2], SiteSetting.solved_quote_length) postInfo[2] = PrettyText.excerpt(postInfo[2], SiteSetting.solved_quote_length)
return postInfo return postInfo

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