mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 03:51:07 +08:00
TopicQuery cleanup in advance of custom sorting:
- Move SQL method constants into a module - Removed unused count methods - Moved methods that don't return a TopicList into Topic - Replaced some confusing method signatures
This commit is contained in:
72
lib/topic_query_sql.rb
Normal file
72
lib/topic_query_sql.rb
Normal file
@ -0,0 +1,72 @@
|
||||
#
|
||||
# SQL fragments used when querying a list of topics.
|
||||
#
|
||||
module TopicQuerySQL
|
||||
|
||||
class << self
|
||||
|
||||
# use the constants in conjuction with COALESCE to determine the order with regard to pinned
|
||||
# topics that have been cleared by the user. There
|
||||
# might be a cleaner way to do this.
|
||||
def lowest_date
|
||||
"2010-01-01"
|
||||
end
|
||||
|
||||
def highest_date
|
||||
"3000-01-01"
|
||||
end
|
||||
|
||||
# If you've clearned the pin, use bumped_at, otherwise put it at the top
|
||||
def order_with_pinned_sql
|
||||
"CASE
|
||||
WHEN (COALESCE(topics.pinned_at, '#{lowest_date}') > COALESCE(tu.cleared_pinned_at, '#{lowest_date}'))
|
||||
THEN '#{highest_date}'
|
||||
ELSE topics.bumped_at
|
||||
END DESC"
|
||||
end
|
||||
|
||||
def order_hotness(user)
|
||||
if user
|
||||
# When logged in take into accounts what pins you've closed
|
||||
"CASE
|
||||
WHEN (COALESCE(topics.pinned_at, '#{lowest_date}') > COALESCE(tu.cleared_pinned_at, '#{lowest_date}'))
|
||||
THEN 100
|
||||
ELSE hot_topics.score + (COALESCE(categories.hotness, 5.0) / 11.0)
|
||||
END DESC"
|
||||
else
|
||||
# When anonymous, don't use topic_user
|
||||
"CASE
|
||||
WHEN topics.pinned_at IS NOT NULL THEN 100
|
||||
ELSE hot_topics.score + (COALESCE(categories.hotness, 5.0) / 11.0)
|
||||
END DESC"
|
||||
end
|
||||
end
|
||||
|
||||
# If you've clearned the pin, use bumped_at, otherwise put it at the top
|
||||
def order_nocategory_with_pinned_sql
|
||||
"CASE
|
||||
WHEN topics.category_id = #{SiteSetting.uncategorized_category_id.to_i} and (COALESCE(topics.pinned_at, '#{lowest_date}') > COALESCE(tu.cleared_pinned_at, '#{lowest_date}'))
|
||||
THEN '#{highest_date}'
|
||||
ELSE topics.bumped_at
|
||||
END DESC"
|
||||
end
|
||||
|
||||
# For anonymous users
|
||||
def order_nocategory_basic_bumped
|
||||
"CASE WHEN topics.category_id = #{SiteSetting.uncategorized_category_id.to_i} and (topics.pinned_at IS NOT NULL) THEN 0 ELSE 1 END, topics.bumped_at DESC"
|
||||
end
|
||||
|
||||
def order_basic_bumped
|
||||
"CASE WHEN (topics.pinned_at IS NOT NULL) THEN 0 ELSE 1 END, topics.bumped_at DESC"
|
||||
end
|
||||
|
||||
def order_with_pinned_sql
|
||||
"CASE
|
||||
WHEN (COALESCE(topics.pinned_at, '#{lowest_date}') > COALESCE(tu.cleared_pinned_at, '#{lowest_date}'))
|
||||
THEN '#{highest_date}'
|
||||
ELSE topics.bumped_at
|
||||
END DESC"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
Reference in New Issue
Block a user