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(: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])
98 def add_to_note_count(info, number) do
99 set_note_count(info, info.note_count + number)
102 def set_note_count(info, number) do
103 params = %{note_count: Enum.max([0, number])}
106 |> cast(params, [:note_count])
107 |> validate_required([:note_count])
110 def set_follower_count(info, number) do
111 params = %{follower_count: Enum.max([0, number])}
114 |> cast(params, [:follower_count])
115 |> validate_required([:follower_count])
118 def set_mutes(info, mutes) do
119 params = %{mutes: mutes}
122 |> cast(params, [:mutes])
123 |> validate_required([:mutes])
126 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
127 def set_notification_mutes(changeset, muted_notifications, notifications?) do
129 put_change(changeset, :muted_notifications, muted_notifications)
130 |> validate_required([:muted_notifications])
136 def set_blocks(info, blocks) do
137 params = %{blocks: blocks}
140 |> cast(params, [:blocks])
141 |> validate_required([:blocks])
144 def set_subscribers(info, subscribers) do
145 params = %{subscribers: subscribers}
148 |> cast(params, [:subscribers])
149 |> validate_required([:subscribers])
152 @spec add_to_mutes(Info.t(), String.t()) :: Changeset.t()
153 def add_to_mutes(info, muted) do
154 set_mutes(info, Enum.uniq([muted | info.mutes]))
157 @spec add_to_muted_notifications(Changeset.t(), Info.t(), String.t(), boolean()) ::
159 def add_to_muted_notifications(changeset, info, muted, notifications?) do
160 set_notification_mutes(
162 Enum.uniq([muted | info.muted_notifications]),
167 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
168 def remove_from_mutes(info, muted) do
169 set_mutes(info, List.delete(info.mutes, muted))
172 @spec remove_from_muted_notifications(Changeset.t(), Info.t(), String.t()) :: Changeset.t()
173 def remove_from_muted_notifications(changeset, info, muted) do
174 set_notification_mutes(changeset, List.delete(info.muted_notifications, muted), true)
177 def add_to_block(info, blocked) do
178 set_blocks(info, Enum.uniq([blocked | info.blocks]))
181 def remove_from_block(info, blocked) do
182 set_blocks(info, List.delete(info.blocks, blocked))
185 def add_to_subscribers(info, subscribed) do
186 set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
189 def remove_from_subscribers(info, subscribed) do
190 set_subscribers(info, List.delete(info.subscribers, subscribed))
193 def set_domain_blocks(info, domain_blocks) do
194 params = %{domain_blocks: domain_blocks}
197 |> cast(params, [:domain_blocks])
198 |> validate_required([:domain_blocks])
201 def add_to_domain_block(info, domain_blocked) do
202 set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
205 def remove_from_domain_block(info, domain_blocked) do
206 set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
209 def set_keys(info, keys) do
210 params = %{keys: keys}
213 |> cast(params, [:keys])
214 |> validate_required([:keys])
217 def remote_user_creation(info, params) do
236 def user_upgrade(info, params) do
251 def profile_update(info, params) do
263 :skip_thread_containment,
264 :pleroma_settings_store
268 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
269 def confirmation_changeset(info, opts) do
270 need_confirmation? = Keyword.get(opts, :need_confirmation)
273 if need_confirmation? do
275 confirmation_pending: true,
276 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
280 confirmation_pending: false,
281 confirmation_token: nil
285 cast(info, params, [:confirmation_pending, :confirmation_token])
288 def mastodon_settings_update(info, settings) do
289 params = %{settings: settings}
292 |> cast(params, [:settings])
293 |> validate_required([:settings])
296 def mascot_update(info, url) do
297 params = %{mascot: url}
300 |> cast(params, [:mascot])
301 |> validate_required([:mascot])
304 def set_source_data(info, source_data) do
305 params = %{source_data: source_data}
308 |> cast(params, [:source_data])
309 |> validate_required([:source_data])
312 def admin_api_update(info, params) do
321 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
322 if id not in info.pinned_activities do
323 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
324 params = %{pinned_activities: info.pinned_activities ++ [id]}
327 |> cast(params, [:pinned_activities])
328 |> validate_length(:pinned_activities,
329 max: max_pinned_statuses,
330 message: "You have already pinned the maximum number of statuses"
337 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
338 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
340 cast(info, params, [:pinned_activities])
343 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
346 moderator: is_moderator
350 def add_reblog_mute(info, ap_id) do
351 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
353 cast(info, params, [:muted_reblogs])
356 def remove_reblog_mute(info, ap_id) do
357 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
359 cast(info, params, [:muted_reblogs])
362 def follow_information_update(info, params) do