inline_renderer.rb 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # frozen_string_literal: true
  2. class InlineRenderer
  3. def initialize(object, current_account, template)
  4. @object = object
  5. @current_account = current_account
  6. @template = template
  7. end
  8. def render
  9. case @template
  10. when :status
  11. serializer = REST::StatusSerializer
  12. preload_associations_for_status
  13. when :notification
  14. serializer = REST::NotificationSerializer
  15. when :conversation
  16. serializer = REST::ConversationSerializer
  17. when :announcement
  18. serializer = REST::AnnouncementSerializer
  19. when :reaction
  20. serializer = REST::ReactionSerializer
  21. when :encrypted_message
  22. serializer = REST::EncryptedMessageSerializer
  23. else
  24. return
  25. end
  26. serializable_resource = ActiveModelSerializers::SerializableResource.new(@object, serializer: serializer, scope: current_user, scope_name: :current_user)
  27. serializable_resource.as_json
  28. end
  29. def self.render(object, current_account, template)
  30. new(object, current_account, template).render
  31. end
  32. private
  33. def preload_associations_for_status
  34. ActiveRecord::Associations::Preloader.new(records: [@object], associations: {
  35. active_mentions: :account,
  36. reblog: {
  37. active_mentions: :account,
  38. },
  39. }).call
  40. end
  41. def current_user
  42. @current_account&.user
  43. end
  44. end