logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

tag_controller.rb (1863B)


  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_by(name: params[:id].downcase)
  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_max_id(
  25. limit_param(DEFAULT_STATUSES_LIMIT),
  26. params[:max_id],
  27. params[:since_id]
  28. )
  29. if truthy_param?(:only_media)
  30. # `SELECT DISTINCT id, updated_at` is too slow, so pluck ids at first, and then select id, updated_at with ids.
  31. status_ids = statuses.joins(:media_attachments).distinct(:id).pluck(:id)
  32. statuses.where(id: status_ids)
  33. else
  34. statuses
  35. end
  36. end
  37. end
  38. def tag_timeline_statuses
  39. Status.as_tag_timeline(@tag, current_account, truthy_param?(:local))
  40. end
  41. def insert_pagination_headers
  42. set_pagination_headers(next_path, prev_path)
  43. end
  44. def pagination_params(core_params)
  45. params.slice(:local, :limit, :only_media).permit(:local, :limit, :only_media).merge(core_params)
  46. end
  47. def next_path
  48. api_v1_timelines_tag_url params[:id], pagination_params(max_id: pagination_max_id)
  49. end
  50. def prev_path
  51. api_v1_timelines_tag_url params[:id], pagination_params(since_id: pagination_since_id)
  52. end
  53. def pagination_max_id
  54. @statuses.last.id
  55. end
  56. def pagination_since_id
  57. @statuses.first.id
  58. end
  59. end