about summary refs log tree commit diff
path: root/app/lib/inline_renderer.rb
blob: 4bb240b48b9fa032ae90348388ef6f77d5a47d5d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# frozen_string_literal: true

class InlineRenderer
  def initialize(object, current_account, template)
    @object          = object
    @current_account = current_account
    @template        = template
  end

  def render
    case @template
    when :status
      serializer = REST::StatusSerializer
      preload_associations_for_status
    when :notification
      serializer = REST::NotificationSerializer
    when :conversation
      serializer = REST::ConversationSerializer
    when :announcement
      serializer = REST::AnnouncementSerializer
    when :reaction
      serializer = REST::ReactionSerializer
    when :encrypted_message
      serializer = REST::EncryptedMessageSerializer
    else
      return
    end

    serializable_resource = ActiveModelSerializers::SerializableResource.new(@object, serializer: serializer, scope: current_user, scope_name: :current_user)
    serializable_resource.as_json
  end

  def self.render(object, current_account, template)
    new(object, current_account, template).render
  end

  private

  def preload_associations_for_status
    ActiveRecord::Associations::Preloader.new.preload(@object, {
      active_mentions: :account,

      reblog: {
        active_mentions: :account,
      },
    })
  end

  def current_user
    @current_account&.user
  end
end