FIX: Correctly send feedback to Akismet when the first post of a topic is flagged (#58)

FIX: Correctly send feedback to Akismet when the first post of a topic is flagged (#58)

diff --git a/lib/discourse_akismet/bouncer.rb b/lib/discourse_akismet/bouncer.rb
index 786d2e1..e4ba311 100644
--- a/lib/discourse_akismet/bouncer.rb
+++ b/lib/discourse_akismet/bouncer.rb
@@ -7,7 +7,6 @@ module DiscourseAkismet
     AKISMET_STATE = 'AKISMET_STATE'
 
     def submit_feedback(target, status)
-      return unless suspect?(target)
       raise Discourse::InvalidParameters.new(:status) unless VALID_STATUSES.include?(status)
       feedback = args_for(target)
 
diff --git a/lib/discourse_akismet/posts_bouncer.rb b/lib/discourse_akismet/posts_bouncer.rb
index b912343..de52127 100644
--- a/lib/discourse_akismet/posts_bouncer.rb
+++ b/lib/discourse_akismet/posts_bouncer.rb
@@ -68,7 +68,7 @@ module DiscourseAkismet
     def args_for(post)
       extra_args = {
         blog: Discourse.base_url,
-        content_type: 'forum-post',
+        content_type: post.is_first_post? ? 'forum-post' : 'reply',
         referrer: post.custom_fields['AKISMET_REFERRER'],
         permalink: "#{Discourse.base_url}#{post.url}",
         comment_author: post.user.try(:username),
@@ -134,7 +134,10 @@ module DiscourseAkismet
     end
 
     def comment_content(post)
-      post.is_first_post? ? "#{post.topic && post.topic.title}\n\n#{post.raw}" : post.raw
+      return post.raw unless post.is_first_post?
+
+      topic = post.topic || Topic.with_deleted.find_by(id: post.topic_id)
+      "#{topic && topic.title}\n\n#{post.raw}"
     end
   end
 end
diff --git a/lib/discourse_akismet/users_bouncer.rb b/lib/discourse_akismet/users_bouncer.rb
index cee8f86..a3bae2d 100644
--- a/lib/discourse_akismet/users_bouncer.rb
+++ b/lib/discourse_akismet/users_bouncer.rb
@@ -11,12 +11,9 @@ module DiscourseAkismet
         .where("ucf.value = 'new' OR (ucf.value = 'skipped' AND users.created_at > ?)", 1.day.ago)
     end
 
-    def should_check?(user)
-      SiteSetting.akismet_review_users? && super(user)
-    end
-
     def suspect?(user)
-      user.trust_level === TrustLevel[0] &&
+      SiteSetting.akismet_review_users? &&
+        user.trust_level === TrustLevel[0] &&
         user.user_profile.bio_raw.present? &&
         user.user_auth_token_logs&.last&.client_ip.present?
     end
@@ -30,10 +27,10 @@ module DiscourseAkismet
         content_type: 'signup',
         permalink: "#{Discourse.base_url}/u/#{user.username_lower}",
         comment_author: user.username,
-        comment_content: profile.bio_raw,
-        comment_author_url: profile.website,
-        user_ip: token.client_ip.to_s,
-        user_agent: token.user_agent
+        comment_content: profile&.bio_raw,
+        comment_author_url: profile&.website,
+        user_ip: token&.client_ip&.to_s,
+        user_agent: token&.user_agent
       }
 
       # Sending the email to akismet is optional
diff --git a/spec/fabricators/reviewable_akismet_post_fabricator.rb b/spec/fabricators/reviewable_akismet_post_fabricator.rb
index df3bd7e..813f7aa 100644
--- a/spec/fabricators/reviewable_akismet_post_fabricator.rb
+++ b/spec/fabricators/reviewable_akismet_post_fabricator.rb
@@ -14,5 +14,10 @@ Fabricator(:reviewable_akismet_user) do
   type 'ReviewableAkismetUser'
   created_by { Discourse.system_user }
   target_type 'User'
-  target { Fabricate(:user) }
+  target do
+    Fabricate(:user, trust_level: TrustLevel[0]).tap do |user|
+      user.user_profile.bio_raw = "I am batman"
+      user.user_auth_token_logs = [UserAuthTokenLog.new(client_ip: '127.0.0.1', action: 'an_action')]
+    end
+  end
 end
diff --git a/spec/lib/posts_bouncer_spec.rb b/spec/lib/posts_bouncer_spec.rb
index a548160..62d43cd 100644
--- a/spec/lib/posts_bouncer_spec.rb
+++ b/spec/lib/posts_bouncer_spec.rb
@@ -7,27 +7,29 @@ describe DiscourseAkismet::PostsBouncer do
   before do
     SiteSetting.akismet_api_key = 'not_a_real_key'
     SiteSetting.akismet_enabled = true
+
+    @referrer = 'https://discourse.org'
+    @ip_address = '1.2.3.4'
+    @user_agent = 'Discourse Agent'
+
+    subject.store_additional_information(post, {
+      ip_address: @ip_address,
+      user_agent: @user_agent,
+      referrer: @referrer
+    })
   end
 
   let(:post) { Fabricate(:post) }
 
   describe '#args_for' do
-    before do
-      post.upsert_custom_fields(
-        'AKISMET_REFERRER' => 'https://discourse.org',
-        'AKISMET_IP_ADDRESS' => '1.2.3.4',
-        'AKISMET_USER_AGENT' => 'Discourse Agent'
-      )
-    end
-
     it "should return args for a post" do
       result = subject.args_for(post)
       expect(result[:content_type]).to eq('forum-post')
       expect(result[:permalink]).to be_present
       expect(result[:comment_content]).to be_present
-      expect(result[:user_ip]).to eq('1.2.3.4')
-      expect(result[:referrer]).to eq('https://discourse.org')
-      expect(result[:user_agent]).to eq('Discourse Agent')
+      expect(result[:user_ip]).to eq(@ip_address)
+      expect(result[:referrer]).to eq(@referrer)
+      expect(result[:user_agent]).to eq(@user_agent)
       expect(result[:comment_author]).to eq(post.user.username)
       expect(result[:comment_author_email]).to eq(post.user.email)
       expect(result[:blog]).to eq(Discourse.base_url)
@@ -39,6 +41,16 @@ describe DiscourseAkismet::PostsBouncer do
       expect(result[:comment_author_email]).to be_blank
     end
 
+    it 'works with deleted posts and topics' do
+      topic_title = post.topic.title
+      PostDestroyer.new(Discourse.system_user, post).destroy
+      deleted_post = Post.with_deleted.find(post.id)
+
+      result = subject.args_for(deleted_post)
+
+      expect(result[:comment_content]).to include(topic_title)
+    end
+
     context "custom munge" do
       after do
         described_class.reset_munge
@@ -65,21 +77,10 @@ describe DiscourseAkismet::PostsBouncer do
   end
 
   describe "custom fields" do
-    before do
-      subject.store_additional_information(
-        post,
-        ip_address: '1.2.3.5',
-        referrer: 'https://eviltrout.com',
-        user_agent: 'Discourse App',
-       )
-
-       subject.move_to_state(post, 'skipped')
-    end
-
     it "custom fields can be attached and IPs anonymized" do
-      expect(post.custom_fields['AKISMET_IP_ADDRESS']).to eq('1.2.3.5')
-      expect(post.custom_fields['AKISMET_REFERRER']).to eq('https://eviltrout.com')
-      expect(post.custom_fields['AKISMET_USER_AGENT']).to eq('Discourse App')
+      expect(post.custom_fields['AKISMET_IP_ADDRESS']).to eq(@ip_address)
+      expect(post.custom_fields['AKISMET_REFERRER']).to eq(@referrer)
+      expect(post.custom_fields['AKISMET_USER_AGENT']).to eq(@user_agent)
 
       UserAnonymizer.new(post.user, nil, anonymize_ip: '0.0.0.0').make_anonymous
       post.reload
diff --git a/spec/models/reviewable_akismet_post_spec.rb b/spec/models/reviewable_akismet_post_spec.rb
index 709727a..dafaea7 100644
--- a/spec/models/reviewable_akismet_post_spec.rb
+++ b/spec/models/reviewable_akismet_post_spec.rb
@@ -72,7 +72,7 @@ describe 'ReviewableAkismetPost' do
     let(:reviewable) { ReviewableAkismetPost.needs_review!(target: post, created_by: admin).reload }
 
     before do
-      post.trash!(admin)
+      PostDestroyer.new(admin, post).destroy
     end
 
     shared_examples 'It logs actions in the staff actions logger' do

GitHub sha: 86dbfc87

This commit appears in #58 which was approved by eviltrout. It was merged by romanrizzi.