Skip to content
Permalink
Browse files

FEATURE: if auto bump is enabled, do not bump solved

  • Loading branch information
SamSaffron committed Jul 18, 2018
1 parent 258956f commit 5b81cc92934f6cf4ca4a71fde206bd3e66c39452
Showing with 65 additions and 23 deletions.
  1. +40 −23 plugin.rb
  2. +25 −0 spec/integration/solved_spec.rb
@@ -60,20 +60,10 @@ class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscourseSolved
end
end

require_dependency "application_controller"
class DiscourseSolved::AnswerController < ::ApplicationController

def accept

limit_accepts

post = Post.find(params[:id].to_i)
def self.accept_answer!(post, acting_user)
topic = post.topic

guardian.ensure_can_accept_answer!(topic)

accepted_id = topic.custom_fields["accepted_answer_post_id"].to_i
if accepted_id > 0
if p2 = Post.find_by(id: accepted_id)
@@ -95,21 +85,21 @@ def accept
UserAction.log_action!(
action_type: UserAction::SOLVED,
user_id: post.user_id,
acting_user_id: guardian.user.id,
acting_user_id: acting_user.id,
target_post_id: post.id,
target_topic_id: post.topic_id
)
end

unless current_user.id == post.user_id
unless acting_user.id == post.user_id
Notification.create!(
notification_type: Notification.types[:custom],
user_id: post.user_id,
topic_id: post.topic_id,
post_number: post.post_number,
data: {
message: 'solved.accepted_notification',
display_username: current_user.username,
display_username: acting_user.username,
topic_title: topic.title
}.to_json
)
@@ -126,17 +116,9 @@ def accept
end

DiscourseEvent.trigger(:accepted_solution, post)
render json: success_json
end

def unaccept

limit_accepts

post = Post.find(params[:id].to_i)

guardian.ensure_can_accept_answer!(post.topic)

def self.unaccept_answer!(post)
post.custom_fields["is_accepted_answer"] = nil
post.topic.custom_fields["accepted_answer_post_id"] = nil
post.topic.save!
@@ -161,7 +143,32 @@ def unaccept
notification.destroy if notification

DiscourseEvent.trigger(:unaccepted_solution, post)
end
end

require_dependency "application_controller"
class DiscourseSolved::AnswerController < ::ApplicationController

def accept

limit_accepts

post = Post.find(params[:id].to_i)
guardian.ensure_can_accept_answer!(post.topic)

DiscourseSolved.accept_answer!(post, current_user)

render json: success_json
end

def unaccept

limit_accepts

post = Post.find(params[:id].to_i)
guardian.ensure_can_accept_answer!(post.topic)

DiscourseSolved.unaccept_answer!(post)
render json: success_json
end

@@ -432,4 +439,14 @@ def include_can_have_answer?
CategoryList.preloaded_topic_custom_fields << "accepted_answer_post_id"
end

on(:filter_auto_bump_topics) do |_category, filters|
filters.push(->(r) { r.where(<<~SQL)
NOT EXISTS(
SELECT 1 FROM topic_custom_fields
WHERE topic_id = topics.id
AND name = 'accepted_answer_post_id'
)
SQL
})
end
end
@@ -9,6 +9,31 @@
SiteSetting.allow_solved_on_all_topics = true
end

describe 'auto bump' do
it 'does not automatically bump solved topics' do
category = Fabricate(:category)

post = create_post(category: category)
post2 = create_post(category: category)

DiscourseSolved.accept_answer!(post, Discourse.system_user)

category.num_auto_bump_daily = 2
category.save!

freeze_time 1.month.from_now

expect(category.auto_bump_topic!).to eq(true)

freeze_time 13.hours.from_now

expect(category.auto_bump_topic!).to eq(false)

expect(post.topic.reload.posts_count).to eq(1)
expect(post2.topic.reload.posts_count).to eq(2)
end
end

describe 'accepting a post as the answer' do
before do
sign_in(user)

0 comments on commit 5b81cc9

Please sign in to comment.
You can’t perform that action at this time.