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: [])
58 field(:notification_settings, :map,
62 "non_follows" => true,
63 "non_followers" => true
67 field(:skip_thread_containment, :boolean, default: false)
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?
78 def set_activation_status(info, deactivated) do
79 params = %{deactivated: deactivated}
82 |> cast(params, [:deactivated])
83 |> validate_required([:deactivated])
86 def set_password_reset_pending(info, pending) do
87 params = %{password_reset_pending: pending}
90 |> cast(params, [:password_reset_pending])
91 |> validate_required([:password_reset_pending])
94 def update_notification_settings(info, settings) do
97 |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
100 notification_settings =
101 info.notification_settings
102 |> Map.merge(settings)
103 |> Map.take(["followers", "follows", "non_follows", "non_followers"])
105 params = %{notification_settings: notification_settings}
108 |> cast(params, [:notification_settings])
109 |> validate_required([:notification_settings])
113 Update email notifications in the given User.Info struct.
117 iex> update_email_notifications(%Pleroma.User.Info{email_notifications: %{"digest" => false}}, %{"digest" => true})
118 %Pleroma.User.Info{email_notifications: %{"digest" => true}}
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"])
128 params = %{email_notifications: email_notifications}
129 fields = [:email_notifications]
132 |> cast(params, fields)
133 |> validate_required(fields)
136 def add_to_note_count(info, number) do
137 set_note_count(info, info.note_count + number)
140 def set_note_count(info, number) do
141 params = %{note_count: Enum.max([0, number])}
144 |> cast(params, [:note_count])
145 |> validate_required([:note_count])
148 def set_follower_count(info, number) do
149 params = %{follower_count: Enum.max([0, number])}
152 |> cast(params, [:follower_count])
153 |> validate_required([:follower_count])
156 def set_mutes(info, mutes) do
157 params = %{mutes: mutes}
160 |> cast(params, [:mutes])
161 |> validate_required([:mutes])
164 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
165 def set_notification_mutes(changeset, muted_notifications, notifications?) do
167 put_change(changeset, :muted_notifications, muted_notifications)
168 |> validate_required([:muted_notifications])
174 def set_blocks(info, blocks) do
175 params = %{blocks: blocks}
178 |> cast(params, [:blocks])
179 |> validate_required([:blocks])
182 def set_subscribers(info, subscribers) do
183 params = %{subscribers: subscribers}
186 |> cast(params, [:subscribers])
187 |> validate_required([:subscribers])
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]))
195 @spec add_to_muted_notifications(Changeset.t(), Info.t(), String.t(), boolean()) ::
197 def add_to_muted_notifications(changeset, info, muted, notifications?) do
198 set_notification_mutes(
200 Enum.uniq([muted | info.muted_notifications]),
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))
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)
215 def add_to_block(info, blocked) do
216 set_blocks(info, Enum.uniq([blocked | info.blocks]))
219 def remove_from_block(info, blocked) do
220 set_blocks(info, List.delete(info.blocks, blocked))
223 def add_to_subscribers(info, subscribed) do
224 set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
227 def remove_from_subscribers(info, subscribed) do
228 set_subscribers(info, List.delete(info.subscribers, subscribed))
231 def set_domain_blocks(info, domain_blocks) do
232 params = %{domain_blocks: domain_blocks}
235 |> cast(params, [:domain_blocks])
236 |> validate_required([:domain_blocks])
239 def add_to_domain_block(info, domain_blocked) do
240 set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
243 def remove_from_domain_block(info, domain_blocked) do
244 set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
247 def set_keys(info, keys) do
248 params = %{keys: keys}
251 |> cast(params, [:keys])
252 |> validate_required([:keys])
255 def remote_user_creation(info, params) do
257 if Map.has_key?(params, :fields) do
258 Map.put(params, :fields, Enum.map(params[:fields], &truncate_field/1))
276 :hide_followers_count,
282 |> validate_fields(true)
285 def user_upgrade(info, params, remote? \\ false) do
298 :hide_followers_count,
301 |> validate_fields(remote?)
304 def profile_update(info, params) do
313 :hide_followers_count,
318 :skip_thread_containment,
321 :pleroma_settings_store
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)
331 |> validate_length(:fields, max: limit)
332 |> validate_change(:fields, fn :fields, fields ->
333 if Enum.all?(fields, &valid_field?/1) do
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)
347 String.length(name) <= name_limit &&
348 String.length(value) <= value_limit
351 defp valid_field?(_), do: false
353 defp truncate_field(%{"name" => name, "value" => value}) do
355 String.split_at(name, Pleroma.Config.get([:instance, :account_field_name_length], 255))
358 String.split_at(value, Pleroma.Config.get([:instance, :account_field_value_length], 255))
360 %{"name" => name, "value" => value}
363 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
364 def confirmation_changeset(info, opts) do
365 need_confirmation? = Keyword.get(opts, :need_confirmation)
368 if need_confirmation? do
370 confirmation_pending: true,
371 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
375 confirmation_pending: false,
376 confirmation_token: nil
380 cast(info, params, [:confirmation_pending, :confirmation_token])
383 def mastodon_settings_update(info, settings) do
384 params = %{settings: settings}
387 |> cast(params, [:settings])
388 |> validate_required([:settings])
391 def mascot_update(info, url) do
392 params = %{mascot: url}
395 |> cast(params, [:mascot])
396 |> validate_required([:mascot])
399 def set_source_data(info, source_data) do
400 params = %{source_data: source_data}
403 |> cast(params, [:source_data])
404 |> validate_required([:source_data])
407 def admin_api_update(info, params) do
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]}
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"
432 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
433 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
435 cast(info, params, [:pinned_activities])
438 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
441 moderator: is_moderator
445 def add_reblog_mute(info, ap_id) do
446 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
448 cast(info, params, [:muted_reblogs])
451 def remove_reblog_mute(info, ap_id) do
452 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
454 cast(info, params, [:muted_reblogs])
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)
463 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
464 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
468 def fields(%{fields: nil}), do: []
470 def fields(%{fields: fields}), do: fields
472 def follow_information_update(info, params) do
479 :hide_followers_count,