FEATURE: Job to mark historical posts as category expert posts (#47)

FEATURE: Job to mark historical posts as category expert posts (#47)

diff --git a/app/jobs/regular/correct_historical_category_expert_posts.rb b/app/jobs/regular/correct_historical_category_expert_posts.rb
new file mode 100644
index 0000000..0284e6b
--- /dev/null
+++ b/app/jobs/regular/correct_historical_category_expert_posts.rb
@@ -0,0 +1,31 @@
+# frozen_string_literal: true
+
+module Jobs
+  class CorrectHistoricalCategoryExpertPosts < ::Jobs::Base
+
+    sidekiq_options queue: 'low'
+
+    def execute(args = {})
+      return unless SiteSetting.approve_past_posts_on_becoming_category_expert
+
+      CategoryCustomField
+        .where(name: CategoryExperts::CATEGORY_EXPERT_GROUP_IDS)
+        .where.not(value: nil)
+        .where.not(value: "")
+        .pluck(:category_id, :value)
+        .each do |category_id, group_ids|
+          groups = Group.where(id: group_ids)
+          user_ids = groups.map(&:user_ids).flatten.uniq
+          next if user_ids.blank?
+
+          posts = Post.joins(:topic)
+            .where(user_id: user_ids)
+            .where(topic: { category_id: category_id })
+
+          posts.each do |post|
+            CategoryExperts::PostHandler.new(post: post).process_new_post(skip_validations: true)
+          end
+        end
+    end
+  end
+end
diff --git a/plugin.rb b/plugin.rb
index fe44abb..8813f6d 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -18,6 +18,7 @@ after_initialize do
     "../app/models/reviewable_category_expert_suggestion",
     "../app/serializers/reviewable_category_expert_suggestion_serializer",
     "../app/jobs/regular/approve_past_category_expert_posts",
+    "../app/jobs/regular/correct_historical_category_expert_posts",
     "../app/jobs/regular/unapprove_past_category_expert_posts",
     "../app/jobs/scheduled/remind_admin_of_category_experts_posts_job",
     "../app/jobs/scheduled/remind_category_experts_job",
diff --git a/spec/jobs/correct_historical_category_expert_posts.rb b/spec/jobs/correct_historical_category_expert_posts.rb
new file mode 100644
index 0000000..f36a1cc
--- /dev/null
+++ b/spec/jobs/correct_historical_category_expert_posts.rb
@@ -0,0 +1,55 @@
+# frozen_string_literal: true
+
+require 'rails_helper'
+
+describe Jobs::CorrectHistoricalCategoryExpertPosts do
+  fab!(:user1) { Fabricate(:user) }
+  fab!(:user2) { Fabricate(:user) }
+  fab!(:group1) { Fabricate(:group, users: [user1]) }
+  fab!(:group2) { Fabricate(:group, users: [user1, user2]) }
+  fab!(:category1) { Fabricate(:category) }
+  fab!(:category2) { Fabricate(:category) }
+  # Create a few topics
+  fab!(:off_topic) { Fabricate(:topic) }
+  fab!(:topic1) { Fabricate(:topic, category: category1) }
+  fab!(:topic2) { Fabricate(:topic, category: category2) }
+  # No custom fields needed
+  fab!(:p1) { create_post(user: user1, topic: off_topic) }
+  fab!(:p2) { create_post(user: user2, topic: off_topic) }
+
+  # custom fields for only p3, not p4.
+  fab!(:p3) { create_post(user: user1, topic: topic1) }
+  fab!(:p4) { create_post(user: user2, topic: topic1) }
+
+  # custom fields for both p5 and p6
+  fab!(:p5) { create_post(user: user1, topic: topic2) }
+  fab!(:p6) { create_post(user: user2, topic: topic2) }
+
+  before do
+    SiteSetting.enable_category_experts = true
+    SiteSetting.category_experts_posts_require_approval = false
+    SiteSetting.approve_past_posts_on_becoming_category_expert = true
+    Jobs.run_immediately!
+  end
+
+  it "correctly applies custom fields" do
+    category1.custom_fields[CategoryExperts::CATEGORY_EXPERT_GROUP_IDS] = group1.id
+    category1.save
+    category2.custom_fields[CategoryExperts::CATEGORY_EXPERT_GROUP_IDS] = group2.id
+    category2.save
+
+    Jobs::CorrectHistoricalCategoryExpertPosts.new.execute
+    # Topic fields
+    expect(off_topic.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES]).to eq(nil)
+    expect(topic1.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES]).to eq(group1.name)
+    expect(topic2.custom_fields[CategoryExperts::TOPIC_EXPERT_POST_GROUP_NAMES]).to eq(group2.name)
+
+    # Post fields
+    expect(p1.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq(nil)
+    expect(p2.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq(nil)
+    expect(p3.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq(group1.name)
+    expect(p4.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq(nil)
+    expect(p5.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq(group2.name)
+    expect(p6.custom_fields[CategoryExperts::POST_APPROVED_GROUP_NAME]).to eq(group2.name)
+  end
+end

GitHub sha: be0d07bdec1a31fae8224a0a157fb328124c4ebc

This commit appears in #47 which was approved by eviltrout. It was merged by markvanlan.