flagging work, we should be clearing from the mod menu if a topic or post is deleted

This commit is contained in:
Sam Saffron 2013-02-06 12:13:41 +11:00
parent 3fd019c6bd
commit 6f2f7b0589
5 changed files with 35 additions and 4 deletions

View file

@ -24,11 +24,14 @@ limit 100
sql.where2 "post_action_type_id in (:flag_types)", flag_types: PostActionType.FlagTypes 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' if params[:filter] == 'old'
sql.where "p.deleted_at is null"
sql.where2 "deleted_at is not null" sql.where2 "deleted_at is not null"
else 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" sql.where2 "deleted_at is null"
end end

View file

@ -26,6 +26,8 @@ class Post < ActiveRecord::Base
rate_limit rate_limit
acts_as_paranoid acts_as_paranoid
after_recover :update_flagged_posts_count
after_destroy :update_flagged_posts_count
belongs_to :user belongs_to :user
belongs_to :topic, counter_cache: :posts_count 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) where("(post_number = 1) or (score >= ?)", SiteSetting.best_of_score_threshold)
end end
def update_flagged_posts_count
PostAction.update_flagged_posts_count
end
def filter_quotes(parent_post=nil) def filter_quotes(parent_post=nil)
return cooked if parent_post.blank? return cooked if parent_post.blank?

View file

@ -13,7 +13,10 @@ class PostAction < ActiveRecord::Base
rate_limit :post_action_rate_limiter rate_limit :post_action_rate_limiter
def self.update_flagged_posts_count 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) $redis.set('posts_flagged_count', val)
admins = User.exec_sql("select id from users where admin = 't'").map{|r| r["id"].to_i} admins = User.exec_sql("select id from users where admin = 't'").map{|r| r["id"].to_i}

View file

@ -11,7 +11,9 @@ class Topic < ActiveRecord::Base
versioned :if => :new_version_required? versioned :if => :new_version_required?
acts_as_paranoid acts_as_paranoid
after_recover :update_flagged_posts_count
after_destroy :update_flagged_posts_count
rate_limit :default_rate_limiter rate_limit :default_rate_limiter
rate_limit :limit_topics_per_day rate_limit :limit_topics_per_day
rate_limit :limit_private_messages_per_day rate_limit :limit_private_messages_per_day
@ -387,6 +389,10 @@ class Topic < ActiveRecord::Base
topic topic
end 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 # Create the summary of the interesting posters in a topic. Cheats to avoid
# many queries. # many queries.

View file

@ -27,6 +27,19 @@ describe PostAction do
PostAction.flagged_posts_count.should == 0 PostAction.flagged_posts_count.should == 0
end 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 end
it "increases the post's bookmark count when saved" do it "increases the post's bookmark count when saved" do