statuses_controller.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # frozen_string_literal: true
  2. class Api::V1::Trends::StatusesController < Api::BaseController
  3. vary_by 'Authorization, Accept-Language'
  4. before_action :set_statuses
  5. after_action :insert_pagination_headers
  6. def index
  7. cache_if_unauthenticated!
  8. render json: @statuses, each_serializer: REST::StatusSerializer
  9. end
  10. private
  11. def enabled?
  12. Setting.trends
  13. end
  14. def set_statuses
  15. @statuses = if enabled?
  16. cache_collection(statuses_from_trends.offset(offset_param).limit(limit_param(DEFAULT_STATUSES_LIMIT)), Status)
  17. else
  18. []
  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