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.

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