logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

favourited_by_accounts_controller.rb (1955B)


  1. # frozen_string_literal: true
  2. class Api::V1::Statuses::FavouritedByAccountsController < Api::BaseController
  3. include Authorization
  4. before_action :authorize_if_got_token
  5. before_action :set_status
  6. after_action :insert_pagination_headers
  7. respond_to :json
  8. def index
  9. @accounts = load_accounts
  10. render json: @accounts, each_serializer: REST::AccountSerializer
  11. end
  12. private
  13. def load_accounts
  14. default_accounts.merge(paginated_favourites).to_a
  15. end
  16. def default_accounts
  17. Account
  18. .includes(:favourites)
  19. .references(:favourites)
  20. .where(favourites: { status_id: @status.id })
  21. end
  22. def paginated_favourites
  23. Favourite.paginate_by_max_id(
  24. limit_param(DEFAULT_ACCOUNTS_LIMIT),
  25. params[:max_id],
  26. params[:since_id]
  27. )
  28. end
  29. def insert_pagination_headers
  30. set_pagination_headers(next_path, prev_path)
  31. end
  32. def next_path
  33. if records_continue?
  34. api_v1_status_favourited_by_index_url pagination_params(max_id: pagination_max_id)
  35. end
  36. end
  37. def prev_path
  38. unless @accounts.empty?
  39. api_v1_status_favourited_by_index_url pagination_params(since_id: pagination_since_id)
  40. end
  41. end
  42. def pagination_max_id
  43. @accounts.last.favourites.last.id
  44. end
  45. def pagination_since_id
  46. @accounts.first.favourites.first.id
  47. end
  48. def records_continue?
  49. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  50. end
  51. def set_status
  52. @status = Status.find(params[:status_id])
  53. authorize @status, :show?
  54. rescue Mastodon::NotPermittedError
  55. # Reraise in order to get a 404 instead of a 403 error code
  56. raise ActiveRecord::RecordNotFound
  57. end
  58. def authorize_if_got_token
  59. request_token = Doorkeeper::OAuth::Token.from_request(request, *Doorkeeper.configuration.access_token_methods)
  60. doorkeeper_authorize! :read if request_token
  61. end
  62. def pagination_params(core_params)
  63. params.slice(:limit).permit(:limit).merge(core_params)
  64. end
  65. end