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.

118 lines
3.4 KiB

  1. # frozen_string_literal: true
  2. class AccountsController < ApplicationController
  3. PAGE_SIZE = 20
  4. include AccountControllerConcern
  5. before_action :set_cache_headers
  6. def show
  7. respond_to do |format|
  8. format.html do
  9. use_pack 'public'
  10. @pinned_statuses = []
  11. if current_account && @account.blocking?(current_account)
  12. @statuses = []
  13. return
  14. end
  15. @pinned_statuses = cache_collection(@account.pinned_statuses, Status) if show_pinned_statuses?
  16. @statuses = filtered_status_page(params)
  17. @statuses = cache_collection(@statuses, Status)
  18. unless @statuses.empty?
  19. @older_url = older_url if @statuses.last.id > filtered_statuses.last.id
  20. @newer_url = newer_url if @statuses.first.id < filtered_statuses.first.id
  21. end
  22. end
  23. format.atom do
  24. @entries = @account.stream_entries.where(hidden: false).with_includes.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id])
  25. render xml: OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.feed(@account, @entries.reject { |entry| entry.status.nil? }))
  26. end
  27. format.rss do
  28. @statuses = cache_collection(default_statuses.without_reblogs.without_replies.limit(PAGE_SIZE), Status)
  29. render xml: RSS::AccountSerializer.render(@account, @statuses)
  30. end
  31. format.json do
  32. skip_session!
  33. render_cached_json(['activitypub', 'actor', @account.cache_key], content_type: 'application/activity+json') do
  34. ActiveModelSerializers::SerializableResource.new(@account, serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter)
  35. end
  36. end
  37. end
  38. end
  39. private
  40. def show_pinned_statuses?
  41. [replies_requested?, media_requested?, params[:max_id].present?, params[:since_id].present?].none?
  42. end
  43. def filtered_statuses
  44. default_statuses.tap do |statuses|
  45. statuses.merge!(only_media_scope) if media_requested?
  46. statuses.merge!(no_replies_scope) unless replies_requested?
  47. end
  48. end
  49. def default_statuses
  50. @account.statuses.not_local_only.where(visibility: [:public, :unlisted])
  51. end
  52. def only_media_scope
  53. Status.where(id: account_media_status_ids)
  54. end
  55. def account_media_status_ids
  56. @account.media_attachments.attached.reorder(nil).select(:status_id).distinct
  57. end
  58. def no_replies_scope
  59. Status.without_replies
  60. end
  61. def set_account
  62. @account = Account.find_local!(params[:username])
  63. end
  64. def older_url
  65. ::Rails.logger.info("older: max_id #{@statuses.last.id}, url #{pagination_url(max_id: @statuses.last.id)}")
  66. pagination_url(max_id: @statuses.last.id)
  67. end
  68. def newer_url
  69. pagination_url(min_id: @statuses.first.id)
  70. end
  71. def pagination_url(max_id: nil, min_id: nil)
  72. if media_requested?
  73. short_account_media_url(@account, max_id: max_id, min_id: min_id)
  74. elsif replies_requested?
  75. short_account_with_replies_url(@account, max_id: max_id, min_id: min_id)
  76. else
  77. short_account_url(@account, max_id: max_id, min_id: min_id)
  78. end
  79. end
  80. def media_requested?
  81. request.path.ends_with?('/media')
  82. end
  83. def replies_requested?
  84. request.path.ends_with?('/with_replies')
  85. end
  86. def filtered_status_page(params)
  87. if params[:min_id].present?
  88. filtered_statuses.paginate_by_min_id(PAGE_SIZE, params[:min_id]).reverse
  89. else
  90. filtered_statuses.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id]).to_a
  91. end
  92. end
  93. end