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.

122 lines
3.3 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. before_action :check_account_suspension
  6. before_action :check_account_block
  7. after_action :insert_pagination_headers
  8. respond_to :json
  9. def index
  10. @statuses = load_statuses
  11. render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
  12. end
  13. private
  14. def set_account
  15. @account = Account.find(params[:account_id])
  16. end
  17. def check_account_suspension
  18. gone if @account.suspended?
  19. end
  20. def check_account_block
  21. gone if current_account.present? && @account.blocking?(current_account)
  22. end
  23. def load_statuses
  24. cached_account_statuses
  25. end
  26. def cached_account_statuses
  27. cache_collection account_statuses, Status
  28. end
  29. def account_statuses
  30. statuses = truthy_param?(:pinned) ? pinned_scope : permitted_account_statuses
  31. statuses = statuses.paginate_by_id(limit_param(DEFAULT_STATUSES_LIMIT), params_slice(:max_id, :since_id, :min_id))
  32. statuses.merge!(only_media_scope) if truthy_param?(:only_media)
  33. statuses.merge!(no_replies_scope) if truthy_param?(:exclude_replies)
  34. statuses.merge!(no_reblogs_scope) if truthy_param?(:exclude_reblogs)
  35. statuses.merge!(hashtag_scope) if params[:tagged].present?
  36. statuses
  37. end
  38. def permitted_account_statuses
  39. @account.statuses.permitted_for(@account, current_account)
  40. end
  41. def only_media_scope
  42. Status.where(id: account_media_status_ids)
  43. end
  44. def account_media_status_ids
  45. # `SELECT DISTINCT id, updated_at` is too slow, so pluck ids at first, and then select id, updated_at with ids.
  46. # Also, Avoid getting slow by not narrowing down by `statuses.account_id`.
  47. # When narrowing down by `statuses.account_id`, `index_statuses_20180106` will be used
  48. # and the table will be joined by `Merge Semi Join`, so the query will be slow.
  49. @account.statuses.joins(:media_attachments).merge(@account.media_attachments).permitted_for(@account, current_account)
  50. .paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id])
  51. .reorder(id: :desc).distinct(:id).pluck(:id)
  52. end
  53. def pinned_scope
  54. @account.pinned_statuses
  55. end
  56. def no_replies_scope
  57. Status.without_replies
  58. end
  59. def no_reblogs_scope
  60. Status.without_reblogs
  61. end
  62. def hashtag_scope
  63. tag = Tag.find_normalized(params[:tagged])
  64. if tag
  65. Status.tagged_with(tag.id)
  66. else
  67. Status.none
  68. end
  69. end
  70. def pagination_params(core_params)
  71. params.slice(:limit, :only_media, :exclude_replies).permit(:limit, :only_media, :exclude_replies).merge(core_params)
  72. end
  73. def insert_pagination_headers
  74. set_pagination_headers(next_path, prev_path)
  75. end
  76. def next_path
  77. if records_continue?
  78. api_v1_account_statuses_url pagination_params(max_id: pagination_max_id)
  79. end
  80. end
  81. def prev_path
  82. unless @statuses.empty?
  83. api_v1_account_statuses_url pagination_params(min_id: pagination_since_id)
  84. end
  85. end
  86. def records_continue?
  87. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  88. end
  89. def pagination_max_id
  90. @statuses.last.id
  91. end
  92. def pagination_since_id
  93. @statuses.first.id
  94. end
  95. end