about summary refs log tree commit diff
path: root/app/serializers/rest/announcement_serializer.rb
blob: 9343b97d2230770b1b5a56b4998e724adb97b2cd (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
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true

class REST::AnnouncementSerializer < ActiveModel::Serializer
  attributes :id, :content, :starts_at, :ends_at, :all_day,
             :published_at, :updated_at

  attribute :read, if: :current_user?

  has_many :mentions
  has_many :statuses
  has_many :tags, serializer: REST::StatusSerializer::TagSerializer
  has_many :emojis, serializer: REST::CustomEmojiSerializer
  has_many :reactions, serializer: REST::ReactionSerializer

  def current_user?
    !current_user.nil?
  end

  def id
    object.id.to_s
  end

  def read
    object.announcement_mutes.where(account: current_user.account).exists?
  end

  def content
    Formatter.instance.linkify(object.text)
  end

  def reactions
    object.reactions(current_user&.account)
  end

  class AccountSerializer < ActiveModel::Serializer
    attributes :id, :username, :url, :acct

    def id
      object.id.to_s
    end

    def url
      ActivityPub::TagManager.instance.url_for(object)
    end

    def acct
      object.pretty_acct
    end
  end

  class StatusSerializer < ActiveModel::Serializer
    attributes :id, :url

    def id
      object.id.to_s
    end

    def url
      ActivityPub::TagManager.instance.url_for(object)
    end
  end
end