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