accounts_controller.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. @body_classes = 'with-modals'
  10. @pinned_statuses = []
  11. @endorsed_accounts = @account.endorsed_accounts.to_a.sample(4)
  12. if current_account && @account.blocking?(current_account)
  13. @statuses = []
  14. return
  15. end
  16. @pinned_statuses = cache_collection(@account.pinned_statuses, Status) if show_pinned_statuses?
  17. @statuses = filtered_status_page(params)
  18. @statuses = cache_collection(@statuses, Status)
  19. unless @statuses.empty?
  20. @older_url = older_url if @statuses.last.id > filtered_statuses.last.id
  21. @newer_url = newer_url if @statuses.first.id < filtered_statuses.first.id
  22. end
  23. end
  24. format.atom do
  25. @entries = @account.stream_entries.where(hidden: false).with_includes.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id])
  26. render xml: OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.feed(@account, @entries.reject { |entry| entry.status.nil? }))
  27. end
  28. format.rss do
  29. @statuses = cache_collection(default_statuses.without_reblogs.without_replies.limit(PAGE_SIZE), Status)
  30. render xml: RSS::AccountSerializer.render(@account, @statuses)
  31. end
  32. format.json do
  33. skip_session!
  34. render_cached_json(['activitypub', 'actor', @account], content_type: 'application/activity+json') do
  35. ActiveModelSerializers::SerializableResource.new(@account, serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter)
  36. end
  37. end
  38. end
  39. end
  40. private
  41. def show_pinned_statuses?
  42. [replies_requested?, media_requested?, tag_requested?, params[:max_id].present?, params[:min_id].present?].none?
  43. end
  44. def filtered_statuses
  45. default_statuses.tap do |statuses|
  46. statuses.merge!(hashtag_scope) if tag_requested?
  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.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 hashtag_scope
  64. Status.tagged_with(Tag.find_by(name: params[:tag].downcase)&.id)
  65. end
  66. def set_account
  67. @account = Account.find_local!(params[:username])
  68. end
  69. def older_url
  70. pagination_url(max_id: @statuses.last.id)
  71. end
  72. def newer_url
  73. pagination_url(min_id: @statuses.first.id)
  74. end
  75. def pagination_url(max_id: nil, min_id: nil)
  76. if tag_requested?
  77. short_account_tag_url(@account, params[:tag], max_id: max_id, min_id: min_id)
  78. elsif media_requested?
  79. short_account_media_url(@account, max_id: max_id, min_id: min_id)
  80. elsif replies_requested?
  81. short_account_with_replies_url(@account, max_id: max_id, min_id: min_id)
  82. else
  83. short_account_url(@account, max_id: max_id, min_id: min_id)
  84. end
  85. end
  86. def media_requested?
  87. request.path.ends_with?('/media')
  88. end
  89. def replies_requested?
  90. request.path.ends_with?('/with_replies')
  91. end
  92. def tag_requested?
  93. request.path.ends_with?(Addressable::URI.parse("/tagged/#{params[:tag]}").normalize)
  94. end
  95. def filtered_status_page(params)
  96. if params[:min_id].present?
  97. filtered_statuses.paginate_by_min_id(PAGE_SIZE, params[:min_id]).reverse
  98. else
  99. filtered_statuses.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id]).to_a
  100. end
  101. end
  102. end