1
0

accounts_controller.rb 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. vary_by -> { public_fetch_mode? ? 'Accept, Accept-Language, Cookie' : 'Accept, Accept-Language, Cookie, Signature' }
  8. before_action :require_account_signature!, if: -> { request.format == :json && authorized_fetch_mode? }
  9. skip_around_action :set_locale, if: -> { [:json, :rss].include?(request.format&.to_sym) }
  10. skip_before_action :require_functional!, unless: :limited_federation_mode?
  11. def show
  12. respond_to do |format|
  13. format.html do
  14. expires_in(15.seconds, public: true, stale_while_revalidate: 30.seconds, stale_if_error: 1.hour) unless user_signed_in?
  15. @rss_url = rss_url
  16. end
  17. format.rss do
  18. expires_in 1.minute, public: true
  19. limit = params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE
  20. @statuses = filtered_statuses.without_reblogs.limit(limit)
  21. @statuses = cache_collection(@statuses, Status)
  22. end
  23. format.json do
  24. expires_in 3.minutes, public: !(authorized_fetch_mode? && signed_request_account.present?)
  25. render_with_cache json: @account, content_type: 'application/activity+json', serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter
  26. end
  27. end
  28. end
  29. private
  30. def filtered_statuses
  31. default_statuses.tap do |statuses|
  32. statuses.merge!(hashtag_scope) if tag_requested?
  33. statuses.merge!(only_media_scope) if media_requested?
  34. statuses.merge!(no_replies_scope) unless replies_requested?
  35. end
  36. end
  37. def default_statuses
  38. @account.statuses.where(visibility: [:public, :unlisted])
  39. end
  40. def only_media_scope
  41. Status.joins(:media_attachments).merge(@account.media_attachments.reorder(nil)).group(:id)
  42. end
  43. def no_replies_scope
  44. Status.without_replies
  45. end
  46. def hashtag_scope
  47. tag = Tag.find_normalized(params[:tag])
  48. if tag
  49. Status.tagged_with(tag.id)
  50. else
  51. Status.none
  52. end
  53. end
  54. def username_param
  55. params[:username]
  56. end
  57. def skip_temporary_suspension_response?
  58. request.format == :json
  59. end
  60. def rss_url
  61. if tag_requested?
  62. short_account_tag_url(@account, params[:tag], format: 'rss')
  63. else
  64. short_account_url(@account, format: 'rss')
  65. end
  66. end
  67. def media_requested?
  68. request.path.split('.').first.end_with?('/media') && !tag_requested?
  69. end
  70. def replies_requested?
  71. request.path.split('.').first.end_with?('/with_replies') && !tag_requested?
  72. end
  73. def tag_requested?
  74. request.path.split('.').first.end_with?(Addressable::URI.parse("/tagged/#{params[:tag]}").normalize)
  75. end
  76. def cached_filtered_status_page
  77. cache_collection_paginated_by_id(
  78. filtered_statuses,
  79. Status,
  80. PAGE_SIZE,
  81. params_slice(:max_id, :min_id, :since_id)
  82. )
  83. end
  84. def params_slice(*keys)
  85. params.slice(*keys).permit(*keys)
  86. end
  87. end