Remove Reply-To from report emails
[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(: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 def add_to_note_count(info, number) do
99 set_note_count(info, info.note_count + number)
100 end
101
102 def set_note_count(info, number) do
103 params = %{note_count: Enum.max([0, number])}
104
105 info
106 |> cast(params, [:note_count])
107 |> validate_required([:note_count])
108 end
109
110 def set_follower_count(info, number) do
111 params = %{follower_count: Enum.max([0, number])}
112
113 info
114 |> cast(params, [:follower_count])
115 |> validate_required([:follower_count])
116 end
117
118 def set_mutes(info, mutes) do
119 params = %{mutes: mutes}
120
121 info
122 |> cast(params, [:mutes])
123 |> validate_required([:mutes])
124 end
125
126 @spec set_notification_mutes(Changeset.t(), [String.t()], boolean()) :: Changeset.t()
127 def set_notification_mutes(changeset, muted_notifications, notifications?) do
128 if notifications? do
129 put_change(changeset, :muted_notifications, muted_notifications)
130 |> validate_required([:muted_notifications])
131 else
132 changeset
133 end
134 end
135
136 def set_blocks(info, blocks) do
137 params = %{blocks: blocks}
138
139 info
140 |> cast(params, [:blocks])
141 |> validate_required([:blocks])
142 end
143
144 def set_subscribers(info, subscribers) do
145 params = %{subscribers: subscribers}
146
147 info
148 |> cast(params, [:subscribers])
149 |> validate_required([:subscribers])
150 end
151
152 @spec add_to_mutes(Info.t(), String.t()) :: Changeset.t()
153 def add_to_mutes(info, muted) do
154 set_mutes(info, Enum.uniq([muted | info.mutes]))
155 end
156
157 @spec add_to_muted_notifications(Changeset.t(), Info.t(), String.t(), boolean()) ::
158 Changeset.t()
159 def add_to_muted_notifications(changeset, info, muted, notifications?) do
160 set_notification_mutes(
161 changeset,
162 Enum.uniq([muted | info.muted_notifications]),
163 notifications?
164 )
165 end
166
167 @spec remove_from_mutes(Info.t(), String.t()) :: Changeset.t()
168 def remove_from_mutes(info, muted) do
169 set_mutes(info, List.delete(info.mutes, muted))
170 end
171
172 @spec remove_from_muted_notifications(Changeset.t(), Info.t(), String.t()) :: Changeset.t()
173 def remove_from_muted_notifications(changeset, info, muted) do
174 set_notification_mutes(changeset, List.delete(info.muted_notifications, muted), true)
175 end
176
177 def add_to_block(info, blocked) do
178 set_blocks(info, Enum.uniq([blocked | info.blocks]))
179 end
180
181 def remove_from_block(info, blocked) do
182 set_blocks(info, List.delete(info.blocks, blocked))
183 end
184
185 def add_to_subscribers(info, subscribed) do
186 set_subscribers(info, Enum.uniq([subscribed | info.subscribers]))
187 end
188
189 def remove_from_subscribers(info, subscribed) do
190 set_subscribers(info, List.delete(info.subscribers, subscribed))
191 end
192
193 def set_domain_blocks(info, domain_blocks) do
194 params = %{domain_blocks: domain_blocks}
195
196 info
197 |> cast(params, [:domain_blocks])
198 |> validate_required([:domain_blocks])
199 end
200
201 def add_to_domain_block(info, domain_blocked) do
202 set_domain_blocks(info, Enum.uniq([domain_blocked | info.domain_blocks]))
203 end
204
205 def remove_from_domain_block(info, domain_blocked) do
206 set_domain_blocks(info, List.delete(info.domain_blocks, domain_blocked))
207 end
208
209 def set_keys(info, keys) do
210 params = %{keys: keys}
211
212 info
213 |> cast(params, [:keys])
214 |> validate_required([:keys])
215 end
216
217 def remote_user_creation(info, params) do
218 info
219 |> cast(params, [
220 :ap_enabled,
221 :source_data,
222 :banner,
223 :locked,
224 :magic_key,
225 :uri,
226 :hub,
227 :topic,
228 :salmon,
229 :hide_followers,
230 :hide_follows,
231 :follower_count,
232 :following_count
233 ])
234 end
235
236 def user_upgrade(info, params) do
237 info
238 |> cast(params, [
239 :ap_enabled,
240 :source_data,
241 :banner,
242 :locked,
243 :magic_key,
244 :follower_count,
245 :following_count,
246 :hide_follows,
247 :hide_followers
248 ])
249 end
250
251 def profile_update(info, params) do
252 info
253 |> cast(params, [
254 :locked,
255 :no_rich_text,
256 :default_scope,
257 :banner,
258 :hide_follows,
259 :hide_followers,
260 :hide_favorites,
261 :background,
262 :show_role,
263 :skip_thread_containment,
264 :pleroma_settings_store
265 ])
266 end
267
268 @spec confirmation_changeset(Info.t(), keyword()) :: Changeset.t()
269 def confirmation_changeset(info, opts) do
270 need_confirmation? = Keyword.get(opts, :need_confirmation)
271
272 params =
273 if need_confirmation? do
274 %{
275 confirmation_pending: true,
276 confirmation_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64()
277 }
278 else
279 %{
280 confirmation_pending: false,
281 confirmation_token: nil
282 }
283 end
284
285 cast(info, params, [:confirmation_pending, :confirmation_token])
286 end
287
288 def mastodon_settings_update(info, settings) do
289 params = %{settings: settings}
290
291 info
292 |> cast(params, [:settings])
293 |> validate_required([:settings])
294 end
295
296 def mascot_update(info, url) do
297 params = %{mascot: url}
298
299 info
300 |> cast(params, [:mascot])
301 |> validate_required([:mascot])
302 end
303
304 def set_source_data(info, source_data) do
305 params = %{source_data: source_data}
306
307 info
308 |> cast(params, [:source_data])
309 |> validate_required([:source_data])
310 end
311
312 def admin_api_update(info, params) do
313 info
314 |> cast(params, [
315 :is_moderator,
316 :is_admin,
317 :show_role
318 ])
319 end
320
321 def add_pinnned_activity(info, %Pleroma.Activity{id: id}) do
322 if id not in info.pinned_activities do
323 max_pinned_statuses = Pleroma.Config.get([:instance, :max_pinned_statuses], 0)
324 params = %{pinned_activities: info.pinned_activities ++ [id]}
325
326 info
327 |> cast(params, [:pinned_activities])
328 |> validate_length(:pinned_activities,
329 max: max_pinned_statuses,
330 message: "You have already pinned the maximum number of statuses"
331 )
332 else
333 change(info)
334 end
335 end
336
337 def remove_pinnned_activity(info, %Pleroma.Activity{id: id}) do
338 params = %{pinned_activities: List.delete(info.pinned_activities, id)}
339
340 cast(info, params, [:pinned_activities])
341 end
342
343 def roles(%Info{is_moderator: is_moderator, is_admin: is_admin}) do
344 %{
345 admin: is_admin,
346 moderator: is_moderator
347 }
348 end
349
350 def add_reblog_mute(info, ap_id) do
351 params = %{muted_reblogs: info.muted_reblogs ++ [ap_id]}
352
353 cast(info, params, [:muted_reblogs])
354 end
355
356 def remove_reblog_mute(info, ap_id) do
357 params = %{muted_reblogs: List.delete(info.muted_reblogs, ap_id)}
358
359 cast(info, params, [:muted_reblogs])
360 end
361
362 def follow_information_update(info, params) do
363 info
364 |> cast(params, [
365 :hide_followers,
366 :hide_follows,
367 :follower_count,
368 :following_count
369 ])
370 end
371 end