Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / user / info.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.User.Info do
6 use Ecto.Schema
7 import Ecto.Changeset
8
9 alias Pleroma.User.Info
10
11 @type t :: %__MODULE__{}
12
13 embedded_schema do
14 field(:banner, :map, default: %{})
15 field(:background, :map, default: %{})
16 field(:source_data, :map, default: %{})
17 field(:note_count, :integer, default: 0)
18 field(:follower_count, :integer, default: 0)
19 # Should be filled in only for remote users
20 field(:following_count, :integer, default: nil)
21 field(:locked, :boolean, default: false)
22 field(:confirmation_pending, :boolean, default: false)
23 field(:password_reset_pending, :boolean, default: false)
24 field(:confirmation_token, :string, default: nil)
25 field(:default_scope, :string, default: "public")
26 field(:blocks, {:array, :string}, default: [])
27 field(:domain_blocks, {:array, :string}, default: [])
28 field(:mutes, {:array, :string}, default: [])
29 field(:muted_reblogs, {:array, :string}, default: [])
30 field(:muted_notifications, {:array, :string}, default: [])
31 field(:subscribers, {:array, :string}, default: [])
32 field(:deactivated, :boolean, default: false)
33 field(:no_rich_text, :boolean, default: false)
34 field(:ap_enabled, :boolean, default: false)
35 field(:is_moderator, :boolean, default: false)
36 field(:is_admin, :boolean, default: false)
37 field(:show_role, :boolean, default: true)
38 field(:keys, :string, default: nil)
39 field(:settings, :map, default: nil)
40 field(:magic_key, :string, default: nil)
41 field(:uri, :string, default: nil)
42 field(:topic, :string, default: nil)
43 field(:hub, :string, default: nil)
44 field(:salmon, :string, default: nil)
45 field(:hide_followers_count, :boolean, default: false)
46 field(:hide_follows_count, :boolean, default: false)
47 field(:hide_followers, :boolean, default: false)
48 field(:hide_follows, :boolean, default: false)
49 field(:hide_favorites, :boolean, default: true)
50 field(:pinned_activities, {:array, :string}, default: [])
51 field(:email_notifications, :map, default: %{"digest" => false})
52 field(:mascot, :map, default: nil)
53 field(:emoji, {:array, :map}, default: [])
54 field(:pleroma_settings_store, :map, default: %{})
55 field(:fields, {:array, :map}, default: nil)
56 field(:raw_fields, {:array, :map}, default: [])
57
58 field(:notification_settings, :map,
59 default: %{
60 "followers" => true,
61 "follows" => true,
62 "non_follows" => true,
63 "non_followers" => true
64 }
65 )
66
67 field(:skip_thread_containment, :boolean, default: false)
68
69 # Found in the wild
70 # ap_id -> Where is this used?
71 # bio -> Where is this used?
72 # avatar -> Where is this used?
73 # fqn -> Where is this used?
74 # host -> Where is this used?
75 # subject _> Where is this used?
76 end
77
78 def set_activation_status(info, deactivated) do
79 params = %{deactivated: deactivated}
80
81 info
82 |> cast(params, [:deactivated])
83 |> validate_required([:deactivated])
84 end
85
86 def set_password_reset_pending(info, pending) do
87 params = %{password_reset_pending: pending}
88
89 info
90 |> cast(params, [:password_reset_pending])
91 |> validate_required([:password_reset_pending])
92 end
93
94 def update_notification_settings(info, settings) do
95 settings =
96 settings
97 |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
98 |> Map.new()
99
100 notification_settings =
101 info.notification_settings
102 |> Map.merge(settings)
103 |> Map.take(["followers", "follows", "non_follows", "non_followers"])
104
105 params = %{notification_settings: notification_settings}
106
107 info
108 |> cast(params, [:notification_settings])
109 |> validate_required([:notification_settings])
110 end
111
112 @doc """
113 Update email notifications in the given User.Info struct.
114
115 Examples:
116
117 iex> update_email_notifications(%Pleroma.User.Info{email_notifications: %{"digest" => false}}, %{"digest" => true})
118 %Pleroma.User.Info{email_notifications: %{"digest" => true}}
119
120 """
121 @spec update_email_notifications(t(), map()) :: Ecto.Changeset.t()
122 def update_email_notifications(info, settings) do
123 email_notifications =
124 info.email_notifications
125 |> Map.merge(settings)
126 |> Map.take(["digest"])
127
128 params = %{email_notifications: email_notifications}
129 fields = [:email_notifications]
130
131 info
132 |> cast(params, fields)
133 |> validate_required(fields)
134 end
135
136 def add_to_note_count(info, number) do
137 set_note_count(info, info.note_count + number)
138 end
139
140 def set_note_count(info, number) do
141 params = %{note_count: Enum.max([0, number])}
142
143 info
144 |> cast(params, [:note_count])
145 |> validate_required([:note_count])
146 end
147
148 def set_follower_count(info, number) do
149 params = %{follower_count: Enum.max([0, number])}
150
151 info
152 |> cast(params, [:follower_count])
153 |> validate_required([:follower_count])
154 end
155
156 def set_mutes(info, mutes) do
157 params = %{mutes: mutes}
158
159 info
160 |> cast(params, [:mutes])
161 |> validate_required([:mutes])
162 end
163
164 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
165 def set_notification_mutes(changeset, muted_notifications, notifications?) do
166 if notifications? do
167 put_change(changeset, :muted_notifications, muted_notifications)
168 |> validate_required([:muted_notifications])
169 else
170 changeset
171 end
172 end
173
174 def set_blocks(info, blocks) do
175 params = %{blocks: blocks}
176
177 info
178 |> cast(params, [:blocks])
179 |> validate_required([:blocks])
180 end
181
182 def set_subscribers(info, subscribers) do
183 params = %{subscribers: subscribers}
184
185 info
186 |> cast(params, [:subscribers])
187 |> validate_required([:subscribers])
188 end
189
190 @spec add_to_mutes(Info.t(), String.t()) :: Changeset.t()
191 def add_to_mutes(info, muted) do
192 set_mutes(info, Enum.uniq([muted | info.mutes]))
193 end
194
195 @spec add_to_muted_notifications(Changeset.t(), Info.t(), String.t(), boolean()) ::
196 Changeset.t()
197 def add_to_muted_notifications(changeset, info, muted, notifications?) do
198 set_notification_mutes(
199 changeset,
200 Enum.uniq([muted | info.muted_notifications]),
201 notifications?
202 )
203 end
204
205 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
206 def remove_from_mutes(info, muted) do
207 set_mutes(info, List.delete(info.mutes, muted))
208 end
209
210 @spec remove_from_muted_notifications(Changeset.t(), Info.t(), String.t()) :: Changeset.t()
211 def remove_from_muted_notifications(changeset, info, muted) do
212 set_notification_mutes(changeset, List.delete(info.muted_notifications, muted), true)
213 end
214
215 def add_to_block(info, blocked) do
216 set_blocks(info, Enum.uniq([blocked | info.blocks]))
217 end
218
219 def remove_from_block(info, blocked) do
220 set_blocks(info, List.delete(info.blocks, blocked))
221 end
222
223 def add_to_subscribers(info, subscribed) do
224 set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
225 end
226
227 def remove_from_subscribers(info, subscribed) do
228 set_subscribers(info, List.delete(info.subscribers, subscribed))
229 end
230
231 def set_domain_blocks(info, domain_blocks) do
232 params = %{domain_blocks: domain_blocks}
233
234 info
235 |> cast(params, [:domain_blocks])
236 |> validate_required([:domain_blocks])
237 end
238
239 def add_to_domain_block(info, domain_blocked) do
240 set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
241 end
242
243 def remove_from_domain_block(info, domain_blocked) do
244 set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
245 end
246
247 def set_keys(info, keys) do
248 params = %{keys: keys}
249
250 info
251 |> cast(params, [:keys])
252 |> validate_required([:keys])
253 end
254
255 def remote_user_creation(info, params) do
256 params =
257 if Map.has_key?(params, :fields) do
258 Map.put(params, :fields, Enum.map(params[:fields], &truncate_field/1))
259 else
260 params
261 end
262
263 info
264 |> cast(params, [
265 :ap_enabled,
266 :source_data,
267 :banner,
268 :locked,
269 :magic_key,
270 :uri,
271 :hub,
272 :topic,
273 :salmon,
274 :hide_followers,
275 :hide_follows,
276 :hide_followers_count,
277 :hide_follows_count,
278 :follower_count,
279 :fields,
280 :following_count
281 ])
282 |> validate_fields(true)
283 end
284
285 def user_upgrade(info, params, remote? \\ false) do
286 info
287 |> cast(params, [
288 :ap_enabled,
289 :source_data,
290 :banner,
291 :locked,
292 :magic_key,
293 :follower_count,
294 :following_count,
295 :hide_follows,
296 :fields,
297 :hide_followers,
298 :hide_followers_count,
299 :hide_follows_count
300 ])
301 |> validate_fields(remote?)
302 end
303
304 def profile_update(info, params) do
305 info
306 |> cast(params, [
307 :locked,
308 :no_rich_text,
309 :default_scope,
310 :banner,
311 :hide_follows,
312 :hide_followers,
313 :hide_followers_count,
314 :hide_follows_count,
315 :hide_favorites,
316 :background,
317 :show_role,
318 :skip_thread_containment,
319 :fields,
320 :raw_fields,
321 :pleroma_settings_store
322 ])
323 |> validate_fields()
324 end
325
326 def validate_fields(changeset, remote? \\ false) do
327 limit_name = if remote?, do: :max_remote_account_fields, else: :max_account_fields
328 limit = Pleroma.Config.get([:instance, limit_name], 0)
329
330 changeset
331 |> validate_length(:fields, max: limit)
332 |> validate_change(:fields, fn :fields, fields ->
333 if Enum.all?(fields, &valid_field?/1) do
334 []
335 else
336 [fields: "invalid"]
337 end
338 end)
339 end
340
341 defp valid_field?(%{"name" => name, "value" => value}) do
342 name_limit = Pleroma.Config.get([:instance, :account_field_name_length], 255)
343 value_limit = Pleroma.Config.get([:instance, :account_field_value_length], 255)
344
345 is_binary(name) &&
346 is_binary(value) &&
347 String.length(name) <= name_limit &&
348 String.length(value) <= value_limit
349 end
350
351 defp valid_field?(_), do: false
352
353 defp truncate_field(%{"name" => name, "value" => value}) do
354 {name, _chopped} =
355 String.split_at(name, Pleroma.Config.get([:instance, :account_field_name_length], 255))
356
357 {value, _chopped} =
358 String.split_at(value, Pleroma.Config.get([:instance, :account_field_value_length], 255))
359
360 %{"name" => name, "value" => value}
361 end
362
363 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
364 def confirmation_changeset(info, opts) do
365 need_confirmation? = Keyword.get(opts, :need_confirmation)
366
367 params =
368 if need_confirmation? do
369 %{
370 confirmation_pending: true,
371 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
372 }
373 else
374 %{
375 confirmation_pending: false,
376 confirmation_token: nil
377 }
378 end
379
380 cast(info, params, [:confirmation_pending, :confirmation_token])
381 end
382
383 def mastodon_settings_update(info, settings) do
384 params = %{settings: settings}
385
386 info
387 |> cast(params, [:settings])
388 |> validate_required([:settings])
389 end
390
391 def mascot_update(info, url) do
392 params = %{mascot: url}
393
394 info
395 |> cast(params, [:mascot])
396 |> validate_required([:mascot])
397 end
398
399 def set_source_data(info, source_data) do
400 params = %{source_data: source_data}
401
402 info
403 |> cast(params, [:source_data])
404 |> validate_required([:source_data])
405 end
406
407 def admin_api_update(info, params) do
408 info
409 |> cast(params, [
410 :is_moderator,
411 :is_admin,
412 :show_role
413 ])
414 end
415
416 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
417 if id not in info.pinned_activities do
418 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
419 params = %{pinned_activities: info.pinned_activities ++ [id]}
420
421 info
422 |> cast(params, [:pinned_activities])
423 |> validate_length(:pinned_activities,
424 max: max_pinned_statuses,
425 message: "You have already pinned the maximum number of statuses"
426 )
427 else
428 change(info)
429 end
430 end
431
432 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
433 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
434
435 cast(info, params, [:pinned_activities])
436 end
437
438 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
439 %{
440 admin: is_admin,
441 moderator: is_moderator
442 }
443 end
444
445 def add_reblog_mute(info, ap_id) do
446 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
447
448 cast(info, params, [:muted_reblogs])
449 end
450
451 def remove_reblog_mute(info, ap_id) do
452 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
453
454 cast(info, params, [:muted_reblogs])
455 end
456
457 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
458 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
459 def fields(%{fields: nil, source_data: %{"attachment" => attachment}}) do
460 limit = Pleroma.Config.get([:instance, :max_remote_account_fields], 0)
461
462 attachment
463 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
464 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
465 |> Enum.take(limit)
466 end
467
468 def fields(%{fields: nil}), do: []
469
470 def fields(%{fields: fields}), do: fields
471
472 def follow_information_update(info, params) do
473 info
474 |> cast(params, [
475 :hide_followers,
476 :hide_follows,
477 :follower_count,
478 :following_count,
479 :hide_followers_count,
480 :hide_follows_count
481 ])
482 end
483 end