FIX: Skip user if the client_ip is not present

FIX: Skip user if the client_ip is not present

diff --git a/lib/discourse_akismet/users_bouncer.rb b/lib/discourse_akismet/users_bouncer.rb
index c99ea97..2d4da04 100644
--- a/lib/discourse_akismet/users_bouncer.rb
+++ b/lib/discourse_akismet/users_bouncer.rb
@@ -8,6 +8,7 @@ module DiscourseAkismet
       SiteSetting.akismet_review_users &&
         user.trust_level === TrustLevel[0] &&
         user.user_profile.bio_raw.present? &&
+        user.user_auth_token_logs&.last&.client_ip.present? &&
         !Reviewable.exists?(target: user)
     end
 
@@ -47,6 +48,7 @@ module DiscourseAkismet
 
     def args_for_user(user)
       profile = user.user_profile
+      token = user.user_auth_token_logs.last
 
       extra_args = {
         content_type: 'signup',
@@ -54,6 +56,8 @@ module DiscourseAkismet
         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
       }
 
       # Sending the email to akismet is optional
@@ -61,11 +65,6 @@ module DiscourseAkismet
         extra_args[:comment_author_email] = user.email
       end
 
-      if token = user.user_auth_token_logs.last
-        extra_args[:user_ip] = token.client_ip.to_s
-        extra_args[:user_agent] = token.user_agent
-      end
-
       extra_args
     end
   end
diff --git a/spec/lib/users_bouncer_spec.rb b/spec/lib/users_bouncer_spec.rb
index e4a8d31..8fd6c5a 100644
--- a/spec/lib/users_bouncer_spec.rb
+++ b/spec/lib/users_bouncer_spec.rb
@@ -7,6 +7,7 @@ RSpec.describe DiscourseAkismet::UsersBouncer, if: defined?(Reviewable) do
   let(:user) do
     user = Fabricate(:user, trust_level: TrustLevel[0])
     user.user_profile.bio_raw = "I am batman"
+    user.user_auth_token_logs = [UserAuthTokenLog.new(client_ip: '127.0.0.1', action: 'an_action')]
     user
   end
 
@@ -41,6 +42,18 @@ RSpec.describe DiscourseAkismet::UsersBouncer, if: defined?(Reviewable) do
     it "returns true for TL0 with a bio" do
       expect(subject.should_check_user?(user)).to eq(true)
     end
+
+    it "returns false when there are no auth token logs for that user" do
+      user.user_auth_token_logs = []
+
+      expect(subject.should_check_user?(user)).to eq(false)
+    end
+
+    it "returns false when there are no auth token logs for that user" do
+      user.user_auth_token_logs = [UserAuthTokenLog.new(client_ip: nil, action: 'an_action')]
+
+      expect(subject.should_check_user?(user)).to eq(false)
+    end
   end
 
   describe "#enqueue_for_check" do
diff --git a/spec/models/user_profile_spec.rb b/spec/models/user_profile_spec.rb
index ece5900..8c4b805 100644
--- a/spec/models/user_profile_spec.rb
+++ b/spec/models/user_profile_spec.rb
@@ -10,7 +10,11 @@ RSpec.describe UserProfile do
     end
 
     let(:user) do
-      Fabricate(:user, trust_level: TrustLevel[0])
+      Fabricate(
+        :user, 
+        trust_level: TrustLevel[0], 
+        user_auth_token_logs: [UserAuthTokenLog.new(client_ip: '127.0.0.1', action: 'an_action')]
+      )
     end
 
     it 'triggers a job to check for spam when the bio changes' do

GitHub sha: ef498d8a

2 Likes