FIX: Send not_spam message only if topic exists (#21)

FIX: Send not_spam message only if topic exists (#21)

It attempted to build a system message containing the topic title, but that failed. This commit ensures that the message is skipped if the topic is no longer accessible because it was deleted.

diff --git a/models/reviewable_akismet_post.rb b/models/reviewable_akismet_post.rb
index 1b63c37..c6624c3 100644
--- a/models/reviewable_akismet_post.rb
+++ b/models/reviewable_akismet_post.rb
@@ -49,8 +49,7 @@ class ReviewableAkismetPost < Reviewable
 
     if post.deleted_at
       PostDestroyer.new(performed_by, post).recover
-      if SiteSetting.akismet_notify_user?
-        post.reload
+      if SiteSetting.akismet_notify_user? && post.reload.topic
         SystemMessage.new(post.user).create('akismet_not_spam', topic_title: post.topic.title, post_link: post.full_url)
       end
     end
diff --git a/spec/models/reviewable_akismet_post_spec.rb b/spec/models/reviewable_akismet_post_spec.rb
index 0ef67f5..0e25105 100644
--- a/spec/models/reviewable_akismet_post_spec.rb
+++ b/spec/models/reviewable_akismet_post_spec.rb
@@ -172,6 +172,17 @@ describe 'ReviewableAkismetPost' do
         pm = Topic.private_messages.last
         expect(pm.allowed_users).to contain_exactly(Discourse.system_user, post.user)
       end
+
+      it 'Does not send a system message to the user if topic is gone' do
+        first_post = Fabricate(:post_with_long_raw_content)
+        post = Fabricate(:post_with_long_raw_content, topic: first_post.topic)
+        PostDestroyer.new(Discourse.system_user, post).destroy
+        reviewable = ReviewableAkismetPost.needs_review!(target: post, created_by: Discourse.system_user)
+        PostDestroyer.new(Discourse.system_user, first_post).destroy
+
+        expect { reviewable.perform admin, action }
+          .to change { Topic.private_messages.count }.by(0)
+      end
     end
 
     describe '#perform_ignore' do

GitHub sha: f3248d75

This commit appears in #21 which was approved by eviltrout and ZogStriP. It was merged by udan11.