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.

112 lines
3.1 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.where(id: account_media_status_ids)
  33. end
  34. def account_media_status_ids
  35. # `SELECT DISTINCT id, updated_at` is too slow, so pluck ids at first, and then select id, updated_at with ids.
  36. # Also, Avoid getting slow by not narrowing down by `statuses.account_id`.
  37. # When narrowing down by `statuses.account_id`, `index_statuses_20180106` will be used
  38. # and the table will be joined by `Merge Semi Join`, so the query will be slow.
  39. @account.statuses.joins(:media_attachments).merge(@account.media_attachments).permitted_for(@account, current_account)
  40. .paginate_by_max_id(limit_param(DEFAULT_STATUSES_LIMIT), params[:max_id], params[:since_id])
  41. .reorder(id: :desc).distinct(:id).pluck(:id)
  42. end
  43. def pinned_scope
  44. return Status.none if @account.blocking?(current_account)
  45. @account.pinned_statuses
  46. end
  47. def no_replies_scope
  48. Status.without_replies
  49. end
  50. def no_reblogs_scope
  51. Status.without_reblogs
  52. end
  53. def hashtag_scope
  54. tag = Tag.find_normalized(params[:tagged])
  55. if tag
  56. Status.tagged_with(tag.id)
  57. else
  58. Status.none
  59. end
  60. end
  61. def pagination_params(core_params)
  62. params.slice(:limit, :only_media, :exclude_replies).permit(:limit, :only_media, :exclude_replies).merge(core_params)
  63. end
  64. def insert_pagination_headers
  65. set_pagination_headers(next_path, prev_path)
  66. end
  67. def next_path
  68. if records_continue?
  69. api_v1_account_statuses_url pagination_params(max_id: pagination_max_id)
  70. end
  71. end
  72. def prev_path
  73. unless @statuses.empty?
  74. api_v1_account_statuses_url pagination_params(min_id: pagination_since_id)
  75. end
  76. end
  77. def records_continue?
  78. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  79. end
  80. def pagination_max_id
  81. @statuses.last.id
  82. end
  83. def pagination_since_id
  84. @statuses.first.id
  85. end
  86. end