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.

102 lines
2.5 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Accounts::StatusesController < Api::BaseController
  3. before_action -> { authorize_if_got_token! :read, :'read:statuses' }
  4. before_action :set_account
  5. after_action :insert_pagination_headers, unless: -> { truthy_param?(:pinned) }
  6. def index
  7. @statuses = load_statuses
  8. render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
  9. end
  10. private
  11. def set_account
  12. @account = Account.find(params[:account_id])
  13. end
  14. def load_statuses
  15. cached_account_statuses
  16. end
  17. def cached_account_statuses
  18. cache_collection account_statuses, Status
  19. end
  20. def account_statuses
  21. statuses = truthy_param?(:pinned) ? pinned_scope : permitted_account_statuses
  22. statuses.merge!(only_media_scope) if truthy_param?(:only_media)
  23. statuses.merge!(no_replies_scope) if truthy_param?(:exclude_replies)
  24. statuses.merge!(no_reblogs_scope) if truthy_param?(:exclude_reblogs)
  25. statuses.merge!(hashtag_scope) if params[:tagged].present?
  26. statuses.paginate_by_id(limit_param(DEFAULT_STATUSES_LIMIT), params_slice(:max_id, :since_id, :min_id))
  27. end
  28. def permitted_account_statuses
  29. @account.statuses.permitted_for(@account, current_account)
  30. end
  31. def only_media_scope
  32. Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
  33. end
  34. def pinned_scope
  35. return Status.none if @account.blocking?(current_account)
  36. @account.pinned_statuses
  37. end
  38. def no_replies_scope
  39. Status.without_replies
  40. end
  41. def no_reblogs_scope
  42. Status.without_reblogs
  43. end
  44. def hashtag_scope
  45. tag = Tag.find_normalized(params[:tagged])
  46. if tag
  47. Status.tagged_with(tag.id)
  48. else
  49. Status.none
  50. end
  51. end
  52. def pagination_params(core_params)
  53. params.slice(:limit, :only_media, :exclude_replies).permit(:limit, :only_media, :exclude_replies).merge(core_params)
  54. end
  55. def insert_pagination_headers
  56. set_pagination_headers(next_path, prev_path)
  57. end
  58. def next_path
  59. if records_continue?
  60. api_v1_account_statuses_url pagination_params(max_id: pagination_max_id)
  61. end
  62. end
  63. def prev_path
  64. unless @statuses.empty?
  65. api_v1_account_statuses_url pagination_params(min_id: pagination_since_id)
  66. end
  67. end
  68. def records_continue?
  69. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  70. end
  71. def pagination_max_id
  72. @statuses.last.id
  73. end
  74. def pagination_since_id
  75. @statuses.first.id
  76. end
  77. end