Factory: Add report_activity_factory
[akkoma] / test / support / factory.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Factory do
6 use ExMachina.Ecto, repo: Pleroma.Repo
7 alias Pleroma.Object
8 alias Pleroma.User
9
10 def participation_factory do
11 conversation = insert(:conversation)
12 user = insert(:user)
13
14 %Pleroma.Conversation.Participation{
15 conversation: conversation,
16 user: user,
17 read: false
18 }
19 end
20
21 def conversation_factory do
22 %Pleroma.Conversation{
23 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
24 }
25 end
26
27 def user_factory do
28 user = %User{
29 name: sequence(:name, &"Test テスト User #{&1}"),
30 email: sequence(:email, &"user#{&1}@example.com"),
31 nickname: sequence(:nickname, &"nick#{&1}"),
32 password_hash: Pbkdf2.hash_pwd_salt("test"),
33 bio: sequence(:bio, &"Tester Number #{&1}"),
34 last_digest_emailed_at: NaiveDateTime.utc_now(),
35 last_refreshed_at: NaiveDateTime.utc_now(),
36 notification_settings: %Pleroma.User.NotificationSetting{},
37 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
38 ap_enabled: true
39 }
40
41 %{
42 user
43 | ap_id: User.ap_id(user),
44 follower_address: User.ap_followers(user),
45 following_address: User.ap_following(user),
46 raw_bio: user.bio
47 }
48 end
49
50 def user_relationship_factory(attrs \\ %{}) do
51 source = attrs[:source] || insert(:user)
52 target = attrs[:target] || insert(:user)
53 relationship_type = attrs[:relationship_type] || :block
54
55 %Pleroma.UserRelationship{
56 source_id: source.id,
57 target_id: target.id,
58 relationship_type: relationship_type
59 }
60 end
61
62 def note_factory(attrs \\ %{}) do
63 text = sequence(:text, &"This is :moominmamma: note #{&1}")
64
65 user = attrs[:user] || insert(:user)
66
67 data = %{
68 "type" => "Note",
69 "content" => text,
70 "source" => text,
71 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
72 "actor" => user.ap_id,
73 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
74 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
75 "likes" => [],
76 "like_count" => 0,
77 "context" => "2hu",
78 "summary" => "2hu",
79 "tag" => ["2hu"],
80 "emoji" => %{
81 "2hu" => "corndog.png"
82 }
83 }
84
85 %Pleroma.Object{
86 data: merge_attributes(data, Map.get(attrs, :data, %{}))
87 }
88 end
89
90 def audio_factory(attrs \\ %{}) do
91 text = sequence(:text, &"lain radio episode #{&1}")
92
93 user = attrs[:user] || insert(:user)
94
95 data = %{
96 "type" => "Audio",
97 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
98 "artist" => "lain",
99 "title" => text,
100 "album" => "lain radio",
101 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
102 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
103 "actor" => user.ap_id,
104 "length" => 180_000
105 }
106
107 %Pleroma.Object{
108 data: merge_attributes(data, Map.get(attrs, :data, %{}))
109 }
110 end
111
112 def listen_factory do
113 audio = insert(:audio)
114
115 data = %{
116 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
117 "type" => "Listen",
118 "actor" => audio.data["actor"],
119 "to" => audio.data["to"],
120 "object" => audio.data,
121 "published" => audio.data["published"]
122 }
123
124 %Pleroma.Activity{
125 data: data,
126 actor: data["actor"],
127 recipients: data["to"]
128 }
129 end
130
131 def direct_note_factory do
132 user2 = insert(:user)
133
134 %Pleroma.Object{data: data} = note_factory()
135 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
136 end
137
138 def article_factory do
139 note_factory()
140 |> Map.put("type", "Article")
141 end
142
143 def tombstone_factory do
144 data = %{
145 "type" => "Tombstone",
146 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
147 "formerType" => "Note",
148 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
149 }
150
151 %Pleroma.Object{
152 data: data
153 }
154 end
155
156 def direct_note_activity_factory do
157 dm = insert(:direct_note)
158
159 data = %{
160 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
161 "type" => "Create",
162 "actor" => dm.data["actor"],
163 "to" => dm.data["to"],
164 "object" => dm.data,
165 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
166 "context" => dm.data["context"]
167 }
168
169 %Pleroma.Activity{
170 data: data,
171 actor: data["actor"],
172 recipients: data["to"]
173 }
174 end
175
176 def note_activity_factory(attrs \\ %{}) do
177 user = attrs[:user] || insert(:user)
178 note = attrs[:note] || insert(:note, user: user)
179
180 data_attrs = attrs[:data_attrs] || %{}
181 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
182
183 data =
184 %{
185 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
186 "type" => "Create",
187 "actor" => note.data["actor"],
188 "to" => note.data["to"],
189 "object" => note.data["id"],
190 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
191 "context" => note.data["context"]
192 }
193 |> Map.merge(data_attrs)
194
195 %Pleroma.Activity{
196 data: data,
197 actor: data["actor"],
198 recipients: data["to"]
199 }
200 |> Map.merge(attrs)
201 end
202
203 defp expiration_offset_by_minutes(attrs, minutes) do
204 scheduled_at =
205 NaiveDateTime.utc_now()
206 |> NaiveDateTime.add(:timer.minutes(minutes), :millisecond)
207 |> NaiveDateTime.truncate(:second)
208
209 %Pleroma.ActivityExpiration{}
210 |> Map.merge(attrs)
211 |> Map.put(:scheduled_at, scheduled_at)
212 end
213
214 def expiration_in_the_past_factory(attrs \\ %{}) do
215 expiration_offset_by_minutes(attrs, -60)
216 end
217
218 def expiration_in_the_future_factory(attrs \\ %{}) do
219 expiration_offset_by_minutes(attrs, 61)
220 end
221
222 def article_activity_factory do
223 article = insert(:article)
224
225 data = %{
226 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
227 "type" => "Create",
228 "actor" => article.data["actor"],
229 "to" => article.data["to"],
230 "object" => article.data,
231 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
232 "context" => article.data["context"]
233 }
234
235 %Pleroma.Activity{
236 data: data,
237 actor: data["actor"],
238 recipients: data["to"]
239 }
240 end
241
242 def announce_activity_factory(attrs \\ %{}) do
243 note_activity = attrs[:note_activity] || insert(:note_activity)
244 user = attrs[:user] || insert(:user)
245
246 data = %{
247 "type" => "Announce",
248 "actor" => note_activity.actor,
249 "object" => note_activity.data["id"],
250 "to" => [user.follower_address, note_activity.data["actor"]],
251 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
252 "context" => note_activity.data["context"]
253 }
254
255 %Pleroma.Activity{
256 data: data,
257 actor: user.ap_id,
258 recipients: data["to"]
259 }
260 end
261
262 def like_activity_factory(attrs \\ %{}) do
263 note_activity = attrs[:note_activity] || insert(:note_activity)
264 object = Object.normalize(note_activity)
265 user = insert(:user)
266
267 data =
268 %{
269 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
270 "actor" => user.ap_id,
271 "type" => "Like",
272 "object" => object.data["id"],
273 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
274 }
275 |> Map.merge(attrs[:data_attrs] || %{})
276
277 %Pleroma.Activity{
278 data: data
279 }
280 end
281
282 def follow_activity_factory do
283 follower = insert(:user)
284 followed = insert(:user)
285
286 data = %{
287 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
288 "actor" => follower.ap_id,
289 "type" => "Follow",
290 "object" => followed.ap_id,
291 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
292 }
293
294 %Pleroma.Activity{
295 data: data,
296 actor: follower.ap_id
297 }
298 end
299
300 def report_activity_factory(attrs \\ %{}) do
301 user = attrs[:user] || insert(:user)
302 activity = attrs[:activity] || insert(:note_activity)
303 state = attrs[:state] || "open"
304
305 data = %{
306 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
307 "actor" => user.ap_id,
308 "type" => "Flag",
309 "object" => [activity.actor, activity.data["id"]],
310 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
311 "to" => [],
312 "cc" => [activity.actor],
313 "context" => activity.data["context"],
314 "state" => state,
315 }
316
317 %Pleroma.Activity{
318 data: data,
319 actor: data["actor"],
320 recipients: data["to"] ++ data["cc"]
321 }
322 end
323
324 def oauth_app_factory do
325 %Pleroma.Web.OAuth.App{
326 client_name: sequence(:client_name, &"Some client #{&1}"),
327 redirect_uris: "https://example.com/callback",
328 scopes: ["read", "write", "follow", "push", "admin"],
329 website: "https://example.com",
330 client_id: Ecto.UUID.generate(),
331 client_secret: "aaa;/&bbb"
332 }
333 end
334
335 def instance_factory do
336 %Pleroma.Instances.Instance{
337 host: "domain.com",
338 unreachable_since: nil
339 }
340 end
341
342 def oauth_token_factory(attrs \\ %{}) do
343 scopes = Map.get(attrs, :scopes, ["read"])
344 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
345 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
346
347 valid_until =
348 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
349
350 %Pleroma.Web.OAuth.Token{
351 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
352 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
353 scopes: scopes,
354 user: user,
355 app: oauth_app,
356 valid_until: valid_until
357 }
358 end
359
360 def oauth_admin_token_factory(attrs \\ %{}) do
361 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
362
363 scopes =
364 attrs
365 |> Map.get(:scopes, ["admin"])
366 |> Kernel.++(["admin"])
367 |> Enum.uniq()
368
369 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
370 oauth_token_factory(attrs)
371 end
372
373 def oauth_authorization_factory do
374 %Pleroma.Web.OAuth.Authorization{
375 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
376 scopes: ["read", "write", "follow", "push"],
377 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
378 user: build(:user),
379 app: build(:oauth_app)
380 }
381 end
382
383 def push_subscription_factory do
384 %Pleroma.Web.Push.Subscription{
385 user: build(:user),
386 token: build(:oauth_token),
387 endpoint: "https://example.com/example/1234",
388 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
389 key_p256dh:
390 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
391 data: %{}
392 }
393 end
394
395 def notification_factory do
396 %Pleroma.Notification{
397 user: build(:user)
398 }
399 end
400
401 def scheduled_activity_factory do
402 %Pleroma.ScheduledActivity{
403 user: build(:user),
404 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
405 params: build(:note) |> Map.from_struct() |> Map.get(:data)
406 }
407 end
408
409 def registration_factory do
410 user = insert(:user)
411
412 %Pleroma.Registration{
413 user: user,
414 provider: "twitter",
415 uid: "171799000",
416 info: %{
417 "name" => "John Doe",
418 "email" => "john@doe.com",
419 "nickname" => "johndoe",
420 "description" => "My bio"
421 }
422 }
423 end
424
425 def config_factory(attrs \\ %{}) do
426 %Pleroma.ConfigDB{
427 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
428 group: :pleroma,
429 value:
430 sequence(
431 :value,
432 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
433 )
434 }
435 |> merge_attributes(attrs)
436 end
437
438 def marker_factory do
439 %Pleroma.Marker{
440 user: build(:user),
441 timeline: "notifications",
442 lock_version: 0,
443 last_read_id: "1"
444 }
445 end
446
447 def mfa_token_factory do
448 %Pleroma.MFA.Token{
449 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
450 authorization: build(:oauth_authorization),
451 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
452 user: build(:user)
453 }
454 end
455
456 def filter_factory do
457 %Pleroma.Filter{
458 user: build(:user),
459 filter_id: sequence(:filter_id, & &1),
460 phrase: "cofe"
461 }
462 end
463 end