remove Salmon module
[akkoma] / lib / pleroma / user / info.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.User.Info do
6 use Ecto.Schema
7 import Ecto.Changeset
8
9 alias Pleroma.User.Info
10
11 @type t :: %__MODULE__{}
12
13 embedded_schema do
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(:password_reset_pending, :boolean, default: false)
24 field(:confirmation_token, :string, default: nil)
25 field(:default_scope, :string, default: "public")
26 field(:blocks, {:array, :string}, default: [])
27 field(:domain_blocks, {:array, :string}, default: [])
28 field(:mutes, {:array, :string}, default: [])
29 field(:muted_reblogs, {:array, :string}, default: [])
30 field(:muted_notifications, {:array, :string}, default: [])
31 field(:subscribers, {:array, :string}, default: [])
32 field(:deactivated, :boolean, default: false)
33 field(:no_rich_text, :boolean, default: false)
34 field(:ap_enabled, :boolean, default: false)
35 field(:is_moderator, :boolean, default: false)
36 field(:is_admin, :boolean, default: false)
37 field(:show_role, :boolean, default: true)
38 field(:keys, :string, default: nil)
39 field(:settings, :map, default: nil)
40 field(:magic_key, :string, default: nil)
41 field(:uri, :string, default: nil)
42 field(:hide_followers_count, :boolean, default: false)
43 field(:hide_follows_count, :boolean, default: false)
44 field(:hide_followers, :boolean, default: false)
45 field(:hide_follows, :boolean, default: false)
46 field(:hide_favorites, :boolean, default: true)
47 field(:unread_conversation_count, :integer, default: 0)
48 field(:pinned_activities, {:array, :string}, default: [])
49 field(:email_notifications, :map, default: %{"digest" => false})
50 field(:mascot, :map, default: nil)
51 field(:emoji, {:array, :map}, default: [])
52 field(:pleroma_settings_store, :map, default: %{})
53 field(:fields, {:array, :map}, default: nil)
54 field(:raw_fields, {:array, :map}, default: [])
55 field(:discoverable, :boolean, default: false)
56
57 field(:notification_settings, :map,
58 default: %{
59 "followers" => true,
60 "follows" => true,
61 "non_follows" => true,
62 "non_followers" => true
63 }
64 )
65
66 field(:skip_thread_containment, :boolean, default: false)
67
68 # Found in the wild
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?
75 end
76
77 def set_activation_status(info, deactivated) do
78 params = %{deactivated: deactivated}
79
80 info
81 |> cast(params, [:deactivated])
82 |> validate_required([:deactivated])
83 end
84
85 def set_password_reset_pending(info, pending) do
86 params = %{password_reset_pending: pending}
87
88 info
89 |> cast(params, [:password_reset_pending])
90 |> validate_required([:password_reset_pending])
91 end
92
93 def update_notification_settings(info, settings) do
94 settings =
95 settings
96 |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
97 |> Map.new()
98
99 notification_settings =
100 info.notification_settings
101 |> Map.merge(settings)
102 |> Map.take(["followers", "follows", "non_follows", "non_followers"])
103
104 params = %{notification_settings: notification_settings}
105
106 info
107 |> cast(params, [:notification_settings])
108 |> validate_required([:notification_settings])
109 end
110
111 @doc """
112 Update email notifications in the given User.Info struct.
113
114 Examples:
115
116 iex> update_email_notifications(%Pleroma.User.Info{email_notifications: %{"digest" => false}}, %{"digest" => true})
117 %Pleroma.User.Info{email_notifications: %{"digest" => true}}
118
119 """
120 @spec update_email_notifications(t(), map()) :: Ecto.Changeset.t()
121 def update_email_notifications(info, settings) do
122 email_notifications =
123 info.email_notifications
124 |> Map.merge(settings)
125 |> Map.take(["digest"])
126
127 params = %{email_notifications: email_notifications}
128 fields = [:email_notifications]
129
130 info
131 |> cast(params, fields)
132 |> validate_required(fields)
133 end
134
135 def add_to_note_count(info, number) do
136 set_note_count(info, info.note_count + number)
137 end
138
139 def set_note_count(info, number) do
140 params = %{note_count: Enum.max([0, number])}
141
142 info
143 |> cast(params, [:note_count])
144 |> validate_required([:note_count])
145 end
146
147 def set_follower_count(info, number) do
148 params = %{follower_count: Enum.max([0, number])}
149
150 info
151 |> cast(params, [:follower_count])
152 |> validate_required([:follower_count])
153 end
154
155 def set_mutes(info, mutes) do
156 params = %{mutes: mutes}
157
158 info
159 |> cast(params, [:mutes])
160 |> validate_required([:mutes])
161 end
162
163 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
164 def set_notification_mutes(changeset, muted_notifications, notifications?) do
165 if notifications? do
166 put_change(changeset, :muted_notifications, muted_notifications)
167 |> validate_required([:muted_notifications])
168 else
169 changeset
170 end
171 end
172
173 def set_blocks(info, blocks) do
174 params = %{blocks: blocks}
175
176 info
177 |> cast(params, [:blocks])
178 |> validate_required([:blocks])
179 end
180
181 def set_subscribers(info, subscribers) do
182 params = %{subscribers: subscribers}
183
184 info
185 |> cast(params, [:subscribers])
186 |> validate_required([:subscribers])
187 end
188
189 @spec add_to_mutes(Info.t(), String.t(), boolean()) :: Changeset.t()
190 def add_to_mutes(info, muted, notifications?) do
191 info
192 |> set_mutes(Enum.uniq([muted | info.mutes]))
193 |> set_notification_mutes(
194 Enum.uniq([muted | info.muted_notifications]),
195 notifications?
196 )
197 end
198
199 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
200 def remove_from_mutes(info, muted) do
201 info
202 |> set_mutes(List.delete(info.mutes, muted))
203 |> set_notification_mutes(List.delete(info.muted_notifications, muted), true)
204 end
205
206 def add_to_block(info, blocked) do
207 set_blocks(info, Enum.uniq([blocked | info.blocks]))
208 end
209
210 def remove_from_block(info, blocked) do
211 set_blocks(info, List.delete(info.blocks, blocked))
212 end
213
214 def add_to_subscribers(info, subscribed) do
215 set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
216 end
217
218 def remove_from_subscribers(info, subscribed) do
219 set_subscribers(info, List.delete(info.subscribers, subscribed))
220 end
221
222 def set_domain_blocks(info, domain_blocks) do
223 params = %{domain_blocks: domain_blocks}
224
225 info
226 |> cast(params, [:domain_blocks])
227 |> validate_required([:domain_blocks])
228 end
229
230 def add_to_domain_block(info, domain_blocked) do
231 set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
232 end
233
234 def remove_from_domain_block(info, domain_blocked) do
235 set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
236 end
237
238 def set_keys(info, keys) do
239 params = %{keys: keys}
240
241 info
242 |> cast(params, [:keys])
243 |> validate_required([:keys])
244 end
245
246 def remote_user_creation(info, params) do
247 params =
248 if Map.has_key?(params, :fields) do
249 Map.put(params, :fields, Enum.map(params[:fields], &truncate_field/1))
250 else
251 params
252 end
253
254 info
255 |> cast(params, [
256 :ap_enabled,
257 :source_data,
258 :banner,
259 :locked,
260 :magic_key,
261 :uri,
262 :hide_followers,
263 :hide_follows,
264 :hide_followers_count,
265 :hide_follows_count,
266 :follower_count,
267 :fields,
268 :following_count,
269 :discoverable
270 ])
271 |> validate_fields(true)
272 end
273
274 def user_upgrade(info, params, remote? \\ false) do
275 info
276 |> cast(params, [
277 :ap_enabled,
278 :source_data,
279 :banner,
280 :locked,
281 :magic_key,
282 :follower_count,
283 :following_count,
284 :hide_follows,
285 :fields,
286 :hide_followers,
287 :discoverable,
288 :hide_followers_count,
289 :hide_follows_count
290 ])
291 |> validate_fields(remote?)
292 end
293
294 def profile_update(info, params) do
295 info
296 |> cast(params, [
297 :locked,
298 :no_rich_text,
299 :default_scope,
300 :banner,
301 :hide_follows,
302 :hide_followers,
303 :hide_followers_count,
304 :hide_follows_count,
305 :hide_favorites,
306 :background,
307 :show_role,
308 :skip_thread_containment,
309 :fields,
310 :raw_fields,
311 :pleroma_settings_store,
312 :discoverable
313 ])
314 |> validate_fields()
315 end
316
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)
320
321 changeset
322 |> validate_length(:fields, max: limit)
323 |> validate_change(:fields, fn :fields, fields ->
324 if Enum.all?(fields, &valid_field?/1) do
325 []
326 else
327 [fields: "invalid"]
328 end
329 end)
330 end
331
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)
335
336 is_binary(name) && is_binary(value) && String.length(name) <= name_limit &&
337 String.length(value) <= value_limit
338 end
339
340 defp valid_field?(_), do: false
341
342 defp truncate_field(%{"name" => name, "value" => value}) do
343 {name, _chopped} =
344 String.split_at(name, Pleroma.Config.get([:instance, :account_field_name_length], 255))
345
346 {value, _chopped} =
347 String.split_at(value, Pleroma.Config.get([:instance, :account_field_value_length], 255))
348
349 %{"name" => name, "value" => value}
350 end
351
352 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
353 def confirmation_changeset(info, opts) do
354 need_confirmation? = Keyword.get(opts, :need_confirmation)
355
356 params =
357 if need_confirmation? do
358 %{
359 confirmation_pending: true,
360 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
361 }
362 else
363 %{
364 confirmation_pending: false,
365 confirmation_token: nil
366 }
367 end
368
369 cast(info, params, [:confirmation_pending, :confirmation_token])
370 end
371
372 def mastodon_settings_update(info, settings) do
373 params = %{settings: settings}
374
375 info
376 |> cast(params, [:settings])
377 |> validate_required([:settings])
378 end
379
380 def mascot_update(info, url) do
381 params = %{mascot: url}
382
383 info
384 |> cast(params, [:mascot])
385 |> validate_required([:mascot])
386 end
387
388 def set_source_data(info, source_data) do
389 params = %{source_data: source_data}
390
391 info
392 |> cast(params, [:source_data])
393 |> validate_required([:source_data])
394 end
395
396 def admin_api_update(info, params) do
397 info
398 |> cast(params, [
399 :is_moderator,
400 :is_admin,
401 :show_role
402 ])
403 end
404
405 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
406 if id not in info.pinned_activities do
407 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
408 params = %{pinned_activities: info.pinned_activities ++ [id]}
409
410 info
411 |> cast(params, [:pinned_activities])
412 |> validate_length(:pinned_activities,
413 max: max_pinned_statuses,
414 message: "You have already pinned the maximum number of statuses"
415 )
416 else
417 change(info)
418 end
419 end
420
421 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
422 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
423
424 cast(info, params, [:pinned_activities])
425 end
426
427 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
428 %{
429 admin: is_admin,
430 moderator: is_moderator
431 }
432 end
433
434 def add_reblog_mute(info, ap_id) do
435 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
436
437 cast(info, params, [:muted_reblogs])
438 end
439
440 def remove_reblog_mute(info, ap_id) do
441 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
442
443 cast(info, params, [:muted_reblogs])
444 end
445
446 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
447 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
448 def fields(%{fields: nil, source_data: %{"attachment" => attachment}}) do
449 limit = Pleroma.Config.get([:instance, :max_remote_account_fields], 0)
450
451 attachment
452 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
453 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
454 |> Enum.take(limit)
455 end
456
457 def fields(%{fields: nil}), do: []
458
459 def fields(%{fields: fields}), do: fields
460
461 def follow_information_update(info, params) do
462 info
463 |> cast(params, [
464 :hide_followers,
465 :hide_follows,
466 :follower_count,
467 :following_count,
468 :hide_followers_count,
469 :hide_follows_count
470 ])
471 end
472 end