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