home_controller.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. with_read_replica do
  8. @statuses = load_statuses
  9. @relationships = StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
  10. end
  11. render json: @statuses,
  12. each_serializer: REST::StatusSerializer,
  13. relationships: @relationships,
  14. status: account_home_feed.regenerating? ? 206 : 200
  15. end
  16. private
  17. def load_statuses
  18. cached_home_statuses
  19. end
  20. def cached_home_statuses
  21. cache_collection home_statuses, Status
  22. end
  23. def home_statuses
  24. account_home_feed.get(
  25. limit_param(DEFAULT_STATUSES_LIMIT),
  26. params[:max_id],
  27. params[:since_id],
  28. params[:min_id]
  29. )
  30. end
  31. def account_home_feed
  32. HomeFeed.new(current_account)
  33. end
  34. def insert_pagination_headers
  35. set_pagination_headers(next_path, prev_path)
  36. end
  37. def pagination_params(core_params)
  38. params.slice(:local, :limit).permit(:local, :limit).merge(core_params)
  39. end
  40. def next_path
  41. api_v1_timelines_home_url pagination_params(max_id: pagination_max_id)
  42. end
  43. def prev_path
  44. api_v1_timelines_home_url pagination_params(min_id: pagination_since_id)
  45. end
  46. def pagination_max_id
  47. @statuses.last.id
  48. end
  49. def pagination_since_id
  50. @statuses.first.id
  51. end
  52. end