1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.User.Info do
9 alias Pleroma.User.Info
11 @type t :: %__MODULE__{}
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 field(:discoverable, :boolean, default: false)
59 field(:notification_settings, :map,
63 "non_follows" => true,
64 "non_followers" => true
68 field(:skip_thread_containment, :boolean, default: false)
71 # ap_id -> Where is this used?
72 # bio -> Where is this used?
73 # avatar -> Where is this used?
74 # fqn -> Where is this used?
75 # host -> Where is this used?
76 # subject _> Where is this used?
79 def set_activation_status(info, deactivated) do
80 params = %{deactivated: deactivated}
83 |> cast(params, [:deactivated])
84 |> validate_required([:deactivated])
87 def set_password_reset_pending(info, pending) do
88 params = %{password_reset_pending: pending}
91 |> cast(params, [:password_reset_pending])
92 |> validate_required([:password_reset_pending])
95 def update_notification_settings(info, settings) do
98 |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
101 notification_settings =
102 info.notification_settings
103 |> Map.merge(settings)
104 |> Map.take(["followers", "follows", "non_follows", "non_followers"])
106 params = %{notification_settings: notification_settings}
109 |> cast(params, [:notification_settings])
110 |> validate_required([:notification_settings])
114 Update email notifications in the given User.Info struct.
118 iex> update_email_notifications(%Pleroma.User.Info{email_notifications: %{"digest" => false}}, %{"digest" => true})
119 %Pleroma.User.Info{email_notifications: %{"digest" => true}}
122 @spec update_email_notifications(t(), map()) :: Ecto.Changeset.t()
123 def update_email_notifications(info, settings) do
124 email_notifications =
125 info.email_notifications
126 |> Map.merge(settings)
127 |> Map.take(["digest"])
129 params = %{email_notifications: email_notifications}
130 fields = [:email_notifications]
133 |> cast(params, fields)
134 |> validate_required(fields)
137 def add_to_note_count(info, number) do
138 set_note_count(info, info.note_count + number)
141 def set_note_count(info, number) do
142 params = %{note_count: Enum.max([0, number])}
145 |> cast(params, [:note_count])
146 |> validate_required([:note_count])
149 def set_follower_count(info, number) do
150 params = %{follower_count: Enum.max([0, number])}
153 |> cast(params, [:follower_count])
154 |> validate_required([:follower_count])
157 def set_mutes(info, mutes) do
158 params = %{mutes: mutes}
161 |> cast(params, [:mutes])
162 |> validate_required([:mutes])
165 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
166 def set_notification_mutes(changeset, muted_notifications, notifications?) do
168 put_change(changeset, :muted_notifications, muted_notifications)
169 |> validate_required([:muted_notifications])
175 def set_blocks(info, blocks) do
176 params = %{blocks: blocks}
179 |> cast(params, [:blocks])
180 |> validate_required([:blocks])
183 def set_subscribers(info, subscribers) do
184 params = %{subscribers: subscribers}
187 |> cast(params, [:subscribers])
188 |> validate_required([:subscribers])
191 @spec add_to_mutes(Info.t(), String.t(), boolean()) :: Changeset.t()
192 def add_to_mutes(info, muted, notifications?) do
194 |> set_mutes(Enum.uniq([muted | info.mutes]))
195 |> set_notification_mutes(
196 Enum.uniq([muted | info.muted_notifications]),
201 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
202 def remove_from_mutes(info, muted) do
204 |> set_mutes(List.delete(info.mutes, muted))
205 |> set_notification_mutes(List.delete(info.muted_notifications, muted), true)
208 def add_to_block(info, blocked) do
209 set_blocks(info, Enum.uniq([blocked | info.blocks]))
212 def remove_from_block(info, blocked) do
213 set_blocks(info, List.delete(info.blocks, blocked))
216 def add_to_subscribers(info, subscribed) do
217 set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
220 def remove_from_subscribers(info, subscribed) do
221 set_subscribers(info, List.delete(info.subscribers, subscribed))
224 def set_domain_blocks(info, domain_blocks) do
225 params = %{domain_blocks: domain_blocks}
228 |> cast(params, [:domain_blocks])
229 |> validate_required([:domain_blocks])
232 def add_to_domain_block(info, domain_blocked) do
233 set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
236 def remove_from_domain_block(info, domain_blocked) do
237 set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
240 def set_keys(info, keys) do
241 params = %{keys: keys}
244 |> cast(params, [:keys])
245 |> validate_required([:keys])
248 def remote_user_creation(info, params) do
250 if Map.has_key?(params, :fields) do
251 Map.put(params, :fields, Enum.map(params[:fields], &truncate_field/1))
269 :hide_followers_count,
276 |> validate_fields(true)
279 def user_upgrade(info, params, remote? \\ false) do
293 :hide_followers_count,
296 |> validate_fields(remote?)
299 def profile_update(info, params) do
308 :hide_followers_count,
313 :skip_thread_containment,
316 :pleroma_settings_store,
322 def validate_fields(changeset, remote? \\ false) do
323 limit_name = if remote?, do: :max_remote_account_fields, else: :max_account_fields
324 limit = Pleroma.Config.get([:instance, limit_name], 0)
327 |> validate_length(:fields, max: limit)
328 |> validate_change(:fields, fn :fields, fields ->
329 if Enum.all?(fields, &valid_field?/1) do
337 defp valid_field?(%{"name" => name, "value" => value}) do
338 name_limit = Pleroma.Config.get([:instance, :account_field_name_length], 255)
339 value_limit = Pleroma.Config.get([:instance, :account_field_value_length], 255)
341 is_binary(name) && is_binary(value) && String.length(name) <= name_limit &&
342 String.length(value) <= value_limit
345 defp valid_field?(_), do: false
347 defp truncate_field(%{"name" => name, "value" => value}) do
349 String.split_at(name, Pleroma.Config.get([:instance, :account_field_name_length], 255))
352 String.split_at(value, Pleroma.Config.get([:instance, :account_field_value_length], 255))
354 %{"name" => name, "value" => value}
357 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
358 def confirmation_changeset(info, opts) do
359 need_confirmation? = Keyword.get(opts, :need_confirmation)
362 if need_confirmation? do
364 confirmation_pending: true,
365 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
369 confirmation_pending: false,
370 confirmation_token: nil
374 cast(info, params, [:confirmation_pending, :confirmation_token])
377 def mastodon_settings_update(info, settings) do
378 params = %{settings: settings}
381 |> cast(params, [:settings])
382 |> validate_required([:settings])
385 def mascot_update(info, url) do
386 params = %{mascot: url}
389 |> cast(params, [:mascot])
390 |> validate_required([:mascot])
393 def set_source_data(info, source_data) do
394 params = %{source_data: source_data}
397 |> cast(params, [:source_data])
398 |> validate_required([:source_data])
401 def admin_api_update(info, params) do
410 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
411 if id not in info.pinned_activities do
412 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
413 params = %{pinned_activities: info.pinned_activities ++ [id]}
416 |> cast(params, [:pinned_activities])
417 |> validate_length(:pinned_activities,
418 max: max_pinned_statuses,
419 message: "You have already pinned the maximum number of statuses"
426 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
427 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
429 cast(info, params, [:pinned_activities])
432 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
435 moderator: is_moderator
439 def add_reblog_mute(info, ap_id) do
440 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
442 cast(info, params, [:muted_reblogs])
445 def remove_reblog_mute(info, ap_id) do
446 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
448 cast(info, params, [:muted_reblogs])
451 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
452 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
453 def fields(%{fields: nil, source_data: %{"attachment" => attachment}}) do
454 limit = Pleroma.Config.get([:instance, :max_remote_account_fields], 0)
457 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
458 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
462 def fields(%{fields: nil}), do: []
464 def fields(%{fields: fields}), do: fields
466 def follow_information_update(info, params) do
473 :hide_followers_count,