2020-11-15 18:48:32 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-12-30 17:04:28 +00:00
|
|
|
class SortingQueryBuilder < BaseService
|
2021-01-23 08:13:47 +00:00
|
|
|
def call(sort_type, group = nil, page = 1, account_id = nil, source = nil)
|
2020-12-24 18:34:14 +00:00
|
|
|
limit = 20
|
|
|
|
|
|
|
|
min_likes = 5
|
|
|
|
min_reblogs = 2
|
|
|
|
min_replies = 1
|
2020-12-30 17:04:28 +00:00
|
|
|
date_limit = 5.years.ago
|
2021-01-24 16:29:57 +00:00
|
|
|
pure_limit = "NOW() - INTERVAL '14 days'"
|
2020-12-24 18:34:14 +00:00
|
|
|
max_page = 8
|
2020-11-15 18:48:32 +00:00
|
|
|
|
|
|
|
case sort_type
|
|
|
|
when 'hot'
|
|
|
|
date_limit = 8.hours.ago
|
2021-01-23 08:13:47 +00:00
|
|
|
pure_limit = "NOW() - INTERVAL '8 hours'"
|
2020-11-15 18:48:32 +00:00
|
|
|
when 'top_today'
|
|
|
|
date_limit = 24.hours.ago
|
2021-01-23 08:13:47 +00:00
|
|
|
pure_limit = "NOW() - INTERVAL '24 hours'"
|
2020-11-15 18:48:32 +00:00
|
|
|
when 'top_weekly'
|
|
|
|
date_limit = 7.days.ago
|
2021-01-23 08:13:47 +00:00
|
|
|
pure_limit = "NOW() - INTERVAL '7 days'"
|
2020-11-15 18:48:32 +00:00
|
|
|
when 'top_monthly'
|
|
|
|
date_limit = 30.days.ago
|
2021-01-23 08:13:47 +00:00
|
|
|
pure_limit = "NOW() - INTERVAL '30 days'"
|
2020-11-15 18:48:32 +00:00
|
|
|
when 'top_yearly'
|
|
|
|
date_limit = 1.year.ago
|
|
|
|
end
|
|
|
|
|
|
|
|
top_order = 'status_stats.favourites_count DESC, status_stats.reblogs_count DESC, status_stats.replies_count DESC'
|
|
|
|
valid_sort_types = [
|
|
|
|
'hot',
|
|
|
|
'newest',
|
|
|
|
'recent',
|
|
|
|
'top_today',
|
|
|
|
'top_weekly',
|
|
|
|
'top_monthly',
|
|
|
|
'top_yearly',
|
|
|
|
'top_all_time',
|
|
|
|
]
|
|
|
|
|
2020-12-24 18:34:14 +00:00
|
|
|
if page.to_i > max_page
|
|
|
|
return []
|
|
|
|
end
|
|
|
|
|
2021-01-23 08:13:47 +00:00
|
|
|
if source == 'group_collection'
|
|
|
|
return [] if account_id.nil?
|
|
|
|
query = "
|
|
|
|
select q.* from (
|
|
|
|
select s.*
|
|
|
|
from statuses s
|
2021-02-02 20:17:04 +00:00
|
|
|
join accounts a
|
|
|
|
on s.account_id = a.id
|
|
|
|
and a.is_flagged_as_spam is false
|
2021-01-23 08:13:47 +00:00
|
|
|
join group_accounts ga
|
|
|
|
on s.group_id = ga.group_id
|
|
|
|
and ga.account_id = #{account_id} "
|
|
|
|
query += "
|
|
|
|
join status_stats ss
|
|
|
|
on s.id = ss.status_id " if sort_type != 'newest'
|
|
|
|
query += "
|
|
|
|
where "
|
|
|
|
query += "
|
|
|
|
ss.updated_at > #{pure_limit} " if sort_type == 'recent'
|
|
|
|
query += "
|
|
|
|
s.created_at > #{pure_limit} " if sort_type != 'recent'
|
|
|
|
query += "
|
|
|
|
and s.reply is false
|
|
|
|
and s.reblog_of_id is null "
|
|
|
|
if sort_type != 'newest'
|
|
|
|
query += "
|
|
|
|
order by ss.favourites_count desc, ss.reblogs_count desc, ss.replies_count desc "
|
|
|
|
else
|
|
|
|
query += "
|
|
|
|
order by s.created_at desc "
|
|
|
|
end
|
|
|
|
|
|
|
|
query += "limit #{limit} "
|
|
|
|
if page.to_i > 1
|
|
|
|
query += "offset #{page.to_i * limit}"
|
|
|
|
end
|
|
|
|
|
|
|
|
query += "
|
|
|
|
) q
|
|
|
|
"
|
|
|
|
return Status.find_by_sql query
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-12-24 18:34:14 +00:00
|
|
|
if sort_type == 'newest'
|
2021-01-23 08:13:47 +00:00
|
|
|
|
2020-12-24 18:34:14 +00:00
|
|
|
query = Status.without_replies.without_reblogs
|
|
|
|
query = query.with_public_visibility if group.nil?
|
|
|
|
query = query.where('statuses.created_at > ?', date_limit)
|
2021-02-02 20:17:04 +00:00
|
|
|
query = query.joins(:account)
|
|
|
|
query = query.where('accounts.is_flagged_as_spam is false')
|
2021-01-25 07:35:10 +00:00
|
|
|
if source == "explore"
|
|
|
|
query = query.where(group: nil)
|
|
|
|
else
|
|
|
|
query = query.where(group: group) unless group.nil?
|
|
|
|
end
|
2020-12-24 18:34:14 +00:00
|
|
|
query = query.page(page.to_i)
|
|
|
|
query = query.per(limit)
|
|
|
|
return query
|
|
|
|
else
|
|
|
|
query = StatusStat.where('status_stats.created_at > ?', date_limit)
|
|
|
|
query = query.order(top_order) unless sort_type == 'recent'
|
|
|
|
query = query.order(updated_at: :desc) if sort_type == 'recent'
|
2020-12-24 18:41:00 +00:00
|
|
|
query = query.where('status_stats.replies_count > ?', min_replies) unless sort_type == 'recent'
|
|
|
|
query = query.where('status_stats.reblogs_count > ?', min_reblogs) unless sort_type == 'recent'
|
|
|
|
query = query.where('status_stats.favourites_count > ?', min_likes) unless sort_type == 'recent'
|
2021-02-02 20:17:04 +00:00
|
|
|
query = query.joins(:account)
|
|
|
|
query = query.where('accounts.is_flagged_as_spam is false')
|
2020-12-24 18:34:14 +00:00
|
|
|
query = query.joins(:status)
|
|
|
|
query = query.where('statuses.reblog_of_id IS NULL')
|
|
|
|
query = query.where('statuses.in_reply_to_id IS NULL')
|
2021-01-25 07:35:10 +00:00
|
|
|
if source == "explore"
|
|
|
|
query = query.where('statuses.group_id': nil)
|
|
|
|
else
|
|
|
|
query = query.where('statuses.group_id': group) unless group.nil?
|
|
|
|
end
|
2020-12-24 18:34:14 +00:00
|
|
|
query = query.where('statuses.visibility': 0) if group.nil?
|
|
|
|
query = query.page(page)
|
|
|
|
query = query.per(limit)
|
|
|
|
query = query.map(&:status)
|
|
|
|
return query
|
|
|
|
end
|
2020-11-15 18:48:32 +00:00
|
|
|
end
|
|
|
|
|
2020-12-30 17:04:28 +00:00
|
|
|
end
|