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.

79 lines
3.0 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::TimelinesController < ApiController
  3. before_action -> { doorkeeper_authorize! :read }
  4. before_action :require_user!, only: [:home, :mentions]
  5. respond_to :json
  6. def home
  7. @statuses = Feed.new(:home, current_account).get(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id])
  8. @statuses = cache_collection(@statuses)
  9. set_maps(@statuses)
  10. set_counters_maps(@statuses)
  11. set_account_counters_maps(@statuses.flat_map { |s| [s.account, s.reblog? ? s.reblog.account : nil] }.compact.uniq)
  12. next_path = api_v1_home_timeline_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
  13. prev_path = api_v1_home_timeline_url(since_id: @statuses.first.id) unless @statuses.empty?
  14. set_pagination_headers(next_path, prev_path)
  15. render action: :index
  16. end
  17. def mentions
  18. @statuses = Feed.new(:mentions, current_account).get(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id])
  19. @statuses = cache_collection(@statuses)
  20. set_maps(@statuses)
  21. set_counters_maps(@statuses)
  22. set_account_counters_maps(@statuses.flat_map { |s| [s.account, s.reblog? ? s.reblog.account : nil] }.compact.uniq)
  23. next_path = api_v1_mentions_timeline_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
  24. prev_path = api_v1_mentions_timeline_url(since_id: @statuses.first.id) unless @statuses.empty?
  25. set_pagination_headers(next_path, prev_path)
  26. render action: :index
  27. end
  28. def public
  29. @statuses = Status.as_public_timeline(current_account).paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id])
  30. @statuses = cache_collection(@statuses)
  31. set_maps(@statuses)
  32. set_counters_maps(@statuses)
  33. set_account_counters_maps(@statuses.flat_map { |s| [s.account, s.reblog? ? s.reblog.account : nil] }.compact.uniq)
  34. next_path = api_v1_public_timeline_url(max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
  35. prev_path = api_v1_public_timeline_url(since_id: @statuses.first.id) unless @statuses.empty?
  36. set_pagination_headers(next_path, prev_path)
  37. render action: :index
  38. end
  39. def tag
  40. @tag = Tag.find_by(name: params[:id].downcase)
  41. @statuses = @tag.nil? ? [] : Status.as_tag_timeline(@tag, current_account).paginate_by_max_id(DEFAULT_STATUSES_LIMIT, params[:max_id], params[:since_id])
  42. @statuses = cache_collection(@statuses)
  43. set_maps(@statuses)
  44. set_counters_maps(@statuses)
  45. set_account_counters_maps(@statuses.flat_map { |s| [s.account, s.reblog? ? s.reblog.account : nil] }.compact.uniq)
  46. next_path = api_v1_hashtag_timeline_url(params[:id], max_id: @statuses.last.id) if @statuses.size == DEFAULT_STATUSES_LIMIT
  47. prev_path = api_v1_hashtag_timeline_url(params[:id], since_id: @statuses.first.id) unless @statuses.empty?
  48. set_pagination_headers(next_path, prev_path)
  49. render action: :index
  50. end
  51. private
  52. def cache_collection(raw)
  53. super(raw, Status)
  54. end
  55. end