闭社主体 forked from https://github.com/tootsuite/mastodon
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.

161 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.where(id: account_media_status_ids)
  63. end
  64. def account_media_status_ids
  65. @account.media_attachments.attached.reorder(nil).select(:status_id).group(:status_id)
  66. end
  67. def no_replies_scope
  68. Status.without_replies
  69. end
  70. def hashtag_scope
  71. tag = Tag.find_normalized(params[:tag])
  72. if tag
  73. Status.tagged_with(tag.id)
  74. else
  75. Status.none
  76. end
  77. end
  78. def username_param
  79. params[:username]
  80. end
  81. def skip_temporary_suspension_response?
  82. request.format == :json
  83. end
  84. def rss_url
  85. if tag_requested?
  86. short_account_tag_url(@account, params[:tag], format: 'rss')
  87. else
  88. short_account_url(@account, format: 'rss')
  89. end
  90. end
  91. def older_url
  92. pagination_url(max_id: @statuses.last.id)
  93. end
  94. def newer_url
  95. pagination_url(min_id: @statuses.first.id)
  96. end
  97. def pagination_url(max_id: nil, min_id: nil)
  98. if tag_requested?
  99. short_account_tag_url(@account, params[:tag], max_id: max_id, min_id: min_id)
  100. elsif media_requested?
  101. short_account_media_url(@account, max_id: max_id, min_id: min_id)
  102. elsif replies_requested?
  103. short_account_with_replies_url(@account, max_id: max_id, min_id: min_id)
  104. else
  105. short_account_url(@account, max_id: max_id, min_id: min_id)
  106. end
  107. end
  108. def media_requested?
  109. request.path.split('.').first.ends_with?('/media') && !tag_requested?
  110. end
  111. def replies_requested?
  112. request.path.split('.').first.ends_with?('/with_replies') && !tag_requested?
  113. end
  114. def tag_requested?
  115. request.path.split('.').first.ends_with?(Addressable::URI.parse("/tagged/#{params[:tag]}").normalize)
  116. end
  117. def cached_filtered_status_page
  118. cache_collection_paginated_by_id(
  119. filtered_statuses,
  120. Status,
  121. PAGE_SIZE,
  122. params_slice(:max_id, :min_id, :since_id)
  123. )
  124. end
  125. def params_slice(*keys)
  126. params.slice(*keys).permit(*keys)
  127. end
  128. end