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, :boolean, default: false)
45 field(:hide_follows, :boolean, default: false)
46 field(:hide_favorites, :boolean, default: true)
47 field(:pinned_activities, {:array, :string}, default: [])
48 field(:email_notifications, :map, default: %{"digest" => false})
49 field(:mascot, :map, default: nil)
50 field(:emoji, {:array, :map}, default: [])
51 field(:pleroma_settings_store, :map, default: %{})
52 field(:fields, {:array, :map}, default: nil)
53 field(:raw_fields, {:array, :map}, default: [])
55 field(:notification_settings, :map,
59 "non_follows" => true,
60 "non_followers" => true
64 field(:skip_thread_containment, :boolean, default: false)
67 # ap_id -> Where is this used?
68 # bio -> Where is this used?
69 # avatar -> Where is this used?
70 # fqn -> Where is this used?
71 # host -> Where is this used?
72 # subject _> Where is this used?
75 def set_activation_status(info, deactivated) do
76 params = %{deactivated: deactivated}
79 |> cast(params, [:deactivated])
80 |> validate_required([:deactivated])
83 def update_notification_settings(info, settings) do
86 |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
89 notification_settings =
90 info.notification_settings
91 |> Map.merge(settings)
92 |> Map.take(["followers", "follows", "non_follows", "non_followers"])
94 params = %{notification_settings: notification_settings}
97 |> cast(params, [:notification_settings])
98 |> validate_required([:notification_settings])
102 Update email notifications in the given User.Info struct.
106 iex> update_email_notifications(%Pleroma.User.Info{email_notifications: %{"digest" => false}}, %{"digest" => true})
107 %Pleroma.User.Info{email_notifications: %{"digest" => true}}
110 @spec update_email_notifications(t(), map()) :: Ecto.Changeset.t()
111 def update_email_notifications(info, settings) do
112 email_notifications =
113 info.email_notifications
114 |> Map.merge(settings)
115 |> Map.take(["digest"])
117 params = %{email_notifications: email_notifications}
118 fields = [:email_notifications]
121 |> cast(params, fields)
122 |> validate_required(fields)
125 def add_to_note_count(info, number) do
126 set_note_count(info, info.note_count + number)
129 def set_note_count(info, number) do
130 params = %{note_count: Enum.max([0, number])}
133 |> cast(params, [:note_count])
134 |> validate_required([:note_count])
137 def set_follower_count(info, number) do
138 params = %{follower_count: Enum.max([0, number])}
141 |> cast(params, [:follower_count])
142 |> validate_required([:follower_count])
145 def set_mutes(info, mutes) do
146 params = %{mutes: mutes}
149 |> cast(params, [:mutes])
150 |> validate_required([:mutes])
153 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
154 def set_notification_mutes(changeset, muted_notifications, notifications?) do
156 put_change(changeset, :muted_notifications, muted_notifications)
157 |> validate_required([:muted_notifications])
163 def set_blocks(info, blocks) do
164 params = %{blocks: blocks}
167 |> cast(params, [:blocks])
168 |> validate_required([:blocks])
171 def set_subscribers(info, subscribers) do
172 params = %{subscribers: subscribers}
175 |> cast(params, [:subscribers])
176 |> validate_required([:subscribers])
179 @spec add_to_mutes(Info.t(), String.t()) :: Changeset.t()
180 def add_to_mutes(info, muted) do
181 set_mutes(info, Enum.uniq([muted | info.mutes]))
184 @spec add_to_muted_notifications(Changeset.t(), Info.t(), String.t(), boolean()) ::
186 def add_to_muted_notifications(changeset, info, muted, notifications?) do
187 set_notification_mutes(
189 Enum.uniq([muted | info.muted_notifications]),
194 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
195 def remove_from_mutes(info, muted) do
196 set_mutes(info, List.delete(info.mutes, muted))
199 @spec remove_from_muted_notifications(Changeset.t(), Info.t(), String.t()) :: Changeset.t()
200 def remove_from_muted_notifications(changeset, info, muted) do
201 set_notification_mutes(changeset, List.delete(info.muted_notifications, muted), true)
204 def add_to_block(info, blocked) do
205 set_blocks(info, Enum.uniq([blocked | info.blocks]))
208 def remove_from_block(info, blocked) do
209 set_blocks(info, List.delete(info.blocks, blocked))
212 def add_to_subscribers(info, subscribed) do
213 set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
216 def remove_from_subscribers(info, subscribed) do
217 set_subscribers(info, List.delete(info.subscribers, subscribed))
220 def set_domain_blocks(info, domain_blocks) do
221 params = %{domain_blocks: domain_blocks}
224 |> cast(params, [:domain_blocks])
225 |> validate_required([:domain_blocks])
228 def add_to_domain_block(info, domain_blocked) do
229 set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
232 def remove_from_domain_block(info, domain_blocked) do
233 set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
236 def set_keys(info, keys) do
237 params = %{keys: keys}
240 |> cast(params, [:keys])
241 |> validate_required([:keys])
244 def remote_user_creation(info, params) do
262 |> validate_fields(true)
265 def user_upgrade(info, params, remote? \\ false) do
279 |> validate_fields(remote?)
282 def profile_update(info, params) do
294 :skip_thread_containment,
297 :pleroma_settings_store
302 def validate_fields(changeset, remote? \\ false) do
303 limit_name = if remote?, do: :max_remote_account_fields, else: :max_account_fields
304 limit = Pleroma.Config.get([:instance, limit_name], 0)
307 |> validate_length(:fields, max: limit)
308 |> validate_change(:fields, fn :fields, fields ->
309 if Enum.all?(fields, &valid_field?/1) do
317 defp valid_field?(%{"name" => name, "value" => value}) do
318 name_limit = Pleroma.Config.get([:instance, :account_field_name_length], 255)
319 value_limit = Pleroma.Config.get([:instance, :account_field_value_length], 255)
323 String.length(name) <= name_limit &&
324 String.length(value) <= value_limit
327 defp valid_field?(_), do: false
329 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
330 def confirmation_changeset(info, opts) do
331 need_confirmation? = Keyword.get(opts, :need_confirmation)
334 if need_confirmation? do
336 confirmation_pending: true,
337 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
341 confirmation_pending: false,
342 confirmation_token: nil
346 cast(info, params, [:confirmation_pending, :confirmation_token])
349 def mastodon_settings_update(info, settings) do
350 params = %{settings: settings}
353 |> cast(params, [:settings])
354 |> validate_required([:settings])
357 def mascot_update(info, url) do
358 params = %{mascot: url}
361 |> cast(params, [:mascot])
362 |> validate_required([:mascot])
365 def set_source_data(info, source_data) do
366 params = %{source_data: source_data}
369 |> cast(params, [:source_data])
370 |> validate_required([:source_data])
373 def admin_api_update(info, params) do
382 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
383 if id not in info.pinned_activities do
384 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
385 params = %{pinned_activities: info.pinned_activities ++ [id]}
388 |> cast(params, [:pinned_activities])
389 |> validate_length(:pinned_activities,
390 max: max_pinned_statuses,
391 message: "You have already pinned the maximum number of statuses"
398 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
399 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
401 cast(info, params, [:pinned_activities])
404 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
407 moderator: is_moderator
411 def add_reblog_mute(info, ap_id) do
412 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
414 cast(info, params, [:muted_reblogs])
417 def remove_reblog_mute(info, ap_id) do
418 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
420 cast(info, params, [:muted_reblogs])
423 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
424 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
425 def fields(%{fields: nil, source_data: %{"attachment" => attachment}}) do
426 limit = Pleroma.Config.get([:instance, :max_remote_account_fields], 0)
429 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
430 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
434 def fields(%{fields: nil}), do: []
436 def fields(%{fields: fields}), do: fields
438 def follow_information_update(info, params) do