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.

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