`human?` helper method on a user

approved

#1

human? helper method on a user

This is cleaner than hard coding id > 0 in ruby code.

diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb
index 8712276..74ba5ab 100644
--- a/app/controllers/session_controller.rb
+++ b/app/controllers/session_controller.rb
@@ -329,7 +329,7 @@ class SessionController < ApplicationController
     RateLimiter.new(nil, "forgot-password-login-min-#{params[:login].to_s[0..100]}", 3, 1.minute).performed!
 
     user = User.find_by_username_or_email(params[:login])
-    user_presence = user.present? && user.id > 0 && !user.staged
+    user_presence = user.present? && user.human? && !user.staged
     if user_presence
       email_token = user.email_tokens.create(email: user.email)
       Jobs.enqueue(:critical_user_email, type: :forgot_password, user_id: user.id, email_token: email_token.token)
diff --git a/app/models/user.rb b/app/models/user.rb
index b07fc32..0de21d1 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -291,6 +291,10 @@ class User < ActiveRecord::Base
     fields.uniq
   end
 
+  def human?
+    self.id > 0
+  end
+
   def effective_locale
     if SiteSetting.allow_user_locale && self.locale.present?
       self.locale
diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb
index 8f48cd8..fee0fc7 100644
--- a/lib/post_destroyer.rb
+++ b/lib/post_destroyer.rb
@@ -226,7 +226,7 @@ class PostDestroyer
   end
 
   def agree_with_flags
-    if @post.has_active_flag? && @user.id > 0 && @user.staff?
+    if @post.has_active_flag? && @user.human? && @user.staff?
       Jobs.enqueue(
         :send_system_message,
         user_id: @post.user_id,
diff --git a/plugins/discourse-narrative-bot/plugin.rb b/plugins/discourse-narrative-bot/plugin.rb
index e44a8ae..93d5afa 100644
--- a/plugins/discourse-narrative-bot/plugin.rb
+++ b/plugins/discourse-narrative-bot/plugin.rb
@@ -154,7 +154,7 @@ after_initialize do
 
   self.add_to_class(:user, :enqueue_narrative_bot_job?) do
     SiteSetting.discourse_narrative_bot_enabled &&
-      self.id > 0 &&
+      self.human? &&
       !self.anonymous? &&
       !self.staged &&
       !SiteSetting.discourse_narrative_bot_ignored_usernames.split('|'.freeze).include?(self.username)
diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb
index 1775351..732e8c4 100644
--- a/spec/models/user_spec.rb
+++ b/spec/models/user_spec.rb
@@ -1989,4 +1989,14 @@ describe User do
       expect(PostAction.with_deleted.where(user_id: user.id).length).to eq(0)
     end
   end
+
+  context "human?" do
+    it "returns true for a regular user" do
+      expect(Fabricate(:user)).to be_human
+    end
+
+    it "returns false for the system user" do
+      expect(Discourse.system_user).not_to be_human
+    end
+  end
 end

GitHub sha: c719658f


Approved #2