You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.5 KiB

4 years ago
4 years ago
  1. # frozen_string_literal: true
  2. class Api::V1::Timelines::HomeController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read, :'read:statuses' }, only: [:show]
  4. before_action :require_user!, only: [:show]
  5. after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
  6. def show
  7. @statuses = load_statuses
  8. min_id = @statuses.empty? ? 0 : [@statuses[0].id, @statuses[-1].id].min
  9. tags_statuses = []
  10. current_account.featured_tags.each do |tag|
  11. @tag = tag
  12. tags_statuses += (tags_statuses + load_tag_statuses).uniq(&:id)
  13. end
  14. if params_slice(:since_id, :min_id).empty?
  15. tags_statuses = tags_statuses.select{|tag| tag.id > min_id}
  16. end
  17. @statuses = (@statuses + tags_statuses).uniq(&:id).sort_by(&:id)
  18. if params_slice(:min_id).empty?
  19. @statuses = @statuses.reverse!
  20. end
  21. render json: @statuses,
  22. each_serializer: REST::StatusSerializer,
  23. relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id),
  24. status: account_home_feed.regenerating? ? 206 : 200
  25. end
  26. private
  27. def load_tag_statuses
  28. cached_tagged_statuses
  29. end
  30. def cached_tagged_statuses
  31. cache_collection tagged_statuses, Status
  32. end
  33. def tagged_statuses
  34. if @tag.nil?
  35. []
  36. else
  37. statuses = tag_timeline_statuses.paginate_by_id(
  38. limit_param(DEFAULT_STATUSES_LIMIT),
  39. params_slice(:max_id, :since_id, :min_id)
  40. )
  41. end
  42. end
  43. def tag_timeline_statuses
  44. HashtagQueryService.new.call(@tag, params.slice(:any, :all, :none), current_account, truthy_param?(:local))
  45. end
  46. def load_statuses
  47. cached_home_statuses
  48. end
  49. def cached_home_statuses
  50. cache_collection home_statuses, Status
  51. end
  52. def home_statuses
  53. account_home_feed.get(
  54. limit_param(DEFAULT_STATUSES_LIMIT),
  55. params[:max_id],
  56. params[:since_id],
  57. params[:min_id]
  58. )
  59. end
  60. def account_home_feed
  61. HomeFeed.new(current_account)
  62. end
  63. def insert_pagination_headers
  64. set_pagination_headers(next_path, prev_path)
  65. end
  66. def pagination_params(core_params)
  67. params.slice(:local, :limit).permit(:local, :limit).merge(core_params)
  68. end
  69. def next_path
  70. api_v1_timelines_home_url pagination_params(max_id: pagination_max_id)
  71. end
  72. def prev_path
  73. api_v1_timelines_home_url pagination_params(min_id: pagination_since_id)
  74. end
  75. def pagination_max_id
  76. @statuses.last.id
  77. end
  78. def pagination_since_id
  79. @statuses.first.id
  80. end
  81. end