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