Merge branch 'develop' into tests/mastodon_api_controller.ex
[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(: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_count, :boolean, default: false)
45 field(:hide_follows_count, :boolean, default: false)
46 field(:hide_followers, :boolean, default: false)
47 field(:hide_follows, :boolean, default: false)
48 field(:hide_favorites, :boolean, default: true)
49 field(:pinned_activities, {:array, :string}, default: [])
50 field(:email_notifications, :map, default: %{"digest" => false})
51 field(:mascot, :map, default: nil)
52 field(:emoji, {:array, :map}, default: [])
53 field(:pleroma_settings_store, :map, default: %{})
54 field(:fields, {:array, :map}, default: nil)
55 field(:raw_fields, {:array, :map}, default: [])
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 update_notification_settings(info, settings) do
86 settings =
87 settings
88 |> Enum.map(fn {k, v} -> {k, v in [true, "true", "True", "1"]} end)
89 |> Map.new()
90
91 notification_settings =
92 info.notification_settings
93 |> Map.merge(settings)
94 |> Map.take(["followers", "follows", "non_follows", "non_followers"])
95
96 params = %{notification_settings: notification_settings}
97
98 info
99 |> cast(params, [:notification_settings])
100 |> validate_required([:notification_settings])
101 end
102
103 @doc """
104 Update email notifications in the given User.Info struct.
105
106 Examples:
107
108 iex> update_email_notifications(%Pleroma.User.Info{email_notifications: %{"digest" => false}}, %{"digest" => true})
109 %Pleroma.User.Info{email_notifications: %{"digest" => true}}
110
111 """
112 @spec update_email_notifications(t(), map()) :: Ecto.Changeset.t()
113 def update_email_notifications(info, settings) do
114 email_notifications =
115 info.email_notifications
116 |> Map.merge(settings)
117 |> Map.take(["digest"])
118
119 params = %{email_notifications: email_notifications}
120 fields = [:email_notifications]
121
122 info
123 |> cast(params, fields)
124 |> validate_required(fields)
125 end
126
127 def add_to_note_count(info, number) do
128 set_note_count(info, info.note_count + number)
129 end
130
131 def set_note_count(info, number) do
132 params = %{note_count: Enum.max([0, number])}
133
134 info
135 |> cast(params, [:note_count])
136 |> validate_required([:note_count])
137 end
138
139 def set_follower_count(info, number) do
140 params = %{follower_count: Enum.max([0, number])}
141
142 info
143 |> cast(params, [:follower_count])
144 |> validate_required([:follower_count])
145 end
146
147 def set_mutes(info, mutes) do
148 params = %{mutes: mutes}
149
150 info
151 |> cast(params, [:mutes])
152 |> validate_required([:mutes])
153 end
154
155 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
156 def set_notification_mutes(changeset, muted_notifications, notifications?) do
157 if notifications? do
158 put_change(changeset, :muted_notifications, muted_notifications)
159 |> validate_required([:muted_notifications])
160 else
161 changeset
162 end
163 end
164
165 def set_blocks(info, blocks) do
166 params = %{blocks: blocks}
167
168 info
169 |> cast(params, [:blocks])
170 |> validate_required([:blocks])
171 end
172
173 def set_subscribers(info, subscribers) do
174 params = %{subscribers: subscribers}
175
176 info
177 |> cast(params, [:subscribers])
178 |> validate_required([:subscribers])
179 end
180
181 @spec add_to_mutes(Info.t(), String.t()) :: Changeset.t()
182 def add_to_mutes(info, muted) do
183 set_mutes(info, Enum.uniq([muted | info.mutes]))
184 end
185
186 @spec add_to_muted_notifications(Changeset.t(), Info.t(), String.t(), boolean()) ::
187 Changeset.t()
188 def add_to_muted_notifications(changeset, info, muted, notifications?) do
189 set_notification_mutes(
190 changeset,
191 Enum.uniq([muted | info.muted_notifications]),
192 notifications?
193 )
194 end
195
196 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
197 def remove_from_mutes(info, muted) do
198 set_mutes(info, List.delete(info.mutes, muted))
199 end
200
201 @spec remove_from_muted_notifications(Changeset.t(), Info.t(), String.t()) :: Changeset.t()
202 def remove_from_muted_notifications(changeset, info, muted) do
203 set_notification_mutes(changeset, 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 :hub,
263 :topic,
264 :salmon,
265 :hide_followers,
266 :hide_follows,
267 :hide_followers_count,
268 :hide_follows_count,
269 :follower_count,
270 :fields,
271 :following_count
272 ])
273 |> validate_fields(true)
274 end
275
276 def user_upgrade(info, params, remote? \\ false) do
277 info
278 |> cast(params, [
279 :ap_enabled,
280 :source_data,
281 :banner,
282 :locked,
283 :magic_key,
284 :follower_count,
285 :following_count,
286 :hide_follows,
287 :fields,
288 :hide_followers,
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 ])
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) &&
337 is_binary(value) &&
338 String.length(name) <= name_limit &&
339 String.length(value) <= value_limit
340 end
341
342 defp valid_field?(_), do: false
343
344 defp truncate_field(%{"name" => name, "value" => value}) do
345 {name, _chopped} =
346 String.split_at(name, Pleroma.Config.get([:instance, :account_field_name_length], 255))
347
348 {value, _chopped} =
349 String.split_at(value, Pleroma.Config.get([:instance, :account_field_value_length], 255))
350
351 %{"name" => name, "value" => value}
352 end
353
354 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
355 def confirmation_changeset(info, opts) do
356 need_confirmation? = Keyword.get(opts, :need_confirmation)
357
358 params =
359 if need_confirmation? do
360 %{
361 confirmation_pending: true,
362 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
363 }
364 else
365 %{
366 confirmation_pending: false,
367 confirmation_token: nil
368 }
369 end
370
371 cast(info, params, [:confirmation_pending, :confirmation_token])
372 end
373
374 def mastodon_settings_update(info, settings) do
375 params = %{settings: settings}
376
377 info
378 |> cast(params, [:settings])
379 |> validate_required([:settings])
380 end
381
382 def mascot_update(info, url) do
383 params = %{mascot: url}
384
385 info
386 |> cast(params, [:mascot])
387 |> validate_required([:mascot])
388 end
389
390 def set_source_data(info, source_data) do
391 params = %{source_data: source_data}
392
393 info
394 |> cast(params, [:source_data])
395 |> validate_required([:source_data])
396 end
397
398 def admin_api_update(info, params) do
399 info
400 |> cast(params, [
401 :is_moderator,
402 :is_admin,
403 :show_role
404 ])
405 end
406
407 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
408 if id not in info.pinned_activities do
409 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
410 params = %{pinned_activities: info.pinned_activities ++ [id]}
411
412 info
413 |> cast(params, [:pinned_activities])
414 |> validate_length(:pinned_activities,
415 max: max_pinned_statuses,
416 message: "You have already pinned the maximum number of statuses"
417 )
418 else
419 change(info)
420 end
421 end
422
423 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
424 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
425
426 cast(info, params, [:pinned_activities])
427 end
428
429 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
430 %{
431 admin: is_admin,
432 moderator: is_moderator
433 }
434 end
435
436 def add_reblog_mute(info, ap_id) do
437 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
438
439 cast(info, params, [:muted_reblogs])
440 end
441
442 def remove_reblog_mute(info, ap_id) do
443 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
444
445 cast(info, params, [:muted_reblogs])
446 end
447
448 # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``.
449 # For example: [{"name": "Pronoun", "value": "she/her"}, …]
450 def fields(%{fields: nil, source_data: %{"attachment" => attachment}}) do
451 limit = Pleroma.Config.get([:instance, :max_remote_account_fields], 0)
452
453 attachment
454 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
455 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
456 |> Enum.take(limit)
457 end
458
459 def fields(%{fields: nil}), do: []
460
461 def fields(%{fields: fields}), do: fields
462
463 def follow_information_update(info, params) do
464 info
465 |> cast(params, [
466 :hide_followers,
467 :hide_follows,
468 :follower_count,
469 :following_count,
470 :hide_followers_count,
471 :hide_follows_count
472 ])
473 end
474 end