info.ex 3.98 KB
Newer Older
lain's avatar
lain committed
1
2
3
4
5
defmodule Pleroma.User.Info do
  use Ecto.Schema
  import Ecto.Changeset

  embedded_schema do
lain's avatar
lain committed
6
    field(:banner, :map, default: %{})
lain's avatar
lain committed
7
    field(:background, :map, default: %{})
lain's avatar
lain committed
8
9
10
11
12
13
14
15
16
17
18
    field(:source_data, :map, default: %{})
    field(:note_count, :integer, default: 0)
    field(:follower_count, :integer, default: 0)
    field(:locked, :boolean, default: false)
    field(:default_scope, :string, default: "public")
    field(:blocks, {:array, :string}, default: [])
    field(:domain_blocks, {:array, :string}, default: [])
    field(:deactivated, :boolean, default: false)
    field(:no_rich_text, :boolean, default: false)
    field(:ap_enabled, :boolean, default: false)
    field(:is_moderator, :boolean, default: false)
lain's avatar
lain committed
19
    field(:is_admin, :boolean, default: false)
lain's avatar
lain committed
20
    field(:keys, :string, default: nil)
lain's avatar
lain committed
21
    field(:settings, :map, default: nil)
lain's avatar
lain committed
22
    field(:magic_key, :string, default: nil)
lain's avatar
lain committed
23
    field(:uri, :string, default: nil)
lain's avatar
lain committed
24
25
    field(:topic, :string, default: nil)
    field(:hub, :string, default: nil)
lain's avatar
lain committed
26
    field(:salmon, :string, default: nil)
lain's avatar
lain committed
27
    field(:hide_network, :boolean, default: false)
28
29
30
31
32
33
34
35

    # Found in the wild
    # ap_id -> Where is this used?
    # bio -> Where is this used?
    # avatar -> Where is this used?
    # fqn -> Where is this used?
    # host -> Where is this used?
    # subject _> Where is this used?
lain's avatar
lain committed
36
37
38
39
40
41
42
43
44
  end

  def set_activation_status(info, deactivated) do
    params = %{deactivated: deactivated}

    info
    |> cast(params, [:deactivated])
    |> validate_required([:deactivated])
  end
lain's avatar
lain committed
45
46

  def add_to_note_count(info, number) do
lain's avatar
lain committed
47
48
49
50
    set_note_count(info, info.note_count + number)
  end

  def set_note_count(info, number) do
lain's avatar
lain committed
51
    params = %{note_count: Enum.max([0, number])}
lain's avatar
lain committed
52
53
54
55
56

    info
    |> cast(params, [:note_count])
    |> validate_required([:note_count])
  end
lain's avatar
lain committed
57
58
59
60
61
62
63
64

  def set_follower_count(info, number) do
    params = %{follower_count: Enum.max([0, number])}

    info
    |> cast(params, [:follower_count])
    |> validate_required([:follower_count])
  end
lain's avatar
lain committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

  def set_blocks(info, blocks) do
    params = %{blocks: blocks}

    info
    |> cast(params, [:blocks])
    |> validate_required([:blocks])
  end

  def add_to_block(info, blocked) do
    set_blocks(info, Enum.uniq([blocked | info.blocks]))
  end

  def remove_from_block(info, blocked) do
    set_blocks(info, List.delete(info.blocks, blocked))
  end
lain's avatar
lain committed
81

lain's avatar
lain committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  def set_domain_blocks(info, domain_blocks) do
    params = %{domain_blocks: domain_blocks}

    info
    |> cast(params, [:domain_blocks])
    |> validate_required([:domain_blocks])
  end

  def add_to_domain_block(info, domain_blocked) do
    set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
  end

  def remove_from_domain_block(info, domain_blocked) do
    set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
  end

lain's avatar
lain committed
98
99
100
101
102
103
104
  def set_keys(info, keys) do
    params = %{keys: keys}

    info
    |> cast(params, [:keys])
    |> validate_required([:keys])
  end
lain's avatar
lain committed
105
106
107

  def remote_user_creation(info, params) do
    info
lain's avatar
lain committed
108
109
110
111
    |> cast(params, [
      :ap_enabled,
      :source_data,
      :banner,
lain's avatar
lain committed
112
      :locked,
lain's avatar
lain committed
113
      :magic_key,
lain's avatar
lain committed
114
115
      :uri,
      :hub,
lain's avatar
lain committed
116
117
      :topic,
      :salmon
lain's avatar
lain committed
118
    ])
lain's avatar
lain committed
119
  end
lain's avatar
lain committed
120

lain's avatar
lain committed
121
122
123
124
125
126
  def user_upgrade(info, params) do
    info
    |> cast(params, [
      :ap_enabled,
      :source_data,
      :banner,
lain's avatar
lain committed
127
128
      :locked,
      :magic_key
lain's avatar
lain committed
129
130
131
    ])
  end

lain's avatar
lain committed
132
133
134
135
136
  def profile_update(info, params) do
    info
    |> cast(params, [
      :locked,
      :no_rich_text,
137
      :default_scope,
lain's avatar
lain committed
138
      :banner,
lain's avatar
lain committed
139
      :hide_network,
lain's avatar
lain committed
140
      :background
lain's avatar
lain committed
141
142
143
    ])
  end

lain's avatar
lain committed
144
145
146
147
148
149
150
151
  def mastodon_profile_update(info, params) do
    info
    |> cast(params, [
      :locked,
      :banner
    ])
  end

rinpatch's avatar
rinpatch committed
152
153
154
155
156
  def mastodon_settings_update(info, params) do
    info
    |> cast(params, [:settings])
  end

lain's avatar
lain committed
157
158
159
  def set_source_data(info, source_data) do
    params = %{source_data: source_data}

lain's avatar
lain committed
160
    info
lain's avatar
lain committed
161
162
    |> cast(params, [:source_data])
    |> validate_required([:source_data])
lain's avatar
lain committed
163
  end
lain's avatar
lain committed
164
165
166
167
168
169
170
171

  def admin_api_update(info, params) do
    info
    |> cast(params, [
      :is_moderator,
      :is_admin
    ])
  end
lain's avatar
lain committed
172
end