info.ex 6.82 KB
Newer Older
1
# Pleroma: A lightweight social networking server
kaniini's avatar
kaniini committed
2
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

lain's avatar
lain committed
5
6
7
8
defmodule Pleroma.User.Info do
  use Ecto.Schema
  import Ecto.Changeset

9
10
  alias Pleroma.User.Info

lain's avatar
lain committed
11
  embedded_schema do
lain's avatar
lain committed
12
    field(:banner, :map, default: %{})
lain's avatar
lain committed
13
    field(:background, :map, default: %{})
lain's avatar
lain committed
14
15
16
17
    field(:source_data, :map, default: %{})
    field(:note_count, :integer, default: 0)
    field(:follower_count, :integer, default: 0)
    field(:locked, :boolean, default: false)
18
19
    field(:confirmation_pending, :boolean, default: false)
    field(:confirmation_token, :string, default: nil)
lain's avatar
lain committed
20
21
22
    field(:default_scope, :string, default: "public")
    field(:blocks, {:array, :string}, default: [])
    field(:domain_blocks, {:array, :string}, default: [])
23
    field(:mutes, {:array, :string}, default: [])
24
    field(:muted_reblogs, {:array, :string}, default: [])
lain's avatar
lain committed
25
26
27
28
    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
29
    field(:is_admin, :boolean, default: false)
30
    field(:show_role, :boolean, default: true)
lain's avatar
lain committed
31
    field(:keys, :string, default: nil)
lain's avatar
lain committed
32
    field(:settings, :map, default: nil)
lain's avatar
lain committed
33
    field(:magic_key, :string, default: nil)
lain's avatar
lain committed
34
    field(:uri, :string, default: nil)
lain's avatar
lain committed
35
36
    field(:topic, :string, default: nil)
    field(:hub, :string, default: nil)
lain's avatar
lain committed
37
    field(:salmon, :string, default: nil)
38
    field(:hide_followers, :boolean, default: false)
39
    field(:hide_follows, :boolean, default: false)
href's avatar
href committed
40
    field(:pinned_activities, {:array, :string}, default: [])
41
    field(:flavour, :string, default: nil)
42
43
44
45
46
47
48
49

    # 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
50
51
52
53
54
55
56
57
58
  end

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

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

  def add_to_note_count(info, number) do
lain's avatar
lain committed
61
62
63
64
    set_note_count(info, info.note_count + number)
  end

  def set_note_count(info, number) do
lain's avatar
lain committed
65
    params = %{note_count: Enum.max([0, number])}
lain's avatar
lain committed
66
67
68
69
70

    info
    |> cast(params, [:note_count])
    |> validate_required([:note_count])
  end
lain's avatar
lain committed
71
72
73
74
75
76
77
78

  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
79

80
81
82
83
84
85
86
87
  def set_mutes(info, mutes) do
    params = %{mutes: mutes}

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

lain's avatar
lain committed
88
89
90
91
92
93
94
95
  def set_blocks(info, blocks) do
    params = %{blocks: blocks}

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

96
97
98
99
100
101
102
103
  def add_to_mutes(info, muted) do
    set_mutes(info, Enum.uniq([muted | info.mutes]))
  end

  def remove_from_mutes(info, muted) do
    set_mutes(info, List.delete(info.mutes, muted))
  end

lain's avatar
lain committed
104
105
106
107
108
109
110
  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
111

lain's avatar
lain committed
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
  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
128
129
130
131
132
133
134
  def set_keys(info, keys) do
    params = %{keys: keys}

    info
    |> cast(params, [:keys])
    |> validate_required([:keys])
  end
lain's avatar
lain committed
135
136
137

  def remote_user_creation(info, params) do
    info
lain's avatar
lain committed
138
139
140
141
    |> cast(params, [
      :ap_enabled,
      :source_data,
      :banner,
lain's avatar
lain committed
142
      :locked,
lain's avatar
lain committed
143
      :magic_key,
lain's avatar
lain committed
144
145
      :uri,
      :hub,
lain's avatar
lain committed
146
147
      :topic,
      :salmon
lain's avatar
lain committed
148
    ])
lain's avatar
lain committed
149
  end
lain's avatar
lain committed
150

lain's avatar
lain committed
151
152
153
154
155
156
  def user_upgrade(info, params) do
    info
    |> cast(params, [
      :ap_enabled,
      :source_data,
      :banner,
lain's avatar
lain committed
157
158
      :locked,
      :magic_key
lain's avatar
lain committed
159
160
161
    ])
  end

lain's avatar
lain committed
162
163
164
165
166
  def profile_update(info, params) do
    info
    |> cast(params, [
      :locked,
      :no_rich_text,
167
      :default_scope,
lain's avatar
lain committed
168
      :banner,
169
      :hide_follows,
170
      :hide_followers,
171
172
      :background,
      :show_role
lain's avatar
lain committed
173
174
175
    ])
  end

176
177
  def confirmation_changeset(info, :confirmed) do
    confirmation_changeset(info, %{
178
179
180
181
182
      confirmation_pending: false,
      confirmation_token: nil
    })
  end

183
184
  def confirmation_changeset(info, :unconfirmed) do
    confirmation_changeset(info, %{
185
186
187
188
189
      confirmation_pending: true,
      confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
    })
  end

190
  def confirmation_changeset(info, params) do
191
192
193
    cast(info, params, [:confirmation_pending, :confirmation_token])
  end

lain's avatar
lain committed
194
195
196
197
198
199
200
201
  def mastodon_profile_update(info, params) do
    info
    |> cast(params, [
      :locked,
      :banner
    ])
  end

eal's avatar
eal committed
202
203
204
  def mastodon_settings_update(info, settings) do
    params = %{settings: settings}

rinpatch's avatar
rinpatch committed
205
206
    info
    |> cast(params, [:settings])
eal's avatar
eal committed
207
    |> validate_required([:settings])
rinpatch's avatar
rinpatch committed
208
209
  end

210
211
212
213
214
215
216
217
  def mastodon_flavour_update(info, flavour) do
    params = %{flavour: flavour}

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

lain's avatar
lain committed
218
219
220
  def set_source_data(info, source_data) do
    params = %{source_data: source_data}

lain's avatar
lain committed
221
    info
lain's avatar
lain committed
222
223
    |> cast(params, [:source_data])
    |> validate_required([:source_data])
lain's avatar
lain committed
224
  end
lain's avatar
lain committed
225
226
227
228
229

  def admin_api_update(info, params) do
    info
    |> cast(params, [
      :is_moderator,
230
231
      :is_admin,
      :show_role
lain's avatar
lain committed
232
233
    ])
  end
minibikini's avatar
minibikini committed
234
235
236

  def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
    if id not in info.pinned_activities do
minibikini's avatar
minibikini committed
237
      max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
minibikini's avatar
minibikini committed
238
239
240
241
242
      params = %{pinned_activities: info.pinned_activities ++ [id]}

      info
      |> cast(params, [:pinned_activities])
      |> validate_length(:pinned_activities,
minibikini's avatar
minibikini committed
243
244
        max: max_pinned_statuses,
        message: "You have already pinned the maximum number of statuses"
minibikini's avatar
minibikini committed
245
246
247
248
249
250
251
252
253
254
255
      )
    else
      change(info)
    end
  end

  def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
    params = %{pinned_activities: List.delete(info.pinned_activities, id)}

    cast(info, params, [:pinned_activities])
  end
256
257
258
259
260
261
262

  def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
    %{
      admin: is_admin,
      moderator: is_moderator
    }
  end
263
264
265
266
267
268
269
270
271
272
273
274
  
  def add_reblog_mute(info, id) do
    params = %{muted_reblogs: info.muted_reblogs ++ [id]}

    cast(info, params, [:muted_reblogs])
  end

  def remove_reblog_mute(info, id) do
    params = %{muted_reblogs: List.delete(info.muted_reblogs, id)}

    cast(info, params, [:muted_reblogs])
  end
lain's avatar
lain committed
275
end