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.

91 lines
2.0 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Accounts::StatusesController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read }
  4. before_action :set_account
  5. after_action :insert_pagination_headers
  6. respond_to :json
  7. def index
  8. @statuses = load_statuses
  9. render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
  10. end
  11. private
  12. def set_account
  13. @account = Account.find(params[:account_id])
  14. end
  15. def load_statuses
  16. cached_account_statuses
  17. end
  18. def cached_account_statuses
  19. cache_collection account_statuses, Status
  20. end
  21. def account_statuses
  22. default_statuses.tap do |statuses|
  23. statuses.merge!(only_media_scope) if params[:only_media]
  24. statuses.merge!(no_replies_scope) if params[:exclude_replies]
  25. end
  26. end
  27. def default_statuses
  28. permitted_account_statuses.paginate_by_max_id(
  29. limit_param(DEFAULT_STATUSES_LIMIT),
  30. params[:max_id],
  31. params[:since_id]
  32. )
  33. end
  34. def permitted_account_statuses
  35. @account.statuses.permitted_for(@account, current_account)
  36. end
  37. def only_media_scope
  38. Status.where(id: account_media_status_ids)
  39. end
  40. def account_media_status_ids
  41. @account.media_attachments.attached.reorder(nil).select(:status_id).distinct
  42. end
  43. def no_replies_scope
  44. Status.without_replies
  45. end
  46. def pagination_params(core_params)
  47. params.permit(:limit, :only_media, :exclude_replies).merge(core_params)
  48. end
  49. def insert_pagination_headers
  50. set_pagination_headers(next_path, prev_path)
  51. end
  52. def next_path
  53. if records_continue?
  54. api_v1_account_statuses_url pagination_params(max_id: pagination_max_id)
  55. end
  56. end
  57. def prev_path
  58. unless @statuses.empty?
  59. api_v1_account_statuses_url pagination_params(since_id: pagination_since_id)
  60. end
  61. end
  62. def records_continue?
  63. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  64. end
  65. def pagination_max_id
  66. @statuses.last.id
  67. end
  68. def pagination_since_id
  69. @statuses.first.id
  70. end
  71. end