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.

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