From c719658f9fdb71cd9628efc0a08b63c9f917044b Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 8 Feb 2019 13:34:54 -0500 Subject: [PATCH] `human?` helper method on a user This is cleaner than hard coding `id > 0` in ruby code. --- app/controllers/session_controller.rb | 2 +- app/models/user.rb | 4 ++++ lib/post_destroyer.rb | 2 +- plugins/discourse-narrative-bot/plugin.rb | 2 +- spec/models/user_spec.rb | 10 ++++++++++ 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb index 8712276560a..74ba5ab2047 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 b07fc32f26f..0de21d1e60c 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 8f48cd898e0..fee0fc79ea8 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 e44a8ae1d25..93d5afa72ce 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 1775351bcf4..732e8c46275 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