accounts_controller.rb 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # frozen_string_literal: true
  2. class AccountsController < ApplicationController
  3. PAGE_SIZE = 20
  4. include AccountControllerConcern
  5. before_action :set_cache_headers
  6. def show
  7. respond_to do |format|
  8. format.html do
  9. @body_classes = 'with-modals'
  10. @pinned_statuses = []
  11. @endorsed_accounts = @account.endorsed_accounts.to_a.sample(4)
  12. if current_account && @account.blocking?(current_account)
  13. @statuses = []
  14. return
  15. end
  16. @pinned_statuses = cache_collection(@account.pinned_statuses, Status) if show_pinned_statuses?
  17. @statuses = filtered_status_page(params)
  18. @statuses = cache_collection(@statuses, Status)
  19. unless @statuses.empty?
  20. @older_url = older_url if @statuses.last.id > filtered_statuses.last.id
  21. @newer_url = newer_url if @statuses.first.id < filtered_statuses.first.id
  22. end
  23. end
  24. format.atom do
  25. @entries = @account.stream_entries.where(hidden: false).with_includes.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id])
  26. render xml: OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.feed(@account, @entries.reject { |entry| entry.status.nil? }))
  27. end
  28. format.rss do
  29. @statuses = cache_collection(default_statuses.without_reblogs.without_replies.limit(PAGE_SIZE), Status)
  30. render xml: RSS::AccountSerializer.render(@account, @statuses)
  31. end
  32. format.json do
  33. skip_session!
  34. render_cached_json(['activitypub', 'actor', @account], content_type: 'application/activity+json') do
  35. ActiveModelSerializers::SerializableResource.new(@account, serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter)
  36. end
  37. end
  38. end
  39. end
  40. private
  41. def show_pinned_statuses?
  42. [replies_requested?, media_requested?, params[:max_id].present?, params[:min_id].present?].none?
  43. end
  44. def filtered_statuses
  45. default_statuses.tap do |statuses|
  46. statuses.merge!(only_media_scope) if media_requested?
  47. statuses.merge!(no_replies_scope) unless replies_requested?
  48. end
  49. end
  50. def default_statuses
  51. @account.statuses.where(visibility: [:public, :unlisted])
  52. end
  53. def only_media_scope
  54. Status.where(id: account_media_status_ids)
  55. end
  56. def account_media_status_ids
  57. @account.media_attachments.attached.reorder(nil).select(:status_id).distinct
  58. end
  59. def no_replies_scope
  60. Status.without_replies
  61. end
  62. def set_account
  63. @account = Account.find_local!(params[:username])
  64. end
  65. def older_url
  66. ::Rails.logger.info("older: max_id #{@statuses.last.id}, url #{pagination_url(max_id: @statuses.last.id)}")
  67. pagination_url(max_id: @statuses.last.id)
  68. end
  69. def newer_url
  70. pagination_url(min_id: @statuses.first.id)
  71. end
  72. def pagination_url(max_id: nil, min_id: nil)
  73. if media_requested?
  74. short_account_media_url(@account, max_id: max_id, min_id: min_id)
  75. elsif replies_requested?
  76. short_account_with_replies_url(@account, max_id: max_id, min_id: min_id)
  77. else
  78. short_account_url(@account, max_id: max_id, min_id: min_id)
  79. end
  80. end
  81. def media_requested?
  82. request.path.ends_with?('/media')
  83. end
  84. def replies_requested?
  85. request.path.ends_with?('/with_replies')
  86. end
  87. def filtered_status_page(params)
  88. if params[:min_id].present?
  89. filtered_statuses.paginate_by_min_id(PAGE_SIZE, params[:min_id]).reverse
  90. else
  91. filtered_statuses.paginate_by_max_id(PAGE_SIZE, params[:max_id], params[:since_id]).to_a
  92. end
  93. end
  94. end