2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-09-06 10:50:21 +08:00

improve logic and performance on front page to avoid massive query

This commit is contained in:
Sam 2013-09-10 16:02:54 +10:00
parent 279c3a3add
commit 36f8c9c45b
2 changed files with 12 additions and 5 deletions

View file

@ -404,6 +404,12 @@ class Guardian
@secure_category_ids ||= @user.secure_category_ids @secure_category_ids ||= @user.secure_category_ids
end end
# all allowed category ids
def allowed_category_ids
unrestricted = Category.where(read_restricted: false).pluck(:id)
unrestricted.concat(secure_category_ids)
end
def topic_create_allowed_category_ids def topic_create_allowed_category_ids
@topic_create_allowed_category_ids ||= @user.topic_create_allowed_category_ids @topic_create_allowed_category_ids ||= @user.topic_create_allowed_category_ids
end end

View file

@ -255,12 +255,13 @@ class TopicQuery
result = result.where('topics.id in (?)', options[:topic_ids]).references(:topics) result = result.where('topics.id in (?)', options[:topic_ids]).references(:topics)
end end
unless @user && @user.moderator? guardian = Guardian.new(@user)
category_ids = @user.secure_category_ids if @user unless guardian.is_staff?
if category_ids.present? allowed_ids = guardian.allowed_category_ids
result = result.where('categories.read_restricted IS NULL OR categories.read_restricted = ? OR categories.id IN (?)', false, category_ids).references(:categories) if allowed_ids.length > 0
result = result.where('topics.category_id IS NULL or topics.category_id IN (?)', allowed_ids)
else else
result = result.where('categories.read_restricted IS NULL OR categories.read_restricted = ?', false).references(:categories) result = result.where('topics.category_id IS NULL')
end end
end end