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.

103 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. @account.suspended? ? [] : cached_account_statuses
  16. end
  17. def cached_account_statuses
  18. statuses = truthy_param?(:pinned) ? pinned_scope : permitted_account_statuses
  19. statuses.merge!(only_media_scope) if truthy_param?(:only_media)
  20. statuses.merge!(no_replies_scope) if truthy_param?(:exclude_replies)
  21. statuses.merge!(no_reblogs_scope) if truthy_param?(:exclude_reblogs)
  22. statuses.merge!(hashtag_scope) if params[:tagged].present?
  23. cache_collection_paginated_by_id(
  24. statuses,
  25. Status,
  26. limit_param(DEFAULT_STATUSES_LIMIT),
  27. params_slice(:max_id, :since_id, :min_id)
  28. )
  29. end
  30. def permitted_account_statuses
  31. @account.statuses.permitted_for(@account, current_account)
  32. end
  33. def only_media_scope
  34. Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
  35. end
  36. def pinned_scope
  37. return Status.none if @account.blocking?(current_account)
  38. @account.pinned_statuses
  39. end
  40. def no_replies_scope
  41. Status.without_replies
  42. end
  43. def no_reblogs_scope
  44. Status.without_reblogs
  45. end
  46. def hashtag_scope
  47. tag = Tag.find_normalized(params[:tagged])
  48. if tag
  49. Status.tagged_with(tag.id)
  50. else
  51. Status.none
  52. end
  53. end
  54. def pagination_params(core_params)
  55. params.slice(:limit, :only_media, :exclude_replies).permit(:limit, :only_media, :exclude_replies).merge(core_params)
  56. end
  57. def insert_pagination_headers
  58. set_pagination_headers(next_path, prev_path)
  59. end
  60. def next_path
  61. if records_continue?
  62. api_v1_account_statuses_url pagination_params(max_id: pagination_max_id)
  63. end
  64. end
  65. def prev_path
  66. unless @statuses.empty?
  67. api_v1_account_statuses_url pagination_params(min_id: pagination_since_id)
  68. end
  69. end
  70. def records_continue?
  71. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  72. end
  73. def pagination_max_id
  74. @statuses.last.id
  75. end
  76. def pagination_since_id
  77. @statuses.first.id
  78. end
  79. end