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.

55 lines
1.4 KiB

  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 = if enabled?
  14. cache_collection(statuses_from_trends.offset(offset_param).limit(limit_param(DEFAULT_STATUSES_LIMIT)), Status)
  15. else
  16. []
  17. end
  18. end
  19. def statuses_from_trends
  20. scope = Trends.statuses.query.allowed.in_locale(content_locale)
  21. scope = scope.filtered_for(current_account) if user_signed_in?
  22. scope
  23. end
  24. def insert_pagination_headers
  25. set_pagination_headers(next_path, prev_path)
  26. end
  27. def pagination_params(core_params)
  28. params.slice(:limit).permit(:limit).merge(core_params)
  29. end
  30. def next_path
  31. api_v1_trends_statuses_url pagination_params(offset: offset_param + limit_param(DEFAULT_STATUSES_LIMIT)) if records_continue?
  32. end
  33. def prev_path
  34. api_v1_trends_statuses_url pagination_params(offset: offset_param - limit_param(DEFAULT_STATUSES_LIMIT)) if offset_param > limit_param(DEFAULT_STATUSES_LIMIT)
  35. end
  36. def offset_param
  37. params[:offset].to_i
  38. end
  39. def records_continue?
  40. @statuses.size == limit_param(DEFAULT_STATUSES_LIMIT)
  41. end
  42. end