The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

75 lines
1.8 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Timelines::TagController < Api::BaseController
  3. before_action :load_tag
  4. after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
  5. respond_to :json
  6. def show
  7. @statuses = load_statuses
  8. render json: @statuses, each_serializer: REST::StatusSerializer, relationships: StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
  9. end
  10. private
  11. def load_tag
  12. @tag = Tag.find_normalized(params[:id])
  13. end
  14. def load_statuses
  15. cached_tagged_statuses
  16. end
  17. def cached_tagged_statuses
  18. cache_collection tagged_statuses, Status
  19. end
  20. def tagged_statuses
  21. if @tag.nil?
  22. []
  23. else
  24. statuses = tag_timeline_statuses.paginate_by_id(
  25. limit_param(DEFAULT_STATUSES_LIMIT),
  26. params_slice(:max_id, :since_id, :min_id)
  27. )
  28. if truthy_param?(:only_media)
  29. # `SELECT DISTINCT id, updated_at` is too slow, so pluck ids at first, and then select id, updated_at with ids.
  30. status_ids = statuses.joins(:media_attachments).distinct(:id).pluck(:id)
  31. statuses.where(id: status_ids)
  32. else
  33. statuses
  34. end
  35. end
  36. end
  37. def tag_timeline_statuses
  38. HashtagQueryService.new.call(@tag, params.slice(:any, :all, :none), current_account, truthy_param?(:local))
  39. end
  40. def insert_pagination_headers
  41. set_pagination_headers(next_path, prev_path)
  42. end
  43. def pagination_params(core_params)
  44. params.slice(:local, :limit, :only_media).permit(:local, :limit, :only_media).merge(core_params)
  45. end
  46. def next_path
  47. api_v1_timelines_tag_url params[:id], pagination_params(max_id: pagination_max_id)
  48. end
  49. def prev_path
  50. api_v1_timelines_tag_url params[:id], pagination_params(min_id: pagination_since_id)
  51. end
  52. def pagination_max_id
  53. @statuses.last.id
  54. end
  55. def pagination_since_id
  56. @statuses.first.id
  57. end
  58. end