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(:hide_followers_count, :boolean, default: false)
43 field(:hide_follows_count, :boolean, default: false)
44 field(:hide_followers, :boolean, default: false)
45 field(:hide_follows, :boolean, default: false)
46 field(:hide_favorites, :boolean, default: true)
47 field(:unread_conversation_count, :integer, default: 0)
48 field(:pinned_activities, {:array, :string}, default: [])
49 field(:email_notifications, :map, default: %{"digest" => false})
50 field(:mascot, :map, default: nil)
51 field(:emoji, {:array, :map}, default: [])
52 field(:pleroma_settings_store, :map, default: %{})
53 field(:fields, {:array, :map}, default: nil)
54 field(:raw_fields, {:array, :map}, default: [])
55 field(:discoverable, :boolean, default: false)
57 field(:notification_settings, :map,
61 "non_follows" => true,
62 "non_followers" => true
66 field(:skip_thread_containment, :boolean, default: false)
69 # ap_id -> Where is this used?
70 # bio -> Where is this used?
71 # avatar -> Where is this used?
72 # fqn -> Where is this used?
73 # host -> Where is this used?
74 # subject _> Where is this used?
77 def set_activation_status(info, deactivated) do
78 params = %{deactivated: deactivated}
81 |> cast(params, [:deactivated])
82 |> validate_required([:deactivated])
85 def set_password_reset_pending(info, pending) do
86 params = %{password_reset_pending: pending}
89 |> cast(params, [:password_reset_pending])
90 |> validate_required([:password_reset_pending])
93 def update_notification_settings(info, settings) do
96 |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
99 notification_settings =
100 info.notification_settings
101 |> Map.merge(settings)
102 |> Map.take(["followers", "follows", "non_follows", "non_followers"])
104 params = %{notification_settings: notification_settings}
107 |> cast(params, [:notification_settings])
108 |> validate_required([:notification_settings])
112 Update email notifications in the given User.Info struct.
116 iex> update_email_notifications(%Pleroma.User.Info{email_notifications: %{"digest" => false}}, %{"digest" => true})
117 %Pleroma.User.Info{email_notifications: %{"digest" => true}}
120 @spec update_email_notifications(t(), map()) :: Ecto.Changeset.t()
121 def update_email_notifications(info, settings) do
122 email_notifications =
123 info.email_notifications
124 |> Map.merge(settings)
125 |> Map.take(["digest"])
127 params = %{email_notifications: email_notifications}
128 fields = [:email_notifications]
131 |> cast(params, fields)
132 |> validate_required(fields)
135 def add_to_note_count(info, number) do
136 set_note_count(info, info.note_count + number)
139 def set_note_count(info, number) do
140 params = %{note_count: Enum.max([0, number])}
143 |> cast(params, [:note_count])
144 |> validate_required([:note_count])
147 def set_follower_count(info, number) do
148 params = %{follower_count: Enum.max([0, number])}
151 |> cast(params, [:follower_count])
152 |> validate_required([:follower_count])
155 def set_mutes(info, mutes) do
156 params = %{mutes: mutes}
159 |> cast(params, [:mutes])
160 |> validate_required([:mutes])
163 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
164 def set_notification_mutes(changeset, muted_notifications, notifications?) do
166 put_change(changeset, :muted_notifications, muted_notifications)
167 |> validate_required([:muted_notifications])
173 def set_blocks(info, blocks) do
174 params = %{blocks: blocks}
177 |> cast(params, [:blocks])
178 |> validate_required([:blocks])
181 def set_subscribers(info, subscribers) do
182 params = %{subscribers: subscribers}
185 |> cast(params, [:subscribers])
186 |> validate_required([:subscribers])
189 @spec add_to_mutes(Info.t(), String.t(), boolean()) :: Changeset.t()
190 def add_to_mutes(info, muted, notifications?) do
192 |> set_mutes(Enum.uniq([muted | info.mutes]))
193 |> set_notification_mutes(
194 Enum.uniq([muted | info.muted_notifications]),
199 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
200 def remove_from_mutes(info, muted) do
202 |> set_mutes(List.delete(info.mutes, muted))
203 |> set_notification_mutes(List.delete(info.muted_notifications, muted), true)
206 def add_to_block(info, blocked) do
207 set_blocks(info, Enum.uniq([blocked | info.blocks]))
210 def remove_from_block(info, blocked) do
211 set_blocks(info, List.delete(info.blocks, blocked))
214 def add_to_subscribers(info, subscribed) do
215 set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
218 def remove_from_subscribers(info, subscribed) do
219 set_subscribers(info, List.delete(info.subscribers, subscribed))
222 def set_domain_blocks(info, domain_blocks) do
223 params = %{domain_blocks: domain_blocks}
226 |> cast(params, [:domain_blocks])
227 |> validate_required([:domain_blocks])
230 def add_to_domain_block(info, domain_blocked) do
231 set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
234 def remove_from_domain_block(info, domain_blocked) do
235 set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
238 def set_keys(info, keys) do
239 params = %{keys: keys}
242 |> cast(params, [:keys])
243 |> validate_required([:keys])
246 def remote_user_creation(info, params) do
248 if Map.has_key?(params, :fields) do
249 Map.put(params, :fields, Enum.map(params[:fields], &truncate_field/1))
264 :hide_followers_count,
271 |> validate_fields(true)
274 def user_upgrade(info, params, remote? \\ false) do
288 :hide_followers_count,
291 |> validate_fields(remote?)
294 def profile_update(info, params) do
303 :hide_followers_count,
308 :skip_thread_containment,
311 :pleroma_settings_store,
317 def validate_fields(changeset, remote? \\ false) do
318 limit_name = if remote?, do: :max_remote_account_fields, else: :max_account_fields
319 limit = Pleroma.Config.get([:instance, limit_name], 0)
322 |> validate_length(:fields, max: limit)
323 |> validate_change(:fields, fn :fields, fields ->
324 if Enum.all?(fields, &valid_field?/1) do
332 defp valid_field?(%{"name" => name, "value" => value}) do
333 name_limit = Pleroma.Config.get([:instance, :account_field_name_length], 255)
334 value_limit = Pleroma.Config.get([:instance, :account_field_value_length], 255)
336 is_binary(name) && is_binary(value) && String.length(name) <= name_limit &&
337 String.length(value) <= value_limit
340 defp valid_field?(_), do: false
342 defp truncate_field(%{"name" => name, "value" => value}) do
344 String.split_at(name, Pleroma.Config.get([:instance, :account_field_name_length], 255))
347 String.split_at(value, Pleroma.Config.get([:instance, :account_field_value_length], 255))
349 %{"name" => name, "value" => value}
352 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
353 def confirmation_changeset(info, opts) do
354 need_confirmation? = Keyword.get(opts, :need_confirmation)
357 if need_confirmation? do
359 confirmation_pending: true,
360 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
364 confirmation_pending: false,
365 confirmation_token: nil
369 cast(info, params, [:confirmation_pending, :confirmation_token])
372 def mastodon_settings_update(info, settings) do
373 params = %{settings: settings}
376 |> cast(params, [:settings])
377 |> validate_required([:settings])
380 def mascot_update(info, url) do
381 params = %{mascot: url}
384 |> cast(params, [:mascot])
385 |> validate_required([:mascot])
388 def set_source_data(info, source_data) do
389 params = %{source_data: source_data}
392 |> cast(params, [:source_data])
393 |> validate_required([:source_data])
396 def admin_api_update(info, params) do
405 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
406 if id not in info.pinned_activities do
407 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
408 params = %{pinned_activities: info.pinned_activities ++ [id]}
411 |> cast(params, [:pinned_activities])
412 |> validate_length(:pinned_activities,
413 max: max_pinned_statuses,
414 message: "You have already pinned the maximum number of statuses"
421 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
422 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
424 cast(info, params, [:pinned_activities])
427 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
430 moderator: is_moderator
434 def add_reblog_mute(info, ap_id) do
435 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
437 cast(info, params, [:muted_reblogs])
440 def remove_reblog_mute(info, ap_id) do
441 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
443 cast(info, params, [:muted_reblogs])
446 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
447 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
448 def fields(%{fields: nil, source_data: %{"attachment" => attachment}}) do
449 limit = Pleroma.Config.get([:instance, :max_remote_account_fields], 0)
452 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
453 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
457 def fields(%{fields: nil}), do: []
459 def fields(%{fields: fields}), do: fields
461 def follow_information_update(info, params) do
468 :hide_followers_count,