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