accounts_controller.rb 2.8 KB

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