status_cache_hydrator.rb 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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] = @status.application.present? ? ActiveModelSerializers::SerializableResource.new(@status.application, serializer: REST::StatusSerializer::ApplicationSerializer).as_json : nil 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.apply_cached_filters(CustomFilter.cached_filters_for(account_id), @status.reblog).map { |filter| ActiveModelSerializers::SerializableResource.new(filter, serializer: REST::FilterResultSerializer).as_json }
  19. # If the reblogged status is being delivered to the author who disabled the display of the application
  20. # used to create the status, we need to hydrate it here too
  21. payload[:reblog][:application] = @status.reblog.application.present? ? ActiveModelSerializers::SerializableResource.new(@status.reblog.application, serializer: REST::StatusSerializer::ApplicationSerializer).as_json : nil if payload[:reblog][:application].nil? && @status.reblog.account_id == account_id
  22. payload[:reblog][:favourited] = Favourite.where(account_id: account_id, status_id: @status.reblog_of_id).exists?
  23. payload[:reblog][:reblogged] = Status.where(account_id: account_id, reblog_of_id: @status.reblog_of_id).exists?
  24. payload[:reblog][:muted] = ConversationMute.where(account_id: account_id, conversation_id: @status.reblog.conversation_id).exists?
  25. payload[:reblog][:bookmarked] = Bookmark.where(account_id: account_id, status_id: @status.reblog_of_id).exists?
  26. payload[:reblog][:pinned] = StatusPin.where(account_id: account_id, status_id: @status.reblog_of_id).exists? if @status.reblog.account_id == account_id
  27. payload[:reblog][:filtered] = payload[:filtered]
  28. if payload[:reblog][:poll]
  29. if @status.reblog.account_id == account_id
  30. payload[:reblog][:poll][:voted] = true
  31. payload[:reblog][:poll][:own_votes] = []
  32. else
  33. own_votes = PollVote.where(poll_id: @status.reblog.poll_id, account_id: account_id).pluck(:choice)
  34. payload[:reblog][:poll][:voted] = !own_votes.empty?
  35. payload[:reblog][:poll][:own_votes] = own_votes
  36. end
  37. end
  38. payload[:favourited] = payload[:reblog][:favourited]
  39. payload[:reblogged] = payload[:reblog][:reblogged]
  40. else
  41. payload[:favourited] = Favourite.where(account_id: account_id, status_id: @status.id).exists?
  42. payload[:reblogged] = Status.where(account_id: account_id, reblog_of_id: @status.id).exists?
  43. payload[:muted] = ConversationMute.where(account_id: account_id, conversation_id: @status.conversation_id).exists?
  44. payload[:bookmarked] = Bookmark.where(account_id: account_id, status_id: @status.id).exists?
  45. payload[:pinned] = StatusPin.where(account_id: account_id, status_id: @status.id).exists? if @status.account_id == account_id
  46. payload[:filtered] = CustomFilter.apply_cached_filters(CustomFilter.cached_filters_for(account_id), @status).map { |filter| ActiveModelSerializers::SerializableResource.new(filter, serializer: REST::FilterResultSerializer).as_json }
  47. if payload[:poll]
  48. payload[:poll][:voted] = @status.account_id == account_id
  49. payload[:poll][:own_votes] = []
  50. end
  51. end
  52. payload
  53. end
  54. end