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.

157 lines
4.4 KiB

  1. # frozen_string_literal: true
  2. class AccountsController < ApplicationController
  3. PAGE_SIZE = 20
  4. PAGE_SIZE_MAX = 200
  5. include AccountControllerConcern
  6. include SignatureAuthentication
  7. before_action :require_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
  8. before_action :set_cache_headers
  9. before_action :set_body_classes
  10. skip_around_action :set_locale, if: -> { [:json, :rss].include?(request.format&.to_sym) }
  11. skip_before_action :require_functional!, unless: :whitelist_mode?
  12. def show
  13. respond_to do |format|
  14. format.html do
  15. expires_in 0, public: true unless user_signed_in?
  16. @pinned_statuses = []
  17. @endorsed_accounts = @account.endorsed_accounts.to_a.sample(4)
  18. @featured_hashtags = @account.featured_tags.order(statuses_count: :desc)
  19. if current_account && @account.blocking?(current_account)
  20. @statuses = []
  21. return
  22. end
  23. @pinned_statuses = cache_collection(@account.pinned_statuses, Status) if show_pinned_statuses?
  24. @statuses = cached_filtered_status_page
  25. @rss_url = rss_url
  26. unless @statuses.empty?
  27. @older_url = older_url if @statuses.last.id > filtered_statuses.last.id
  28. @newer_url = newer_url if @statuses.first.id < filtered_statuses.first.id
  29. end
  30. end
  31. format.rss do
  32. expires_in 1.minute, public: true
  33. limit = params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE
  34. @statuses = filtered_statuses.without_reblogs.limit(limit)
  35. @statuses = cache_collection(@statuses, Status)
  36. render xml: RSS::AccountSerializer.render(@account, @statuses, params[:tag])
  37. end
  38. format.json do
  39. expires_in 3.minutes, public: !(authorized_fetch_mode? && signed_request_account.present?)
  40. render_with_cache json: @account, content_type: 'application/activity+json', serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter
  41. end
  42. end
  43. end
  44. private
  45. def set_body_classes
  46. @body_classes = 'with-modals'
  47. end
  48. def show_pinned_statuses?
  49. [replies_requested?, media_requested?, tag_requested?, params[:max_id].present?, params[:min_id].present?].none?
  50. end
  51. def filtered_statuses
  52. default_statuses.tap do |statuses|
  53. statuses.merge!(hashtag_scope) if tag_requested?
  54. statuses.merge!(only_media_scope) if media_requested?
  55. statuses.merge!(no_replies_scope) unless replies_requested?
  56. end
  57. end
  58. def default_statuses
  59. @account.statuses.where(visibility: [:public, :unlisted])
  60. end
  61. def only_media_scope
  62. Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
  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 skip_temporary_suspension_response?
  79. request.format == :json
  80. end
  81. def rss_url
  82. if tag_requested?
  83. short_account_tag_url(@account, params[:tag], format: 'rss')
  84. else
  85. short_account_url(@account, format: 'rss')
  86. end
  87. end
  88. def older_url
  89. pagination_url(max_id: @statuses.last.id)
  90. end
  91. def newer_url
  92. pagination_url(min_id: @statuses.first.id)
  93. end
  94. def pagination_url(max_id: nil, min_id: nil)
  95. if tag_requested?
  96. short_account_tag_url(@account, params[:tag], max_id: max_id, min_id: min_id)
  97. elsif media_requested?
  98. short_account_media_url(@account, max_id: max_id, min_id: min_id)
  99. elsif replies_requested?
  100. short_account_with_replies_url(@account, max_id: max_id, min_id: min_id)
  101. else
  102. short_account_url(@account, max_id: max_id, min_id: min_id)
  103. end
  104. end
  105. def media_requested?
  106. request.path.split('.').first.end_with?('/media') && !tag_requested?
  107. end
  108. def replies_requested?
  109. request.path.split('.').first.end_with?('/with_replies') && !tag_requested?
  110. end
  111. def tag_requested?
  112. request.path.split('.').first.end_with?(Addressable::URI.parse("/tagged/#{params[:tag]}").normalize)
  113. end
  114. def cached_filtered_status_page
  115. cache_collection_paginated_by_id(
  116. filtered_statuses,
  117. Status,
  118. PAGE_SIZE,
  119. params_slice(:max_id, :min_id, :since_id)
  120. )
  121. end
  122. def params_slice(*keys)
  123. params.slice(*keys).permit(*keys)
  124. end
  125. end