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.

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