FIX: prevent DOS when fixing avatar in quotes

This commit is contained in:
Régis Hanol 2015-04-24 11:14:10 +02:00
parent a6ea9bd05c
commit 80b2935e11
3 changed files with 18 additions and 13 deletions

View file

@ -1,10 +0,0 @@
module Jobs
class FixAvatarInQuotes < Jobs::Base
def execute(args)
post_ids_to_rebake = Post.exec_sql("SELECT post_id FROM quoted_posts WHERE quoted_post_id IN (SELECT id FROM posts WHERE user_id = ?)", args[:user_id]).values.flatten.map(&:to_i)
Post.where(id: post_ids_to_rebake).find_each.map(&:rebake!)
end
end
end

View file

@ -525,6 +525,22 @@ class Post < ActiveRecord::Base
end
end
def self.rebake_all_quoted_posts(user_id)
return if user_id.blank?
Post.exec_sql <<-SQL
WITH user_quoted_posts AS (
SELECT post_id
FROM quoted_posts
WHERE quoted_post_id IN (SELECT id FROM posts WHERE user_id = #{user_id})
)
UPDATE posts
SET baked_version = NULL
WHERE baked_version IS NOT NULL
AND id IN (SELECT post_id FROM user_quoted_posts)
SQL
end
private
def parse_quote_into_arguments(quote)

View file

@ -677,9 +677,8 @@ class User < ActiveRecord::Base
Jobs.enqueue(:update_gravatar, user_id: self.id, avatar_id: avatar.id)
end
if self.uploaded_avatar_id_changed?
Jobs.enqueue(:fix_avatar_in_quotes, user_id: self.id)
end
# mark all the user's quoted posts as "needing a rebake"
Post.rebake_all_quoted_posts(self.id) if self.uploaded_avatar_id_changed?
end
def first_post_created_at