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