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.

79 lines
1.8 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. paginated_mutes.map(&:target_account)
  18. end
  19. def load_mutes
  20. paginated_mutes.includes(:account, :target_account).to_a
  21. end
  22. def paginated_mutes
  23. @paginated_mutes ||= Mute.eager_load(:target_account)
  24. .where(account: current_account)
  25. .paginate_by_max_id(
  26. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  27. params[:max_id],
  28. params[:since_id]
  29. )
  30. end
  31. def insert_pagination_headers
  32. set_pagination_headers(next_path, prev_path)
  33. end
  34. def next_path
  35. if records_continue?
  36. url_for pagination_params(max_id: pagination_max_id)
  37. end
  38. end
  39. def prev_path
  40. unless @data.empty?
  41. url_for pagination_params(since_id: pagination_since_id)
  42. end
  43. end
  44. def pagination_max_id
  45. if params[:action] == "details"
  46. @mutes.last.id
  47. else
  48. paginated_mutes.last.id
  49. end
  50. end
  51. def pagination_since_id
  52. if params[:action] == "details"
  53. @mutes.first.id
  54. else
  55. paginated_mutes.first.id
  56. end
  57. end
  58. def records_continue?
  59. @data.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  60. end
  61. def pagination_params(core_params)
  62. params.slice(:limit).permit(:limit).merge(core_params)
  63. end
  64. end