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.

139 lines
3.9 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. mark_cacheable! unless user_signed_in?
  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. mark_cacheable!
  27. @entries = @account.stream_entries.where(hidden: false).with_includes.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id])
  28. render xml: OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.feed(@account, @entries.reject { |entry| entry.status.nil? }))
  29. end
  30. format.rss do
  31. mark_cacheable!
  32. @statuses = cache_collection(default_statuses.without_reblogs.without_replies.limit(PAGE_SIZE), Status)
  33. render xml: RSS::AccountSerializer.render(@account, @statuses)
  34. end
  35. format.json do
  36. render_cached_json(['activitypub', 'actor', @account], content_type: 'application/activity+json') do
  37. ActiveModelSerializers::SerializableResource.new(@account, serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter)
  38. end
  39. end
  40. end
  41. end
  42. private
  43. def show_pinned_statuses?
  44. [replies_requested?, media_requested?, tag_requested?, params[:max_id].present?, params[:min_id].present?].none?
  45. end
  46. def filtered_statuses
  47. default_statuses.tap do |statuses|
  48. statuses.merge!(hashtag_scope) if tag_requested?
  49. statuses.merge!(only_media_scope) if media_requested?
  50. statuses.merge!(no_replies_scope) unless replies_requested?
  51. end
  52. end
  53. def default_statuses
  54. @account.statuses.where(visibility: [:public, :unlisted])
  55. end
  56. def only_media_scope
  57. Status.where(id: account_media_status_ids)
  58. end
  59. def account_media_status_ids
  60. @account.media_attachments.attached.reorder(nil).select(:status_id).distinct
  61. end
  62. def no_replies_scope
  63. Status.without_replies
  64. end
  65. def hashtag_scope
  66. tag = Tag.find_normalized(params[:tag])
  67. if tag
  68. Status.tagged_with(tag.id)
  69. else
  70. Status.none
  71. end
  72. end
  73. def username_param
  74. params[:username]
  75. end
  76. def older_url
  77. pagination_url(max_id: @statuses.last.id)
  78. end
  79. def newer_url
  80. pagination_url(min_id: @statuses.first.id)
  81. end
  82. def pagination_url(max_id: nil, min_id: nil)
  83. if tag_requested?
  84. short_account_tag_url(@account, params[:tag], max_id: max_id, min_id: min_id)
  85. elsif media_requested?
  86. short_account_media_url(@account, max_id: max_id, min_id: min_id)
  87. elsif replies_requested?
  88. short_account_with_replies_url(@account, max_id: max_id, min_id: min_id)
  89. else
  90. short_account_url(@account, max_id: max_id, min_id: min_id)
  91. end
  92. end
  93. def media_requested?
  94. request.path.ends_with?('/media')
  95. end
  96. def replies_requested?
  97. request.path.ends_with?('/with_replies')
  98. end
  99. def tag_requested?
  100. request.path.ends_with?(Addressable::URI.parse("/tagged/#{params[:tag]}").normalize)
  101. end
  102. def filtered_status_page(params)
  103. if params[:min_id].present?
  104. filtered_statuses.paginate_by_min_id(PAGE_SIZE, params[:min_id]).reverse
  105. else
  106. filtered_statuses.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id]).to_a
  107. end
  108. end
  109. end