diff --git a/app/models/user.rb b/app/models/user.rb index 17d1d883598..8308ad5efba 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -683,6 +683,39 @@ class User < ActiveRecord::Base end end + def number_of_deleted_posts + Post.with_deleted + .where(user_id: self.id) + .where(user_deleted: false) + .where.not(deleted_by_id: self.id) + .where.not(deleted_at: nil) + .count + end + + def number_of_flagged_posts + Post.with_deleted + .where(user_id: self.id) + .where(id: PostAction.where(post_action_type_id: PostActionType.notify_flag_type_ids) + .where(disagreed_at: nil) + .select(:post_id)) + .count + end + + def number_of_flags_given + PostAction.where(user_id: self.id) + .where(disagreed_at: nil) + .where(post_action_type_id: PostActionType.notify_flag_type_ids) + .count + end + + def number_of_warnings + self.warnings.count + end + + def number_of_suspensions + UserHistory.for(self, :suspend_user).count + end + protected def badge_grant @@ -818,38 +851,6 @@ class User < ActiveRecord::Base end end - def number_of_deleted_posts - Post.with_deleted - .where(user_id: self.id) - .where(user_deleted: false) - .where.not(deleted_by_id: self.id) - .where.not(deleted_at: nil) - .count - end - - def number_of_flagged_posts - Post.with_deleted - .where(user_id: self.id) - .where(id: PostAction.where(post_action_type_id: PostActionType.notify_flag_type_ids) - .where(disagreed_at: nil) - .select(:post_id)) - .count - end - - def number_of_flags_given - PostAction.where(user_id: self.id) - .where(post_action_type_id: PostActionType.notify_flag_type_ids) - .count - end - - def number_of_warnings - self.warnings.count - end - - def number_of_suspensions - UserHistory.for(self, :suspend_user).count - end - private def previous_visit_at_update_required?(timestamp) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 8ecb4c55e93..71858f1092e 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1219,4 +1219,27 @@ describe User do end + describe "number_of_flags_given" do + + let(:user) { Fabricate(:user) } + let(:moderator) { Fabricate(:moderator) } + + it "doesn't count disagreed flags" do + post_agreed = Fabricate(:post) + PostAction.act(user, post_agreed, PostActionType.types[:off_topic]) + PostAction.agree_flags!(post_agreed, moderator) + + post_deferred = Fabricate(:post) + PostAction.act(user, post_deferred, PostActionType.types[:inappropriate]) + PostAction.defer_flags!(post_deferred, moderator) + + post_disagreed = Fabricate(:post) + PostAction.act(user, post_disagreed, PostActionType.types[:spam]) + PostAction.clear_flags!(post_disagreed, moderator) + + expect(user.number_of_flags_given).to eq(2) + end + + end + end