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.
 
 
 
 

79 lines
1.8 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Statuses::FavouritedByAccountsController < Api::BaseController
  3. include Authorization
  4. before_action -> { authorize_if_got_token! :read, :'read:accounts' }
  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. scope = default_accounts
  15. scope = scope.where.not(id: current_account.excluded_from_timeline_account_ids) unless current_account.nil?
  16. scope.merge(paginated_favourites).to_a
  17. end
  18. def default_accounts
  19. Account
  20. .includes(:favourites, :account_stat)
  21. .references(:favourites)
  22. .where(favourites: { status_id: @status.id })
  23. end
  24. def paginated_favourites
  25. Favourite.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. api_v1_status_favourited_by_index_url pagination_params(max_id: pagination_max_id)
  37. end
  38. end
  39. def prev_path
  40. unless @accounts.empty?
  41. api_v1_status_favourited_by_index_url pagination_params(since_id: pagination_since_id)
  42. end
  43. end
  44. def pagination_max_id
  45. @accounts.last.favourites.last.id
  46. end
  47. def pagination_since_id
  48. @accounts.first.favourites.first.id
  49. end
  50. def records_continue?
  51. @accounts.size == limit_param(DEFAULT_ACCOUNTS_LIMIT)
  52. end
  53. def set_status
  54. @status = Status.find(params[:status_id])
  55. authorize @status, :show?
  56. rescue Mastodon::NotPermittedError
  57. not_found
  58. end
  59. def pagination_params(core_params)
  60. params.slice(:limit).permit(:limit).merge(core_params)
  61. end
  62. end