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.

81 lines
1.7 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::MutesController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :follow, :'read:mutes' }
  4. before_action :require_user!
  5. after_action :insert_pagination_headers
  6. respond_to :json
  7. def index
  8. @data = @accounts = load_accounts
  9. render json: @accounts, each_serializer: REST::AccountSerializer
  10. end
  11. def details
  12. @data = @mutes = load_mutes
  13. render json: @mutes, each_serializer: REST::MuteSerializer
  14. end
  15. private
  16. def load_accounts
  17. default_accounts.merge(paginated_mutes).to_a
  18. end
  19. def default_accounts
  20. Account.includes(:muted_by).references(:muted_by)
  21. end
  22. def load_mutes
  23. paginated_mutes.includes(:account, :target_account).to_a
  24. end
  25. def paginated_mutes
  26. Mute.where(account: current_account).paginate_by_max_id(
  27. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  28. params[:max_id],
  29. params[:since_id]
  30. )
  31. end
  32. def insert_pagination_headers
  33. set_pagination_headers(next_path, prev_path)
  34. end
  35. def next_path
  36. if records_continue?
  37. url_for pagination_params(max_id: pagination_max_id)
  38. end
  39. end
  40. def prev_path
  41. unless@data.empty?
  42. url_for pagination_params(since_id: pagination_since_id)
  43. end
  44. end
  45. def pagination_max_id
  46. if params[:action] == "details"
  47. @mutes.last.id
  48. else
  49. @accounts.last.muted_by_ids.last
  50. end
  51. end
  52. def pagination_since_id
  53. if params[:action] == "details"
  54. @mutes.first.id
  55. else
  56. @accounts.first.muted_by_ids.first
  57. end
  58. end
  59. def records_continue?
  60. @data.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  61. end
  62. def pagination_params(core_params)
  63. params.slice(:limit).permit(:limit).merge(core_params)
  64. end
  65. end