statuses_controller.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # frozen_string_literal: true
  2. class Api::V1::Trends::StatusesController < Api::BaseController
  3. before_action :set_statuses
  4. after_action :insert_pagination_headers
  5. def index
  6. render json: @statuses, each_serializer: REST::StatusSerializer
  7. end
  8. private
  9. def enabled?
  10. Setting.trends
  11. end
  12. def set_statuses
  13. @statuses = begin
  14. if enabled?
  15. cache_collection(statuses_from_trends.offset(offset_param).limit(limit_param(DEFAULT_STATUSES_LIMIT)), Status)
  16. else
  17. []
  18. end
  19. end
  20. end
  21. def statuses_from_trends
  22. scope = Trends.statuses.query.allowed.in_locale(content_locale)
  23. scope = scope.filtered_for(current_account) if user_signed_in?
  24. scope
  25. end
  26. def insert_pagination_headers
  27. set_pagination_headers(next_path, prev_path)
  28. end
  29. def pagination_params(core_params)
  30. params.slice(:limit).permit(:limit).merge(core_params)
  31. end
  32. def next_path
  33. api_v1_trends_statuses_url pagination_params(offset: offset_param + limit_param(DEFAULT_STATUSES_LIMIT)) if records_continue?
  34. end
  35. def prev_path
  36. api_v1_trends_statuses_url pagination_params(offset: offset_param - limit_param(DEFAULT_STATUSES_LIMIT)) if offset_param > limit_param(DEFAULT_STATUSES_LIMIT)
  37. end
  38. def offset_param
  39. params[:offset].to_i
  40. end
  41. def records_continue?
  42. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  43. end
  44. end