tag_controller.rb 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # frozen_string_literal: true
  2. class Api::V1::Timelines::TagController < Api::BaseController
  3. before_action -> { doorkeeper_authorize! :read, :'read:statuses' }, only: :show, if: :require_auth?
  4. before_action :load_tag
  5. after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
  6. def show
  7. cache_if_unauthenticated!
  8. @statuses = load_statuses
  9. render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
  10. end
  11. private
  12. def require_auth?
  13. !Setting.timeline_preview
  14. end
  15. def load_tag
  16. @tag = Tag.find_normalized(params[:id])
  17. end
  18. def load_statuses
  19. cached_tagged_statuses
  20. end
  21. def cached_tagged_statuses
  22. @tag.nil? ? [] : cache_collection(tag_timeline_statuses, Status)
  23. end
  24. def tag_timeline_statuses
  25. tag_feed.get(
  26. limit_param(DEFAULT_STATUSES_LIMIT),
  27. params[:max_id],
  28. params[:since_id],
  29. params[:min_id]
  30. )
  31. end
  32. def tag_feed
  33. TagFeed.new(
  34. @tag,
  35. current_account,
  36. any: params[:any],
  37. all: params[:all],
  38. none: params[:none],
  39. local: truthy_param?(:local),
  40. remote: truthy_param?(:remote),
  41. only_media: truthy_param?(:only_media)
  42. )
  43. end
  44. def insert_pagination_headers
  45. set_pagination_headers(next_path, prev_path)
  46. end
  47. def pagination_params(core_params)
  48. params.slice(:local, :limit, :only_media).permit(:local, :limit, :only_media).merge(core_params)
  49. end
  50. def next_path
  51. api_v1_timelines_tag_url params[:id], pagination_params(max_id: pagination_max_id)
  52. end
  53. def prev_path
  54. api_v1_timelines_tag_url params[:id], pagination_params(min_id: pagination_since_id)
  55. end
  56. def pagination_max_id
  57. @statuses.last.id
  58. end
  59. def pagination_since_id
  60. @statuses.first.id
  61. end
  62. end