2019-07-02 08:10:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Api::V1::Timelines::GroupController < Api::BaseController
|
|
|
|
before_action :set_group
|
2020-08-07 23:26:14 +01:00
|
|
|
before_action :set_sort_type
|
2019-07-02 08:10:25 +01:00
|
|
|
before_action :set_statuses
|
|
|
|
|
2020-08-11 04:22:08 +01:00
|
|
|
after_action :insert_pagination_headers, unless: -> {
|
2020-08-12 23:40:46 +01:00
|
|
|
@statuses.empty?
|
2020-08-11 04:22:08 +01:00
|
|
|
}
|
2019-07-02 08:10:25 +01:00
|
|
|
|
|
|
|
def show
|
2020-07-22 05:05:54 +01:00
|
|
|
if current_user
|
|
|
|
render json: @statuses,
|
|
|
|
each_serializer: REST::StatusSerializer,
|
2020-11-15 18:48:32 +00:00
|
|
|
group_id: params[:id],
|
2020-09-10 21:07:01 +01:00
|
|
|
relationships: StatusRelationshipsPresenter.new(@statuses, current_user.account_id, group_id: @group.id)
|
2020-07-22 05:05:54 +01:00
|
|
|
else
|
|
|
|
render json: @statuses, each_serializer: REST::StatusSerializer
|
|
|
|
end
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2020-08-07 23:26:14 +01:00
|
|
|
def set_sort_type
|
|
|
|
@sort_type = 'newest'
|
|
|
|
@sort_type = params[:sort_by] if [
|
2020-08-11 04:16:08 +01:00
|
|
|
'hot',
|
2020-08-07 23:26:14 +01:00
|
|
|
'newest',
|
|
|
|
'recent',
|
|
|
|
'top_today',
|
|
|
|
'top_weekly',
|
|
|
|
'top_monthly',
|
|
|
|
'top_yearly',
|
|
|
|
'top_all_time',
|
|
|
|
].include? params[:sort_by]
|
|
|
|
|
|
|
|
return @sort_type
|
|
|
|
end
|
|
|
|
|
2019-07-02 08:10:25 +01:00
|
|
|
def set_group
|
2020-07-22 05:05:54 +01:00
|
|
|
@group = Group.where(id: params[:id], is_archived: false).first
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_statuses
|
|
|
|
@statuses = cached_group_statuses
|
|
|
|
end
|
|
|
|
|
|
|
|
def cached_group_statuses
|
|
|
|
cache_collection group_statuses, Status
|
|
|
|
end
|
|
|
|
|
|
|
|
def group_statuses
|
2020-11-15 18:48:32 +00:00
|
|
|
SortingQueryBuilder.new.call(@sort_type, params[:max_id], @group)
|
2019-07-02 08:10:25 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def insert_pagination_headers
|
|
|
|
set_pagination_headers(next_path, prev_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pagination_params(core_params)
|
|
|
|
params.slice(:limit).permit(:limit).merge(core_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def next_path
|
|
|
|
api_v1_timelines_group_url params[:id], pagination_params(max_id: pagination_max_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def prev_path
|
|
|
|
api_v1_timelines_group_url params[:id], pagination_params(min_id: pagination_since_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pagination_max_id
|
|
|
|
@statuses.last.id
|
|
|
|
end
|
|
|
|
|
|
|
|
def pagination_since_id
|
|
|
|
@statuses.first.id
|
|
|
|
end
|
|
|
|
end
|