FEATURE: do not automatically approve first post (#33)

FEATURE: do not automatically approve first post (#33)

First post should not be automatically approved

diff --git a/lib/category_experts/post_handler.rb b/lib/category_experts/post_handler.rb
index 30b2834..5ff46b5 100644
--- a/lib/category_experts/post_handler.rb
+++ b/lib/category_experts/post_handler.rb
@@ -50,6 +50,7 @@ module CategoryExperts
 
     def mark_post_as_approved
       raise Discourse::InvalidParameters unless ensure_poster_is_category_expert
+      return if post.is_first_post?
 
       post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME] = users_expert_group.name
       post.custom_fields[CategoryExperts::POST_PENDING_EXPERT_APPROVAL] = false
diff --git a/spec/lib/post_handler_spec.rb b/spec/lib/post_handler_spec.rb
index 2f847e0..18ac22c 100644
--- a/spec/lib/post_handler_spec.rb
+++ b/spec/lib/post_handler_spec.rb
@@ -10,6 +10,7 @@ describe CategoryExperts::PostHandler do
   fab!(:group) { Fabricate(:group, users: [expert]) }
   fab!(:second_group) { Fabricate(:group, users: [second_expert]) }
   fab!(:topic) { Fabricate(:topic, category: category) }
+  fab!(:first_post) { Fabricate(:post, topic: topic) }
 
   before do
     SiteSetting.enable_category_experts
@@ -50,7 +51,10 @@ describe CategoryExperts::PostHandler do
       SiteSetting.category_experts_posts_require_approval = false
     end
 
-    it "marks posts as approved automatically" do
+    it "marks posts as approved automatically but not the first post" do
+      expect(first_post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq(nil)
+      expect(topic.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES]).to eq(nil)
+
       result = NewPostManager.new(expert, raw: 'this is a new post', topic_id: topic.id).perform
 
       expect(result.post.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq(group.name)
diff --git a/spec/requests/category_experts_controller_spec.rb b/spec/requests/category_experts_controller_spec.rb
index 0fc0468..9da1621 100644
--- a/spec/requests/category_experts_controller_spec.rb
+++ b/spec/requests/category_experts_controller_spec.rb
@@ -114,6 +114,7 @@ describe CategoryExpertsController do
 
   describe "#approve_post" do
     fab!(:topic) { Fabricate(:topic, category: category1) }
+    fab!(:first_post) { Fabricate(:post, topic: topic) }
 
     before do
       create_post(topic_id: topic.id, user: user)
@@ -156,7 +157,7 @@ describe CategoryExpertsController do
       end
 
       it "adds the group names to the topic custom field when an approved post already exists" do
-        CategoryExperts::PostHandler.new(post: topic.first_post).mark_post_as_approved
+        CategoryExperts::PostHandler.new(post: topic.posts.last).mark_post_as_approved
 
         post = create_post(topic_id: topic.id, user: other_user)
         CategoryExperts::PostHandler.new(post: post).mark_post_for_approval
@@ -214,12 +215,12 @@ describe CategoryExpertsController do
       end
 
       it "doesn't remove the group name from the topic custom field if another approved post exists" do
-        CategoryExperts::PostHandler.new(post: topic.first_post).mark_post_as_approved
-
         post = create_post(topic_id: topic.id, user: user)
         CategoryExperts::PostHandler.new(post: post).mark_post_as_approved
 
-        post("/category-experts/unapprove.json", params: { post_id: post.id })
+        post2 = create_post(topic_id: topic.id, user: user)
+        CategoryExperts::PostHandler.new(post: post2).mark_post_as_approved
+        post("/category-experts/unapprove.json", params: { post_id: post2.id })
 
         expect(response.status).to eq(200)
 

GitHub sha: 083ca4f4

This commit appears in #33 which was approved by markvanlan. It was merged by lis2.