diff --git a/app/controllers/admin/flags_controller.rb b/app/controllers/admin/flags_controller.rb index d20f268d422..2647029c990 100644 --- a/app/controllers/admin/flags_controller.rb +++ b/app/controllers/admin/flags_controller.rb @@ -24,11 +24,14 @@ limit 100 sql.where2 "post_action_type_id in (:flag_types)", flag_types: PostActionType.FlagTypes + + # it may make sense to add a view that shows flags on deleted posts, + # we don't clear the flags on post deletion, just supress counts + # they may have deleted_at on the action not set if params[:filter] == 'old' - sql.where "p.deleted_at is null" sql.where2 "deleted_at is not null" else - sql.where "p.deleted_at is null" + sql.where "p.deleted_at is null and t.deleted_at is null" sql.where2 "deleted_at is null" end diff --git a/app/models/post.rb b/app/models/post.rb index 37207a3d1eb..1957cd5322c 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -26,6 +26,8 @@ class Post < ActiveRecord::Base rate_limit acts_as_paranoid + after_recover :update_flagged_posts_count + after_destroy :update_flagged_posts_count belongs_to :user belongs_to :topic, counter_cache: :posts_count @@ -163,6 +165,10 @@ class Post < ActiveRecord::Base where("(post_number = 1) or (score >= ?)", SiteSetting.best_of_score_threshold) end + def update_flagged_posts_count + PostAction.update_flagged_posts_count + end + def filter_quotes(parent_post=nil) return cooked if parent_post.blank? diff --git a/app/models/post_action.rb b/app/models/post_action.rb index 631cb56a604..5c62edd7da2 100644 --- a/app/models/post_action.rb +++ b/app/models/post_action.rb @@ -13,7 +13,10 @@ class PostAction < ActiveRecord::Base rate_limit :post_action_rate_limiter def self.update_flagged_posts_count - val = exec_sql('select count(*) from posts where deleted_at is null and id in (select post_id from post_actions where post_action_type_id in (?) and deleted_at is null)', PostActionType.FlagTypes).values[0][0].to_i + val = exec_sql('select count(*) from posts p + join topics t on t.id = p.topic_id + where p.deleted_at is null and t.deleted_at is null and p.id in + (select post_id from post_actions where post_action_type_id in (?) and deleted_at is null)', PostActionType.FlagTypes).values[0][0].to_i $redis.set('posts_flagged_count', val) admins = User.exec_sql("select id from users where admin = 't'").map{|r| r["id"].to_i} diff --git a/app/models/topic.rb b/app/models/topic.rb index ae70bd123be..13a93a661ac 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -11,7 +11,9 @@ class Topic < ActiveRecord::Base versioned :if => :new_version_required? acts_as_paranoid - + after_recover :update_flagged_posts_count + after_destroy :update_flagged_posts_count + rate_limit :default_rate_limiter rate_limit :limit_topics_per_day rate_limit :limit_private_messages_per_day @@ -387,6 +389,10 @@ class Topic < ActiveRecord::Base topic end + def update_flagged_posts_count + PostAction.update_flagged_posts_count + end + # Create the summary of the interesting posters in a topic. Cheats to avoid # many queries. diff --git a/spec/models/post_action_spec.rb b/spec/models/post_action_spec.rb index 95c7062c2ee..10c63990000 100644 --- a/spec/models/post_action_spec.rb +++ b/spec/models/post_action_spec.rb @@ -27,6 +27,19 @@ describe PostAction do PostAction.flagged_posts_count.should == 0 end + it "should reset counts when a topic is deleted" do + PostAction.act(codinghorror, post, PostActionType.Types[:off_topic]) + post.topic.destroy + PostAction.flagged_posts_count.should == 0 + end + + it "should reset counts when a post is deleted" do + post2 = Fabricate(:post, topic_id: post.topic_id) + PostAction.act(codinghorror, post2, PostActionType.Types[:off_topic]) + post2.destroy + PostAction.flagged_posts_count.should == 0 + end + end it "increases the post's bookmark count when saved" do