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.3 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Trends::TagsController < Api::BaseController
  3. before_action :set_tags
  4. after_action :insert_pagination_headers
  5. DEFAULT_TAGS_LIMIT = 10
  6. def index
  7. render json: @tags, each_serializer: REST::TagSerializer, relationships: TagRelationshipsPresenter.new(@tags, current_user&.account_id)
  8. end
  9. private
  10. def enabled?
  11. Setting.trends
  12. end
  13. def set_tags
  14. @tags = if enabled?
  15. tags_from_trends.offset(offset_param).limit(limit_param(DEFAULT_TAGS_LIMIT))
  16. else
  17. []
  18. end
  19. end
  20. def tags_from_trends
  21. Trends.tags.query.allowed
  22. end
  23. def insert_pagination_headers
  24. set_pagination_headers(next_path, prev_path)
  25. end
  26. def pagination_params(core_params)
  27. params.slice(:limit).permit(:limit).merge(core_params)
  28. end
  29. def next_path
  30. api_v1_trends_tags_url pagination_params(offset: offset_param + limit_param(DEFAULT_TAGS_LIMIT)) if records_continue?
  31. end
  32. def prev_path
  33. api_v1_trends_tags_url pagination_params(offset: offset_param - limit_param(DEFAULT_TAGS_LIMIT)) if offset_param > limit_param(DEFAULT_TAGS_LIMIT)
  34. end
  35. def offset_param
  36. params[:offset].to_i
  37. end
  38. def records_continue?
  39. @tags.size == limit_param(DEFAULT_TAGS_LIMIT)
  40. end
  41. end