info.ex 4.66 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
    field(:source_data, :map, default: %{})
    field(:note_count, :integer, default: 0)
    field(:follower_count, :integer, default: 0)
    field(:locked, :boolean, default: false)
12
13
    field(:confirmation_pending, :boolean, default: false)
    field(:confirmation_token, :string, default: nil)
lain's avatar
lain committed
14
15
16
17
18
19
20
    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
21
    field(:is_admin, :boolean, default: false)
lain's avatar
lain committed
22
    field(:keys, :string, default: nil)
lain's avatar
lain committed
23
    field(:settings, :map, default: nil)
lain's avatar
lain committed
24
    field(:magic_key, :string, default: nil)
lain's avatar
lain committed
25
    field(:uri, :string, default: nil)
lain's avatar
lain committed
26
27
    field(:topic, :string, default: nil)
    field(:hub, :string, default: nil)
lain's avatar
lain committed
28
    field(:salmon, :string, default: nil)
lain's avatar
lain committed
29
    field(:hide_network, :boolean, default: false)
30
31
32
33
34
35
36
37

    # 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
38
39
40
41
42
43
44
45
46
  end

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

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

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

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

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

  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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82

  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
83

lain's avatar
lain committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
  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
100
101
102
103
104
105
106
  def set_keys(info, keys) do
    params = %{keys: keys}

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

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

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

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

146
147
  def confirmation_changeset(info, :confirmed) do
    confirmation_changeset(info, %{
148
149
150
151
152
      confirmation_pending: false,
      confirmation_token: nil
    })
  end

153
154
  def confirmation_changeset(info, :unconfirmed) do
    confirmation_changeset(info, %{
155
156
157
158
159
      confirmation_pending: true,
      confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
    })
  end

160
  def confirmation_changeset(info, params) do
161
162
163
    cast(info, params, [:confirmation_pending, :confirmation_token])
  end

lain's avatar
lain committed
164
165
166
167
168
169
170
171
  def mastodon_profile_update(info, params) do
    info
    |> cast(params, [
      :locked,
      :banner
    ])
  end

eal's avatar
eal committed
172
173
174
  def mastodon_settings_update(info, settings) do
    params = %{settings: settings}

rinpatch's avatar
rinpatch committed
175
176
    info
    |> cast(params, [:settings])
eal's avatar
eal committed
177
    |> validate_required([:settings])
rinpatch's avatar
rinpatch committed
178
179
  end

lain's avatar
lain committed
180
181
182
  def set_source_data(info, source_data) do
    params = %{source_data: source_data}

lain's avatar
lain committed
183
    info
lain's avatar
lain committed
184
185
    |> cast(params, [:source_data])
    |> validate_required([:source_data])
lain's avatar
lain committed
186
  end
lain's avatar
lain committed
187
188
189
190
191
192
193
194

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