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