The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

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