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 field(:locked, :boolean, default: false)
20 field(:confirmation_pending, :boolean, default: false)
21 field(:confirmation_token, :string, default: nil)
22 field(:default_scope, :string, default: "public")
23 field(:blocks, {:array, :string}, default: [])
24 field(:domain_blocks, {:array, :string}, default: [])
25 field(:mutes, {:array, :string}, default: [])
26 field(:muted_reblogs, {:array, :string}, default: [])
27 field(:muted_notifications, {:array, :string}, default: [])
28 field(:subscribers, {:array, :string}, default: [])
29 field(:deactivated, :boolean, default: false)
30 field(:no_rich_text, :boolean, default: false)
31 field(:ap_enabled, :boolean, default: false)
32 field(:is_moderator, :boolean, default: false)
33 field(:is_admin, :boolean, default: false)
34 field(:show_role, :boolean, default: true)
35 field(:keys, :string, default: nil)
36 field(:settings, :map, default: nil)
37 field(:magic_key, :string, default: nil)
38 field(:uri, :string, default: nil)
39 field(:topic, :string, default: nil)
40 field(:hub, :string, default: nil)
41 field(:salmon, :string, default: nil)
42 field(:hide_followers, :boolean, default: false)
43 field(:hide_follows, :boolean, default: false)
44 field(:hide_favorites, :boolean, default: true)
45 field(:pinned_activities, {:array, :string}, default: [])
46 field(:flavour, :string, default: nil)
47 field(:email_notifications, :map, default: %{"digest" => false})
48 field(:mascot, :map, default: nil)
49 field(:emoji, {:array, :map}, default: [])
50 field(:pleroma_settings_store, :map, default: %{})
52 field(:notification_settings, :map,
56 "non_follows" => true,
57 "non_followers" => true
61 field(:skip_thread_containment, :boolean, default: false)
64 # ap_id -> Where is this used?
65 # bio -> Where is this used?
66 # avatar -> Where is this used?
67 # fqn -> Where is this used?
68 # host -> Where is this used?
69 # subject _> Where is this used?
72 def set_activation_status(info, deactivated) do
73 params = %{deactivated: deactivated}
76 |> cast(params, [:deactivated])
77 |> validate_required([:deactivated])
80 def update_notification_settings(info, settings) do
83 |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
86 notification_settings =
87 info.notification_settings
88 |> Map.merge(settings)
89 |> Map.take(["followers", "follows", "non_follows", "non_followers"])
91 params = %{notification_settings: notification_settings}
94 |> cast(params, [:notification_settings])
95 |> validate_required([:notification_settings])
99 Update email notifications in the given User.Info struct.
103 iex> update_email_notifications(%Pleroma.User.Info{email_notifications: %{"digest" => false}}, %{"digest" => true})
104 %Pleroma.User.Info{email_notifications: %{"digest" => true}}
107 @spec update_email_notifications(t(), map()) :: Ecto.Changeset.t()
108 def update_email_notifications(info, settings) do
109 email_notifications =
110 info.email_notifications
111 |> Map.merge(settings)
112 |> Map.take(["digest"])
114 params = %{email_notifications: email_notifications}
115 fields = [:email_notifications]
118 |> cast(params, fields)
119 |> validate_required(fields)
122 def add_to_note_count(info, number) do
123 set_note_count(info, info.note_count + number)
126 def set_note_count(info, number) do
127 params = %{note_count: Enum.max([0, number])}
130 |> cast(params, [:note_count])
131 |> validate_required([:note_count])
134 def set_follower_count(info, number) do
135 params = %{follower_count: Enum.max([0, number])}
138 |> cast(params, [:follower_count])
139 |> validate_required([:follower_count])
142 def set_mutes(info, mutes) do
143 params = %{mutes: mutes}
146 |> cast(params, [:mutes])
147 |> validate_required([:mutes])
150 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
151 def set_notification_mutes(changeset, muted_notifications, notifications?) do
153 put_change(changeset, :muted_notifications, muted_notifications)
154 |> validate_required([:muted_notifications])
160 def set_blocks(info, blocks) do
161 params = %{blocks: blocks}
164 |> cast(params, [:blocks])
165 |> validate_required([:blocks])
168 def set_subscribers(info, subscribers) do
169 params = %{subscribers: subscribers}
172 |> cast(params, [:subscribers])
173 |> validate_required([:subscribers])
176 @spec add_to_mutes(Info.t(), String.t()) :: Changeset.t()
177 def add_to_mutes(info, muted) do
178 set_mutes(info, Enum.uniq([muted | info.mutes]))
181 @spec add_to_muted_notifications(Changeset.t(), Info.t(), String.t(), boolean()) ::
183 def add_to_muted_notifications(changeset, info, muted, notifications?) do
184 set_notification_mutes(
186 Enum.uniq([muted | info.muted_notifications]),
191 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
192 def remove_from_mutes(info, muted) do
193 set_mutes(info, List.delete(info.mutes, muted))
196 @spec remove_from_muted_notifications(Changeset.t(), Info.t(), String.t()) :: Changeset.t()
197 def remove_from_muted_notifications(changeset, info, muted) do
198 set_notification_mutes(changeset, List.delete(info.muted_notifications, muted), true)
201 def add_to_block(info, blocked) do
202 set_blocks(info, Enum.uniq([blocked | info.blocks]))
205 def remove_from_block(info, blocked) do
206 set_blocks(info, List.delete(info.blocks, blocked))
209 def add_to_subscribers(info, subscribed) do
210 set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
213 def remove_from_subscribers(info, subscribed) do
214 set_subscribers(info, List.delete(info.subscribers, subscribed))
217 def set_domain_blocks(info, domain_blocks) do
218 params = %{domain_blocks: domain_blocks}
221 |> cast(params, [:domain_blocks])
222 |> validate_required([:domain_blocks])
225 def add_to_domain_block(info, domain_blocked) do
226 set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
229 def remove_from_domain_block(info, domain_blocked) do
230 set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
233 def set_keys(info, keys) do
234 params = %{keys: keys}
237 |> cast(params, [:keys])
238 |> validate_required([:keys])
241 def remote_user_creation(info, params) do
256 def user_upgrade(info, params) do
267 def profile_update(info, params) do
279 :skip_thread_containment,
280 :pleroma_settings_store
284 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
285 def confirmation_changeset(info, opts) do
286 need_confirmation? = Keyword.get(opts, :need_confirmation)
289 if need_confirmation? do
291 confirmation_pending: true,
292 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
296 confirmation_pending: false,
297 confirmation_token: nil
301 cast(info, params, [:confirmation_pending, :confirmation_token])
304 def mastodon_settings_update(info, settings) do
305 params = %{settings: settings}
308 |> cast(params, [:settings])
309 |> validate_required([:settings])
312 def mascot_update(info, url) do
313 params = %{mascot: url}
316 |> cast(params, [:mascot])
317 |> validate_required([:mascot])
320 def set_source_data(info, source_data) do
321 params = %{source_data: source_data}
324 |> cast(params, [:source_data])
325 |> validate_required([:source_data])
328 def admin_api_update(info, params) do
337 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
338 if id not in info.pinned_activities do
339 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
340 params = %{pinned_activities: info.pinned_activities ++ [id]}
343 |> cast(params, [:pinned_activities])
344 |> validate_length(:pinned_activities,
345 max: max_pinned_statuses,
346 message: "You have already pinned the maximum number of statuses"
353 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
354 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
356 cast(info, params, [:pinned_activities])
359 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
362 moderator: is_moderator
366 def add_reblog_mute(info, ap_id) do
367 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
369 cast(info, params, [:muted_reblogs])
372 def remove_reblog_mute(info, ap_id) do
373 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
375 cast(info, params, [:muted_reblogs])