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(:confirmation_token, :string, default: nil)
24 field(:default_scope, :string, default: "public")
25 field(:blocks, {:array, :string}, default: [])
26 field(:domain_blocks, {:array, :string}, default: [])
27 field(:mutes, {:array, :string}, default: [])
28 field(:muted_reblogs, {:array, :string}, default: [])
29 field(:muted_notifications, {:array, :string}, default: [])
30 field(:subscribers, {:array, :string}, default: [])
31 field(:deactivated, :boolean, default: false)
32 field(:no_rich_text, :boolean, default: false)
33 field(:ap_enabled, :boolean, default: false)
34 field(:is_moderator, :boolean, default: false)
35 field(:is_admin, :boolean, default: false)
36 field(:show_role, :boolean, default: true)
37 field(:keys, :string, default: nil)
38 field(:settings, :map, default: nil)
39 field(:magic_key, :string, default: nil)
40 field(:uri, :string, default: nil)
41 field(:topic, :string, default: nil)
42 field(:hub, :string, default: nil)
43 field(:salmon, :string, default: nil)
44 field(:hide_followers_count, :boolean, default: false)
45 field(:hide_follows_count, :boolean, default: false)
46 field(:hide_followers, :boolean, default: false)
47 field(:hide_follows, :boolean, default: false)
48 field(:hide_favorites, :boolean, default: true)
49 field(:pinned_activities, {:array, :string}, default: [])
50 field(:email_notifications, :map, default: %{"digest" => false})
51 field(:mascot, :map, default: nil)
52 field(:emoji, {:array, :map}, default: [])
53 field(:pleroma_settings_store, :map, default: %{})
54 field(:fields, {:array, :map}, default: nil)
55 field(:raw_fields, {:array, :map}, default: [])
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 update_notification_settings(info, settings) do
88 |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
91 notification_settings =
92 info.notification_settings
93 |> Map.merge(settings)
94 |> Map.take(["followers", "follows", "non_follows", "non_followers"])
96 params = %{notification_settings: notification_settings}
99 |> cast(params, [:notification_settings])
100 |> validate_required([:notification_settings])
104 Update email notifications in the given User.Info struct.
108 iex> update_email_notifications(%Pleroma.User.Info{email_notifications: %{"digest" => false}}, %{"digest" => true})
109 %Pleroma.User.Info{email_notifications: %{"digest" => true}}
112 @spec update_email_notifications(t(), map()) :: Ecto.Changeset.t()
113 def update_email_notifications(info, settings) do
114 email_notifications =
115 info.email_notifications
116 |> Map.merge(settings)
117 |> Map.take(["digest"])
119 params = %{email_notifications: email_notifications}
120 fields = [:email_notifications]
123 |> cast(params, fields)
124 |> validate_required(fields)
127 def add_to_note_count(info, number) do
128 set_note_count(info, info.note_count + number)
131 def set_note_count(info, number) do
132 params = %{note_count: Enum.max([0, number])}
135 |> cast(params, [:note_count])
136 |> validate_required([:note_count])
139 def set_follower_count(info, number) do
140 params = %{follower_count: Enum.max([0, number])}
143 |> cast(params, [:follower_count])
144 |> validate_required([:follower_count])
147 def set_mutes(info, mutes) do
148 params = %{mutes: mutes}
151 |> cast(params, [:mutes])
152 |> validate_required([:mutes])
155 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
156 def set_notification_mutes(changeset, muted_notifications, notifications?) do
158 put_change(changeset, :muted_notifications, muted_notifications)
159 |> validate_required([:muted_notifications])
165 def set_blocks(info, blocks) do
166 params = %{blocks: blocks}
169 |> cast(params, [:blocks])
170 |> validate_required([:blocks])
173 def set_subscribers(info, subscribers) do
174 params = %{subscribers: subscribers}
177 |> cast(params, [:subscribers])
178 |> validate_required([:subscribers])
181 @spec add_to_mutes(Info.t(), String.t()) :: Changeset.t()
182 def add_to_mutes(info, muted) do
183 set_mutes(info, Enum.uniq([muted | info.mutes]))
186 @spec add_to_muted_notifications(Changeset.t(), Info.t(), String.t(), boolean()) ::
188 def add_to_muted_notifications(changeset, info, muted, notifications?) do
189 set_notification_mutes(
191 Enum.uniq([muted | info.muted_notifications]),
196 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
197 def remove_from_mutes(info, muted) do
198 set_mutes(info, List.delete(info.mutes, muted))
201 @spec remove_from_muted_notifications(Changeset.t(), Info.t(), String.t()) :: Changeset.t()
202 def remove_from_muted_notifications(changeset, info, muted) do
203 set_notification_mutes(changeset, 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))
267 :hide_followers_count,
273 |> validate_fields(true)
276 def user_upgrade(info, params, remote? \\ false) do
289 :hide_followers_count,
292 |> validate_fields(remote?)
295 def profile_update(info, params) do
304 :hide_followers_count,
309 :skip_thread_containment,
312 :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)
338 String.length(name) <= name_limit &&
339 String.length(value) <= value_limit
342 defp valid_field?(_), do: false
344 defp truncate_field(%{"name" => name, "value" => value}) do
346 String.split_at(name, Pleroma.Config.get([:instance, :account_field_name_length], 255))
349 String.split_at(value, Pleroma.Config.get([:instance, :account_field_value_length], 255))
351 %{"name" => name, "value" => value}
354 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
355 def confirmation_changeset(info, opts) do
356 need_confirmation? = Keyword.get(opts, :need_confirmation)
359 if need_confirmation? do
361 confirmation_pending: true,
362 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
366 confirmation_pending: false,
367 confirmation_token: nil
371 cast(info, params, [:confirmation_pending, :confirmation_token])
374 def mastodon_settings_update(info, settings) do
375 params = %{settings: settings}
378 |> cast(params, [:settings])
379 |> validate_required([:settings])
382 def mascot_update(info, url) do
383 params = %{mascot: url}
386 |> cast(params, [:mascot])
387 |> validate_required([:mascot])
390 def set_source_data(info, source_data) do
391 params = %{source_data: source_data}
394 |> cast(params, [:source_data])
395 |> validate_required([:source_data])
398 def admin_api_update(info, params) do
407 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
408 if id not in info.pinned_activities do
409 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
410 params = %{pinned_activities: info.pinned_activities ++ [id]}
413 |> cast(params, [:pinned_activities])
414 |> validate_length(:pinned_activities,
415 max: max_pinned_statuses,
416 message: "You have already pinned the maximum number of statuses"
423 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
424 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
426 cast(info, params, [:pinned_activities])
429 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
432 moderator: is_moderator
436 def add_reblog_mute(info, ap_id) do
437 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
439 cast(info, params, [:muted_reblogs])
442 def remove_reblog_mute(info, ap_id) do
443 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
445 cast(info, params, [:muted_reblogs])
448 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
449 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
450 def fields(%{fields: nil, source_data: %{"attachment" => attachment}}) do
451 limit = Pleroma.Config.get([:instance, :max_remote_account_fields], 0)
454 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
455 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
459 def fields(%{fields: nil}), do: []
461 def fields(%{fields: fields}), do: fields
463 def follow_information_update(info, params) do
470 :hide_followers_count,