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.

92 lines
1.9 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Accounts::StatusesController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read }
  4. before_action :set_account
  5. after_action :insert_pagination_headers
  6. respond_to :json
  7. def index
  8. @statuses = load_statuses
  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.tap do |statuses|
  16. set_maps(statuses)
  17. end
  18. end
  19. def cached_account_statuses
  20. cache_collection account_statuses, Status
  21. end
  22. def account_statuses
  23. default_statuses.tap do |statuses|
  24. statuses.merge!(only_media_scope) if params[:only_media]
  25. statuses.merge!(no_replies_scope) if params[:exclude_replies]
  26. end
  27. end
  28. def default_statuses
  29. permitted_account_statuses.paginate_by_max_id(
  30. limit_param(DEFAULT_STATUSES_LIMIT),
  31. params[:max_id],
  32. params[:since_id]
  33. )
  34. end
  35. def permitted_account_statuses
  36. @account.statuses.permitted_for(@account, current_account)
  37. end
  38. def only_media_scope
  39. Status.where(id: account_media_status_ids)
  40. end
  41. def account_media_status_ids
  42. @account.media_attachments.attached.reorder(nil).select(:status_id).distinct
  43. end
  44. def no_replies_scope
  45. Status.without_replies
  46. end
  47. def pagination_params(core_params)
  48. params.permit(:limit, :only_media, :exclude_replies).merge(core_params)
  49. end
  50. def insert_pagination_headers
  51. set_pagination_headers(next_path, prev_path)
  52. end
  53. def next_path
  54. if records_continue?
  55. api_v1_account_statuses_url pagination_params(max_id: pagination_max_id)
  56. end
  57. end
  58. def prev_path
  59. unless @statuses.empty?
  60. api_v1_account_statuses_url pagination_params(since_id: pagination_since_id)
  61. end
  62. end
  63. def records_continue?
  64. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  65. end
  66. def pagination_max_id
  67. @statuses.last.id
  68. end
  69. def pagination_since_id
  70. @statuses.first.id
  71. end
  72. end