Merge remote-tracking branch 'pleroma/develop' into poll-notification-fixes
[akkoma] / test / support / factory.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
8 require Pleroma.Constants
9
10 alias Pleroma.Object
11 alias Pleroma.User
12
13 def participation_factory do
14 conversation = insert(:conversation)
15 user = insert(:user)
16
17 %Pleroma.Conversation.Participation{
18 conversation: conversation,
19 user: user,
20 read: false
21 }
22 end
23
24 def conversation_factory do
25 %Pleroma.Conversation{
26 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
27 }
28 end
29
30 def user_factory(attrs \\ %{}) do
31 user = %User{
32 name: sequence(:name, &"Test テスト User #{&1}"),
33 email: sequence(:email, &"user#{&1}@example.com"),
34 nickname: sequence(:nickname, &"nick#{&1}"),
35 password_hash: Pleroma.Password.Pbkdf2.hash_pwd_salt("test"),
36 bio: sequence(:bio, &"Tester Number #{&1}"),
37 is_discoverable: true,
38 last_digest_emailed_at: NaiveDateTime.utc_now(),
39 last_refreshed_at: NaiveDateTime.utc_now(),
40 notification_settings: %Pleroma.User.NotificationSetting{},
41 multi_factor_authentication_settings: %Pleroma.MFA.Settings{},
42 ap_enabled: true
43 }
44
45 urls =
46 if attrs[:local] == false do
47 base_domain = attrs[:domain] || Enum.random(["domain1.com", "domain2.com", "domain3.com"])
48
49 ap_id = "https://#{base_domain}/users/#{user.nickname}"
50
51 %{
52 ap_id: ap_id,
53 follower_address: ap_id <> "/followers",
54 following_address: ap_id <> "/following",
55 featured_address: ap_id <> "/collections/featured"
56 }
57 else
58 %{
59 ap_id: User.ap_id(user),
60 follower_address: User.ap_followers(user),
61 following_address: User.ap_following(user),
62 featured_address: User.ap_featured_collection(user)
63 }
64 end
65
66 attrs = Map.delete(attrs, :domain)
67
68 user
69 |> Map.put(:raw_bio, user.bio)
70 |> Map.merge(urls)
71 |> merge_attributes(attrs)
72 end
73
74 def user_relationship_factory(attrs \\ %{}) do
75 source = attrs[:source] || insert(:user)
76 target = attrs[:target] || insert(:user)
77 relationship_type = attrs[:relationship_type] || :block
78
79 %Pleroma.UserRelationship{
80 source_id: source.id,
81 target_id: target.id,
82 relationship_type: relationship_type
83 }
84 end
85
86 def note_factory(attrs \\ %{}) do
87 text = sequence(:text, &"This is :moominmamma: note #{&1}")
88
89 user = attrs[:user] || insert(:user)
90
91 data = %{
92 "type" => "Note",
93 "content" => text,
94 "source" => text,
95 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
96 "actor" => user.ap_id,
97 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
98 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
99 "likes" => [],
100 "like_count" => 0,
101 "context" => "2hu",
102 "summary" => "2hu",
103 "tag" => ["2hu"],
104 "emoji" => %{
105 "2hu" => "corndog.png"
106 }
107 }
108
109 %Pleroma.Object{
110 data: merge_attributes(data, Map.get(attrs, :data, %{}))
111 }
112 end
113
114 def attachment_note_factory(attrs \\ %{}) do
115 user = attrs[:user] || insert(:user)
116 {length, attrs} = Map.pop(attrs, :length, 1)
117
118 data = %{
119 "attachment" =>
120 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
121 |> Enum.take(length)
122 }
123
124 build(:note, Map.put(attrs, :data, data))
125 end
126
127 defp attachment_data(ap_id, href) do
128 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
129
130 %{
131 "url" => [
132 %{
133 "href" => href,
134 "type" => "Link",
135 "mediaType" => "image/jpeg"
136 }
137 ],
138 "name" => "some name",
139 "type" => "Document",
140 "actor" => ap_id,
141 "mediaType" => "image/jpeg"
142 }
143 end
144
145 def audio_factory(attrs \\ %{}) do
146 text = sequence(:text, &"lain radio episode #{&1}")
147
148 user = attrs[:user] || insert(:user)
149
150 data = %{
151 "type" => "Audio",
152 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
153 "artist" => "lain",
154 "title" => text,
155 "album" => "lain radio",
156 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
157 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
158 "actor" => user.ap_id,
159 "length" => 180_000
160 }
161
162 %Pleroma.Object{
163 data: merge_attributes(data, Map.get(attrs, :data, %{}))
164 }
165 end
166
167 def listen_factory do
168 audio = insert(:audio)
169
170 data = %{
171 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
172 "type" => "Listen",
173 "actor" => audio.data["actor"],
174 "to" => audio.data["to"],
175 "object" => audio.data,
176 "published" => audio.data["published"]
177 }
178
179 %Pleroma.Activity{
180 data: data,
181 actor: data["actor"],
182 recipients: data["to"]
183 }
184 end
185
186 def direct_note_factory do
187 user2 = insert(:user)
188
189 %Pleroma.Object{data: data} = note_factory()
190 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
191 end
192
193 def article_factory do
194 %Pleroma.Object{data: data} = note_factory()
195 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
196 end
197
198 def tombstone_factory do
199 data = %{
200 "type" => "Tombstone",
201 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
202 "formerType" => "Note",
203 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
204 }
205
206 %Pleroma.Object{
207 data: data
208 }
209 end
210
211 def question_factory(attrs \\ %{}) do
212 user = attrs[:user] || insert(:user)
213
214 data = %{
215 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
216 "type" => "Question",
217 "actor" => user.ap_id,
218 "attributedTo" => user.ap_id,
219 "attachment" => [],
220 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
221 "cc" => [user.follower_address],
222 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
223 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
224 "oneOf" => [
225 %{
226 "type" => "Note",
227 "name" => "chocolate",
228 "replies" => %{"totalItems" => 0, "type" => "Collection"}
229 },
230 %{
231 "type" => "Note",
232 "name" => "vanilla",
233 "replies" => %{"totalItems" => 0, "type" => "Collection"}
234 }
235 ]
236 }
237
238 %Pleroma.Object{
239 data: merge_attributes(data, Map.get(attrs, :data, %{}))
240 }
241 end
242
243 def direct_note_activity_factory do
244 dm = insert(:direct_note)
245
246 data = %{
247 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
248 "type" => "Create",
249 "actor" => dm.data["actor"],
250 "to" => dm.data["to"],
251 "object" => dm.data,
252 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
253 "context" => dm.data["context"]
254 }
255
256 %Pleroma.Activity{
257 data: data,
258 actor: data["actor"],
259 recipients: data["to"]
260 }
261 end
262
263 def add_activity_factory(attrs \\ %{}) do
264 featured_collection_activity(attrs, "Add")
265 end
266
267 def remove_activity_factor(attrs \\ %{}) do
268 featured_collection_activity(attrs, "Remove")
269 end
270
271 defp featured_collection_activity(attrs, type) do
272 user = attrs[:user] || insert(:user)
273 note = attrs[:note] || insert(:note, user: user)
274
275 data_attrs =
276 attrs
277 |> Map.get(:data_attrs, %{})
278 |> Map.put(:type, type)
279
280 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
281
282 data =
283 %{
284 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
285 "target" => user.featured_address,
286 "object" => note.data["object"],
287 "actor" => note.data["actor"],
288 "type" => "Add",
289 "to" => [Pleroma.Constants.as_public()],
290 "cc" => [user.follower_address]
291 }
292 |> Map.merge(data_attrs)
293
294 %Pleroma.Activity{
295 data: data,
296 actor: data["actor"],
297 recipients: data["to"]
298 }
299 |> Map.merge(attrs)
300 end
301
302 def note_activity_factory(attrs \\ %{}) do
303 user = attrs[:user] || insert(:user)
304 note = attrs[:note] || insert(:note, user: user)
305
306 data_attrs = attrs[:data_attrs] || %{}
307 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
308
309 data =
310 %{
311 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
312 "type" => "Create",
313 "actor" => note.data["actor"],
314 "to" => note.data["to"],
315 "object" => note.data["id"],
316 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
317 "context" => note.data["context"]
318 }
319 |> Map.merge(data_attrs)
320
321 %Pleroma.Activity{
322 data: data,
323 actor: data["actor"],
324 recipients: data["to"]
325 }
326 |> Map.merge(attrs)
327 end
328
329 def article_activity_factory do
330 article = insert(:article)
331
332 data = %{
333 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
334 "type" => "Create",
335 "actor" => article.data["actor"],
336 "to" => article.data["to"],
337 "object" => article.data,
338 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
339 "context" => article.data["context"]
340 }
341
342 %Pleroma.Activity{
343 data: data,
344 actor: data["actor"],
345 recipients: data["to"]
346 }
347 end
348
349 def announce_activity_factory(attrs \\ %{}) do
350 note_activity = attrs[:note_activity] || insert(:note_activity)
351 user = attrs[:user] || insert(:user)
352
353 data = %{
354 "type" => "Announce",
355 "actor" => note_activity.actor,
356 "object" => note_activity.data["id"],
357 "to" => [user.follower_address, note_activity.data["actor"]],
358 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
359 "context" => note_activity.data["context"]
360 }
361
362 %Pleroma.Activity{
363 data: data,
364 actor: user.ap_id,
365 recipients: data["to"]
366 }
367 end
368
369 def like_activity_factory(attrs \\ %{}) do
370 note_activity = attrs[:note_activity] || insert(:note_activity)
371 object = Object.normalize(note_activity, fetch: false)
372 user = insert(:user)
373
374 data =
375 %{
376 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
377 "actor" => user.ap_id,
378 "type" => "Like",
379 "object" => object.data["id"],
380 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
381 }
382 |> Map.merge(attrs[:data_attrs] || %{})
383
384 %Pleroma.Activity{
385 data: data
386 }
387 end
388
389 def follow_activity_factory do
390 follower = insert(:user)
391 followed = insert(:user)
392
393 data = %{
394 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
395 "actor" => follower.ap_id,
396 "type" => "Follow",
397 "object" => followed.ap_id,
398 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
399 }
400
401 %Pleroma.Activity{
402 data: data,
403 actor: follower.ap_id
404 }
405 end
406
407 def report_activity_factory(attrs \\ %{}) do
408 user = attrs[:user] || insert(:user)
409 activity = attrs[:activity] || insert(:note_activity)
410 state = attrs[:state] || "open"
411
412 data = %{
413 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
414 "actor" => user.ap_id,
415 "type" => "Flag",
416 "object" => [activity.actor, activity.data["id"]],
417 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
418 "to" => [],
419 "cc" => [activity.actor],
420 "context" => activity.data["context"],
421 "state" => state
422 }
423
424 %Pleroma.Activity{
425 data: data,
426 actor: data["actor"],
427 recipients: data["to"] ++ data["cc"]
428 }
429 end
430
431 def question_activity_factory(attrs \\ %{}) do
432 user = attrs[:user] || insert(:user)
433 question = attrs[:question] || insert(:question, user: user)
434
435 data_attrs = attrs[:data_attrs] || %{}
436 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
437
438 data =
439 %{
440 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
441 "type" => "Create",
442 "actor" => question.data["actor"],
443 "to" => question.data["to"],
444 "object" => question.data["id"],
445 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
446 "context" => question.data["context"]
447 }
448 |> Map.merge(data_attrs)
449
450 %Pleroma.Activity{
451 data: data,
452 actor: data["actor"],
453 recipients: data["to"]
454 }
455 |> Map.merge(attrs)
456 end
457
458 def oauth_app_factory do
459 %Pleroma.Web.OAuth.App{
460 client_name: sequence(:client_name, &"Some client #{&1}"),
461 redirect_uris: "https://example.com/callback",
462 scopes: ["read", "write", "follow", "push", "admin"],
463 website: "https://example.com",
464 client_id: Ecto.UUID.generate(),
465 client_secret: "aaa;/&bbb"
466 }
467 end
468
469 def instance_factory do
470 %Pleroma.Instances.Instance{
471 host: "domain.com",
472 unreachable_since: nil
473 }
474 end
475
476 def oauth_token_factory(attrs \\ %{}) do
477 scopes = Map.get(attrs, :scopes, ["read"])
478 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
479 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
480
481 valid_until =
482 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
483
484 %Pleroma.Web.OAuth.Token{
485 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
486 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
487 scopes: scopes,
488 user: user,
489 app: oauth_app,
490 valid_until: valid_until
491 }
492 end
493
494 def oauth_admin_token_factory(attrs \\ %{}) do
495 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
496
497 scopes =
498 attrs
499 |> Map.get(:scopes, ["admin"])
500 |> Kernel.++(["admin"])
501 |> Enum.uniq()
502
503 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
504 oauth_token_factory(attrs)
505 end
506
507 def oauth_authorization_factory do
508 %Pleroma.Web.OAuth.Authorization{
509 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
510 scopes: ["read", "write", "follow", "push"],
511 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
512 user: build(:user),
513 app: build(:oauth_app)
514 }
515 end
516
517 def push_subscription_factory do
518 %Pleroma.Web.Push.Subscription{
519 user: build(:user),
520 token: build(:oauth_token),
521 endpoint: "https://example.com/example/1234",
522 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
523 key_p256dh:
524 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
525 data: %{}
526 }
527 end
528
529 def notification_factory do
530 %Pleroma.Notification{
531 user: build(:user)
532 }
533 end
534
535 def scheduled_activity_factory do
536 %Pleroma.ScheduledActivity{
537 user: build(:user),
538 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
539 params: build(:note) |> Map.from_struct() |> Map.get(:data)
540 }
541 end
542
543 def registration_factory do
544 user = insert(:user)
545
546 %Pleroma.Registration{
547 user: user,
548 provider: "twitter",
549 uid: "171799000",
550 info: %{
551 "name" => "John Doe",
552 "email" => "john@doe.com",
553 "nickname" => "johndoe",
554 "description" => "My bio"
555 }
556 }
557 end
558
559 def config_factory(attrs \\ %{}) do
560 %Pleroma.ConfigDB{
561 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
562 group: :pleroma,
563 value:
564 sequence(
565 :value,
566 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
567 )
568 }
569 |> merge_attributes(attrs)
570 end
571
572 def marker_factory do
573 %Pleroma.Marker{
574 user: build(:user),
575 timeline: "notifications",
576 lock_version: 0,
577 last_read_id: "1"
578 }
579 end
580
581 def mfa_token_factory do
582 %Pleroma.MFA.Token{
583 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
584 authorization: build(:oauth_authorization),
585 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
586 user: build(:user)
587 }
588 end
589
590 def filter_factory do
591 %Pleroma.Filter{
592 user: build(:user),
593 filter_id: sequence(:filter_id, & &1),
594 phrase: "cofe",
595 context: ["home"]
596 }
597 end
598 end