status_cache_hydrator.rb 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. hydrate_reblog_payload(payload, account_id)
  16. else
  17. hydrate_non_reblog_payload(payload, account_id)
  18. end
  19. end
  20. private
  21. def hydrate_non_reblog_payload(empty_payload, account_id)
  22. empty_payload.tap do |payload|
  23. payload[:favourited] = Favourite.where(account_id: account_id, status_id: @status.id).exists?
  24. payload[:reblogged] = Status.where(account_id: account_id, reblog_of_id: @status.id).exists?
  25. payload[:muted] = ConversationMute.where(account_id: account_id, conversation_id: @status.conversation_id).exists?
  26. payload[:bookmarked] = Bookmark.where(account_id: account_id, status_id: @status.id).exists?
  27. payload[:pinned] = StatusPin.where(account_id: account_id, status_id: @status.id).exists? if @status.account_id == account_id
  28. payload[:filtered] = mapped_applied_custom_filter(account_id, @status)
  29. if payload[:poll]
  30. payload[:poll][:voted] = @status.account_id == account_id
  31. payload[:poll][:own_votes] = []
  32. end
  33. end
  34. end
  35. def hydrate_reblog_payload(empty_payload, account_id)
  36. empty_payload.tap do |payload|
  37. payload[:muted] = false
  38. payload[:bookmarked] = false
  39. payload[:pinned] = false if @status.account_id == account_id
  40. payload[:filtered] = mapped_applied_custom_filter(account_id, @status.reblog)
  41. # If the reblogged status is being delivered to the author who disabled the display of the application
  42. # used to create the status, we need to hydrate it here too
  43. payload[:reblog][:application] = payload_reblog_application if payload[:reblog][:application].nil? && @status.reblog.account_id == account_id
  44. payload[:reblog][:favourited] = Favourite.where(account_id: account_id, status_id: @status.reblog_of_id).exists?
  45. payload[:reblog][:reblogged] = Status.where(account_id: account_id, reblog_of_id: @status.reblog_of_id).exists?
  46. payload[:reblog][:muted] = ConversationMute.where(account_id: account_id, conversation_id: @status.reblog.conversation_id).exists?
  47. payload[:reblog][:bookmarked] = Bookmark.where(account_id: account_id, status_id: @status.reblog_of_id).exists?
  48. payload[:reblog][:pinned] = StatusPin.where(account_id: account_id, status_id: @status.reblog_of_id).exists? if @status.reblog.account_id == account_id
  49. payload[:reblog][:filtered] = payload[:filtered]
  50. if payload[:reblog][:poll]
  51. if @status.reblog.account_id == account_id
  52. payload[:reblog][:poll][:voted] = true
  53. payload[:reblog][:poll][:own_votes] = []
  54. else
  55. own_votes = PollVote.where(poll_id: @status.reblog.poll_id, account_id: account_id).pluck(:choice)
  56. payload[:reblog][:poll][:voted] = !own_votes.empty?
  57. payload[:reblog][:poll][:own_votes] = own_votes
  58. end
  59. end
  60. payload[:favourited] = payload[:reblog][:favourited]
  61. payload[:reblogged] = payload[:reblog][:reblogged]
  62. end
  63. end
  64. def mapped_applied_custom_filter(account_id, status)
  65. CustomFilter
  66. .apply_cached_filters(CustomFilter.cached_filters_for(account_id), status)
  67. .map { |filter| serialized_filter(filter) }
  68. end
  69. def serialized_filter(filter)
  70. ActiveModelSerializers::SerializableResource.new(
  71. filter,
  72. serializer: REST::FilterResultSerializer
  73. ).as_json
  74. end
  75. def payload_application
  76. @status.application.present? ? serialized_status_application_json : nil
  77. end
  78. def serialized_status_application_json
  79. ActiveModelSerializers::SerializableResource.new(
  80. @status.application,
  81. serializer: REST::StatusSerializer::ApplicationSerializer
  82. ).as_json
  83. end
  84. def payload_reblog_application
  85. @status.reblog.application.present? ? serialized_status_reblog_application_json : nil
  86. end
  87. def serialized_status_reblog_application_json
  88. ActiveModelSerializers::SerializableResource.new(
  89. @status.reblog.application,
  90. serializer: REST::StatusSerializer::ApplicationSerializer
  91. ).as_json
  92. end
  93. end