status_cache_hydrator.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. class StatusCacheHydrator
  3. def initialize(status)
  4. @status = status
  5. end
  6. def hydrate(account_id)
  7. # The cache of the serialized hash is generated by the fan-out-on-write service
  8. payload = Rails.cache.fetch("fan-out/#{@status.id}") { InlineRenderer.render(@status, nil, :status) }
  9. # If we're delivering to the author who disabled the display of the application used to create the
  10. # status, we need to hydrate the application, since it was not rendered for the basic payload
  11. payload[:application] = payload_application if payload[:application].nil? && @status.account_id == account_id
  12. # We take advantage of the fact that some relationships can only occur with an original status, not
  13. # the reblog that wraps it, so we can assume that some values are always false
  14. if payload[:reblog]
  15. payload[:muted] = false
  16. payload[:bookmarked] = false
  17. payload[:pinned] = false if @status.account_id == account_id
  18. payload[:filtered] = CustomFilter
  19. .apply_cached_filters(CustomFilter.cached_filters_for(account_id), @status.reblog)
  20. .map { |filter| serialized_filter(filter) }
  21. # If the reblogged status is being delivered to the author who disabled the display of the application
  22. # used to create the status, we need to hydrate it here too
  23. payload[:reblog][:application] = payload_reblog_application if payload[:reblog][:application].nil? && @status.reblog.account_id == account_id
  24. payload[:reblog][:favourited] = Favourite.where(account_id: account_id, status_id: @status.reblog_of_id).exists?
  25. payload[:reblog][:reblogged] = Status.where(account_id: account_id, reblog_of_id: @status.reblog_of_id).exists?
  26. payload[:reblog][:muted] = ConversationMute.where(account_id: account_id, conversation_id: @status.reblog.conversation_id).exists?
  27. payload[:reblog][:bookmarked] = Bookmark.where(account_id: account_id, status_id: @status.reblog_of_id).exists?
  28. payload[:reblog][:pinned] = StatusPin.where(account_id: account_id, status_id: @status.reblog_of_id).exists? if @status.reblog.account_id == account_id
  29. payload[:reblog][:filtered] = payload[:filtered]
  30. if payload[:reblog][:poll]
  31. if @status.reblog.account_id == account_id
  32. payload[:reblog][:poll][:voted] = true
  33. payload[:reblog][:poll][:own_votes] = []
  34. else
  35. own_votes = PollVote.where(poll_id: @status.reblog.poll_id, account_id: account_id).pluck(:choice)
  36. payload[:reblog][:poll][:voted] = !own_votes.empty?
  37. payload[:reblog][:poll][:own_votes] = own_votes
  38. end
  39. end
  40. payload[:favourited] = payload[:reblog][:favourited]
  41. payload[:reblogged] = payload[:reblog][:reblogged]
  42. else
  43. payload[:favourited] = Favourite.where(account_id: account_id, status_id: @status.id).exists?
  44. payload[:reblogged] = Status.where(account_id: account_id, reblog_of_id: @status.id).exists?
  45. payload[:muted] = ConversationMute.where(account_id: account_id, conversation_id: @status.conversation_id).exists?
  46. payload[:bookmarked] = Bookmark.where(account_id: account_id, status_id: @status.id).exists?
  47. payload[:pinned] = StatusPin.where(account_id: account_id, status_id: @status.id).exists? if @status.account_id == account_id
  48. payload[:filtered] = CustomFilter
  49. .apply_cached_filters(CustomFilter.cached_filters_for(account_id), @status)
  50. .map { |filter| serialized_filter(filter) }
  51. if payload[:poll]
  52. payload[:poll][:voted] = @status.account_id == account_id
  53. payload[:poll][:own_votes] = []
  54. end
  55. end
  56. payload
  57. end
  58. private
  59. def serialized_filter(filter)
  60. ActiveModelSerializers::SerializableResource.new(
  61. filter,
  62. serializer: REST::FilterResultSerializer
  63. ).as_json
  64. end
  65. def payload_application
  66. @status.application.present? ? serialized_status_application_json : nil
  67. end
  68. def serialized_status_application_json
  69. ActiveModelSerializers::SerializableResource.new(
  70. @status.application,
  71. serializer: REST::StatusSerializer::ApplicationSerializer
  72. ).as_json
  73. end
  74. def payload_reblog_application
  75. @status.reblog.application.present? ? serialized_status_reblog_application_json : nil
  76. end
  77. def serialized_status_reblog_application_json
  78. ActiveModelSerializers::SerializableResource.new(
  79. @status.reblog.application,
  80. serializer: REST::StatusSerializer::ApplicationSerializer
  81. ).as_json
  82. end
  83. end