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(:mascot, :map, default: nil)
47 field(:emoji, {:array, :map}, default: [])
48 field(:pleroma_settings_store, :map, default: %{})
50 field(:notification_settings, :map,
54 "non_follows" => true,
55 "non_followers" => true
59 field(:skip_thread_containment, :boolean, default: false)
62 # ap_id -> Where is this used?
63 # bio -> Where is this used?
64 # avatar -> Where is this used?
65 # fqn -> Where is this used?
66 # host -> Where is this used?
67 # subject _> Where is this used?
70 def set_activation_status(info, deactivated) do
71 params = %{deactivated: deactivated}
74 |> cast(params, [:deactivated])
75 |> validate_required([:deactivated])
78 def update_notification_settings(info, settings) do
81 |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
84 notification_settings =
85 info.notification_settings
86 |> Map.merge(settings)
87 |> Map.take(["followers", "follows", "non_follows", "non_followers"])
89 params = %{notification_settings: notification_settings}
92 |> cast(params, [:notification_settings])
93 |> validate_required([:notification_settings])
96 def add_to_note_count(info, number) do
97 set_note_count(info, info.note_count + number)
100 def set_note_count(info, number) do
101 params = %{note_count: Enum.max([0, number])}
104 |> cast(params, [:note_count])
105 |> validate_required([:note_count])
108 def set_follower_count(info, number) do
109 params = %{follower_count: Enum.max([0, number])}
112 |> cast(params, [:follower_count])
113 |> validate_required([:follower_count])
116 def set_mutes(info, mutes) do
117 params = %{mutes: mutes}
120 |> cast(params, [:mutes])
121 |> validate_required([:mutes])
124 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
125 def set_notification_mutes(changeset, muted_notifications, notifications?) do
127 put_change(changeset, :muted_notifications, muted_notifications)
128 |> validate_required([:muted_notifications])
134 def set_blocks(info, blocks) do
135 params = %{blocks: blocks}
138 |> cast(params, [:blocks])
139 |> validate_required([:blocks])
142 def set_subscribers(info, subscribers) do
143 params = %{subscribers: subscribers}
146 |> cast(params, [:subscribers])
147 |> validate_required([:subscribers])
150 @spec add_to_mutes(Info.t(), String.t()) :: Changeset.t()
151 def add_to_mutes(info, muted) do
152 set_mutes(info, Enum.uniq([muted | info.mutes]))
155 @spec add_to_muted_notifications(Changeset.t(), Info.t(), String.t(), boolean()) ::
157 def add_to_muted_notifications(changeset, info, muted, notifications?) do
158 set_notification_mutes(
160 Enum.uniq([muted | info.muted_notifications]),
165 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
166 def remove_from_mutes(info, muted) do
167 set_mutes(info, List.delete(info.mutes, muted))
170 @spec remove_from_muted_notifications(Changeset.t(), Info.t(), String.t()) :: Changeset.t()
171 def remove_from_muted_notifications(changeset, info, muted) do
172 set_notification_mutes(changeset, List.delete(info.muted_notifications, muted), true)
175 def add_to_block(info, blocked) do
176 set_blocks(info, Enum.uniq([blocked | info.blocks]))
179 def remove_from_block(info, blocked) do
180 set_blocks(info, List.delete(info.blocks, blocked))
183 def add_to_subscribers(info, subscribed) do
184 set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
187 def remove_from_subscribers(info, subscribed) do
188 set_subscribers(info, List.delete(info.subscribers, subscribed))
191 def set_domain_blocks(info, domain_blocks) do
192 params = %{domain_blocks: domain_blocks}
195 |> cast(params, [:domain_blocks])
196 |> validate_required([:domain_blocks])
199 def add_to_domain_block(info, domain_blocked) do
200 set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
203 def remove_from_domain_block(info, domain_blocked) do
204 set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
207 def set_keys(info, keys) do
208 params = %{keys: keys}
211 |> cast(params, [:keys])
212 |> validate_required([:keys])
215 def remote_user_creation(info, params) do
230 def user_upgrade(info, params) do
241 def profile_update(info, params) do
253 :skip_thread_containment,
254 :pleroma_settings_store
258 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
259 def confirmation_changeset(info, opts) do
260 need_confirmation? = Keyword.get(opts, :need_confirmation)
263 if need_confirmation? do
265 confirmation_pending: true,
266 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
270 confirmation_pending: false,
271 confirmation_token: nil
275 cast(info, params, [:confirmation_pending, :confirmation_token])
278 def mastodon_settings_update(info, settings) do
279 params = %{settings: settings}
282 |> cast(params, [:settings])
283 |> validate_required([:settings])
286 def mascot_update(info, url) do
287 params = %{mascot: url}
290 |> cast(params, [:mascot])
291 |> validate_required([:mascot])
294 def set_source_data(info, source_data) do
295 params = %{source_data: source_data}
298 |> cast(params, [:source_data])
299 |> validate_required([:source_data])
302 def admin_api_update(info, params) do
311 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
312 if id not in info.pinned_activities do
313 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
314 params = %{pinned_activities: info.pinned_activities ++ [id]}
317 |> cast(params, [:pinned_activities])
318 |> validate_length(:pinned_activities,
319 max: max_pinned_statuses,
320 message: "You have already pinned the maximum number of statuses"
327 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
328 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
330 cast(info, params, [:pinned_activities])
333 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
336 moderator: is_moderator
340 def add_reblog_mute(info, ap_id) do
341 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
343 cast(info, params, [:muted_reblogs])
346 def remove_reblog_mute(info, ap_id) do
347 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
349 cast(info, params, [:muted_reblogs])