Skip to content

Commit 36f8c9c

Browse files
committed
improve logic and performance on front page to avoid massive query
1 parent 279c3a3 commit 36f8c9c

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

lib/guardian.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,12 @@ def secure_category_ids
404404
@secure_category_ids ||= @user.secure_category_ids
405405
end
406406

407+
# all allowed category ids
408+
def allowed_category_ids
409+
unrestricted = Category.where(read_restricted: false).pluck(:id)
410+
unrestricted.concat(secure_category_ids)
411+
end
412+
407413
def topic_create_allowed_category_ids
408414
@topic_create_allowed_category_ids ||= @user.topic_create_allowed_category_ids
409415
end

lib/topic_query.rb

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -255,12 +255,13 @@ def default_results(options={})
255255
result = result.where('topics.id in (?)', options[:topic_ids]).references(:topics)
256256
end
257257

258-
unless @user && @user.moderator?
259-
category_ids = @user.secure_category_ids if @user
260-
if category_ids.present?
261-
result = result.where('categories.read_restricted IS NULL OR categories.read_restricted = ? OR categories.id IN (?)', false, category_ids).references(:categories)
258+
guardian = Guardian.new(@user)
259+
unless guardian.is_staff?
260+
allowed_ids = guardian.allowed_category_ids
261+
if allowed_ids.length > 0
262+
result = result.where('topics.category_id IS NULL or topics.category_id IN (?)', allowed_ids)
262263
else
263-
result = result.where('categories.read_restricted IS NULL OR categories.read_restricted = ?', false).references(:categories)
264+
result = result.where('topics.category_id IS NULL')
264265
end
265266
end
266267

0 commit comments

Comments
 (0)