2018-06-05 15:29:17 +08:00
|
|
|
# frozen_string_literal: true
|
2013-11-14 01:26:32 +08:00
|
|
|
#
|
|
|
|
# SQL fragments used when querying a list of topics.
|
|
|
|
#
|
|
|
|
module TopicQuerySQL
|
|
|
|
|
|
|
|
class << self
|
|
|
|
|
|
|
|
def lowest_date
|
2015-01-19 22:00:55 +08:00
|
|
|
"1900-01-01"
|
2013-11-14 01:26:32 +08:00
|
|
|
end
|
|
|
|
|
2014-01-19 02:03:09 +08:00
|
|
|
def order_by_category_sql(dir)
|
2018-06-05 15:29:17 +08:00
|
|
|
-"CASE WHEN categories.id = #{SiteSetting.uncategorized_category_id.to_i} THEN '' ELSE categories.name END #{dir}"
|
2014-01-19 02:03:09 +08:00
|
|
|
end
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
# If you've cleared the pin, use bumped_at, otherwise put it at the top
|
2013-11-14 01:26:32 +08:00
|
|
|
def order_with_pinned_sql
|
2018-06-05 15:29:17 +08:00
|
|
|
-"CASE
|
2013-11-14 01:26:32 +08:00
|
|
|
WHEN (COALESCE(topics.pinned_at, '#{lowest_date}') > COALESCE(tu.cleared_pinned_at, '#{lowest_date}'))
|
2015-01-07 07:37:30 +08:00
|
|
|
THEN topics.pinned_at + interval '9999 years'
|
|
|
|
ELSE topics.bumped_at
|
2013-11-14 01:26:32 +08:00
|
|
|
END DESC"
|
|
|
|
end
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
# If you've cleared the pin, use bumped_at, otherwise put it at the top
|
2013-11-14 01:26:32 +08:00
|
|
|
def order_nocategory_with_pinned_sql
|
2018-06-05 15:29:17 +08:00
|
|
|
-"CASE
|
2015-01-07 07:37:30 +08:00
|
|
|
WHEN topics.pinned_globally
|
|
|
|
AND (COALESCE(topics.pinned_at, '#{lowest_date}') > COALESCE(tu.cleared_pinned_at, '#{lowest_date}'))
|
|
|
|
THEN topics.pinned_at + interval '9999 years'
|
|
|
|
ELSE topics.bumped_at
|
2013-11-14 01:26:32 +08:00
|
|
|
END DESC"
|
|
|
|
end
|
|
|
|
|
2014-01-19 02:03:09 +08:00
|
|
|
def order_basic_bumped
|
|
|
|
"CASE WHEN (topics.pinned_at IS NOT NULL) THEN 0 ELSE 1 END, topics.bumped_at DESC"
|
|
|
|
end
|
|
|
|
|
2013-11-14 01:26:32 +08:00
|
|
|
def order_nocategory_basic_bumped
|
2015-01-07 07:37:30 +08:00
|
|
|
"CASE WHEN topics.pinned_globally AND (topics.pinned_at IS NOT NULL) THEN 0 ELSE 1 END, topics.bumped_at DESC"
|
2013-11-14 01:26:32 +08:00
|
|
|
end
|
|
|
|
|
2014-01-19 02:03:09 +08:00
|
|
|
def order_top_for(score)
|
2018-06-05 15:29:17 +08:00
|
|
|
-"COALESCE(top_topics.#{score}, 0) DESC, topics.bumped_at DESC"
|
2014-01-19 02:03:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def order_top_with_pinned_category_for(score)
|
|
|
|
# display pinned topics first
|
2018-06-05 15:29:17 +08:00
|
|
|
-"CASE WHEN (COALESCE(topics.pinned_at, '#{lowest_date}') > COALESCE(tu.cleared_pinned_at, '#{lowest_date}')) THEN 0 ELSE 1 END,
|
2014-01-19 02:03:09 +08:00
|
|
|
top_topics.#{score} DESC,
|
|
|
|
topics.bumped_at DESC"
|
2013-11-14 01:26:32 +08:00
|
|
|
end
|
|
|
|
|
2016-08-16 04:16:04 +08:00
|
|
|
def order_top_with_notification_levels(score)
|
2018-06-05 15:29:17 +08:00
|
|
|
-"COALESCE(topic_users.notification_level, 1) DESC, COALESCE(category_users.notification_level, 1) DESC, COALESCE(top_topics.#{score}, 0) DESC, topics.bumped_at DESC"
|
2016-08-16 04:16:04 +08:00
|
|
|
end
|
|
|
|
|
2013-11-14 01:26:32 +08:00
|
|
|
end
|
|
|
|
end
|