Post editing (#202)
[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_factory(attrs \\ %{}) do
115 user = attrs[:user] || insert(:user)
116
117 data =
118 attachment_data(user.ap_id, nil)
119 |> Map.put("id", Pleroma.Web.ActivityPub.Utils.generate_object_id())
120
121 %Pleroma.Object{
122 data: merge_attributes(data, Map.get(attrs, :data, %{}))
123 }
124 end
125
126 def attachment_note_factory(attrs \\ %{}) do
127 user = attrs[:user] || insert(:user)
128 {length, attrs} = Map.pop(attrs, :length, 1)
129
130 data = %{
131 "attachment" =>
132 Stream.repeatedly(fn -> attachment_data(user.ap_id, attrs[:href]) end)
133 |> Enum.take(length)
134 }
135
136 build(:note, Map.put(attrs, :data, data))
137 end
138
139 defp attachment_data(ap_id, href) do
140 href = href || sequence(:href, &"#{Pleroma.Web.Endpoint.url()}/media/#{&1}.jpg")
141
142 %{
143 "url" => [
144 %{
145 "href" => href,
146 "type" => "Link",
147 "mediaType" => "image/jpeg"
148 }
149 ],
150 "name" => "some name",
151 "type" => "Document",
152 "actor" => ap_id,
153 "mediaType" => "image/jpeg"
154 }
155 end
156
157 def followers_only_note_factory(attrs \\ %{}) do
158 %Pleroma.Object{data: data} = note_factory(attrs)
159 %Pleroma.Object{data: Map.merge(data, %{"to" => [data["actor"] <> "/followers"]})}
160 end
161
162 def audio_factory(attrs \\ %{}) do
163 text = sequence(:text, &"lain radio episode #{&1}")
164
165 user = attrs[:user] || insert(:user)
166
167 data = %{
168 "type" => "Audio",
169 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
170 "artist" => "lain",
171 "title" => text,
172 "album" => "lain radio",
173 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
174 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
175 "actor" => user.ap_id,
176 "length" => 180_000
177 }
178
179 %Pleroma.Object{
180 data: merge_attributes(data, Map.get(attrs, :data, %{}))
181 }
182 end
183
184 def listen_factory do
185 audio = insert(:audio)
186
187 data = %{
188 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
189 "type" => "Listen",
190 "actor" => audio.data["actor"],
191 "to" => audio.data["to"],
192 "object" => audio.data,
193 "published" => audio.data["published"]
194 }
195
196 %Pleroma.Activity{
197 data: data,
198 actor: data["actor"],
199 recipients: data["to"]
200 }
201 end
202
203 def direct_note_factory do
204 user2 = insert(:user)
205
206 %Pleroma.Object{data: data} = note_factory()
207 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
208 end
209
210 def article_factory do
211 %Pleroma.Object{data: data} = note_factory()
212 %Pleroma.Object{data: Map.merge(data, %{"type" => "Article"})}
213 end
214
215 def tombstone_factory do
216 data = %{
217 "type" => "Tombstone",
218 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
219 "formerType" => "Note",
220 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
221 }
222
223 %Pleroma.Object{
224 data: data
225 }
226 end
227
228 def question_factory(attrs \\ %{}) do
229 user = attrs[:user] || insert(:user)
230
231 data = %{
232 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
233 "type" => "Question",
234 "actor" => user.ap_id,
235 "attributedTo" => user.ap_id,
236 "attachment" => [],
237 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
238 "cc" => [user.follower_address],
239 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
240 "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
241 "oneOf" => [
242 %{
243 "type" => "Note",
244 "name" => "chocolate",
245 "replies" => %{"totalItems" => 0, "type" => "Collection"}
246 },
247 %{
248 "type" => "Note",
249 "name" => "vanilla",
250 "replies" => %{"totalItems" => 0, "type" => "Collection"}
251 }
252 ]
253 }
254
255 %Pleroma.Object{
256 data: merge_attributes(data, Map.get(attrs, :data, %{}))
257 }
258 end
259
260 def direct_note_activity_factory do
261 dm = insert(:direct_note)
262
263 data = %{
264 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
265 "type" => "Create",
266 "actor" => dm.data["actor"],
267 "to" => dm.data["to"],
268 "object" => dm.data,
269 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
270 "context" => dm.data["context"]
271 }
272
273 %Pleroma.Activity{
274 data: data,
275 actor: data["actor"],
276 recipients: data["to"]
277 }
278 end
279
280 def add_activity_factory(attrs \\ %{}) do
281 featured_collection_activity(attrs, "Add")
282 end
283
284 def remove_activity_factor(attrs \\ %{}) do
285 featured_collection_activity(attrs, "Remove")
286 end
287
288 defp featured_collection_activity(attrs, type) do
289 user = attrs[:user] || insert(:user)
290 note = attrs[:note] || insert(:note, user: user)
291
292 data_attrs =
293 attrs
294 |> Map.get(:data_attrs, %{})
295 |> Map.put(:type, type)
296
297 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
298
299 data =
300 %{
301 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
302 "target" => user.featured_address,
303 "object" => note.data["object"],
304 "actor" => note.data["actor"],
305 "type" => "Add",
306 "to" => [Pleroma.Constants.as_public()],
307 "cc" => [user.follower_address]
308 }
309 |> Map.merge(data_attrs)
310
311 %Pleroma.Activity{
312 data: data,
313 actor: data["actor"],
314 recipients: data["to"]
315 }
316 |> Map.merge(attrs)
317 end
318
319 def followers_only_note_activity_factory(attrs \\ %{}) do
320 user = attrs[:user] || insert(:user)
321 note = insert(:followers_only_note, user: user)
322
323 data_attrs = attrs[:data_attrs] || %{}
324 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
325
326 data =
327 %{
328 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
329 "type" => "Create",
330 "actor" => note.data["actor"],
331 "to" => note.data["to"],
332 "object" => note.data,
333 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
334 "context" => note.data["context"]
335 }
336 |> Map.merge(data_attrs)
337
338 %Pleroma.Activity{
339 data: data,
340 actor: data["actor"],
341 recipients: data["to"]
342 }
343 |> Map.merge(attrs)
344 end
345
346 def note_activity_factory(attrs \\ %{}) do
347 user = attrs[:user] || insert(:user)
348 note = attrs[:note] || insert(:note, user: user)
349
350 data_attrs = attrs[:data_attrs] || %{}
351 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
352
353 data =
354 %{
355 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
356 "type" => "Create",
357 "actor" => note.data["actor"],
358 "to" => note.data["to"],
359 "object" => note.data["id"],
360 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
361 "context" => note.data["context"]
362 }
363 |> Map.merge(data_attrs)
364
365 %Pleroma.Activity{
366 data: data,
367 actor: data["actor"],
368 recipients: data["to"]
369 }
370 |> Map.merge(attrs)
371 end
372
373 def article_activity_factory do
374 article = insert(:article)
375
376 data = %{
377 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
378 "type" => "Create",
379 "actor" => article.data["actor"],
380 "to" => article.data["to"],
381 "object" => article.data,
382 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
383 "context" => article.data["context"]
384 }
385
386 %Pleroma.Activity{
387 data: data,
388 actor: data["actor"],
389 recipients: data["to"]
390 }
391 end
392
393 def announce_activity_factory(attrs \\ %{}) do
394 note_activity = attrs[:note_activity] || insert(:note_activity)
395 user = attrs[:user] || insert(:user)
396
397 data = %{
398 "type" => "Announce",
399 "actor" => note_activity.actor,
400 "object" => note_activity.data["id"],
401 "to" => [user.follower_address, note_activity.data["actor"]],
402 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
403 "context" => note_activity.data["context"]
404 }
405
406 %Pleroma.Activity{
407 data: data,
408 actor: user.ap_id,
409 recipients: data["to"]
410 }
411 end
412
413 def like_activity_factory(attrs \\ %{}) do
414 note_activity = attrs[:note_activity] || insert(:note_activity)
415 object = Object.normalize(note_activity, fetch: false)
416 user = insert(:user)
417
418 data =
419 %{
420 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
421 "actor" => user.ap_id,
422 "type" => "Like",
423 "object" => object.data["id"],
424 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
425 }
426 |> Map.merge(attrs[:data_attrs] || %{})
427
428 %Pleroma.Activity{
429 data: data
430 }
431 end
432
433 def follow_activity_factory do
434 follower = insert(:user)
435 followed = insert(:user)
436
437 data = %{
438 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
439 "actor" => follower.ap_id,
440 "type" => "Follow",
441 "object" => followed.ap_id,
442 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
443 }
444
445 %Pleroma.Activity{
446 data: data,
447 actor: follower.ap_id
448 }
449 end
450
451 def report_activity_factory(attrs \\ %{}) do
452 user = attrs[:user] || insert(:user)
453 activity = attrs[:activity] || insert(:note_activity)
454 state = attrs[:state] || "open"
455
456 data = %{
457 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
458 "actor" => user.ap_id,
459 "type" => "Flag",
460 "object" => [activity.actor, activity.data["id"]],
461 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
462 "to" => [],
463 "cc" => [activity.actor],
464 "context" => activity.data["context"],
465 "state" => state
466 }
467
468 %Pleroma.Activity{
469 data: data,
470 actor: data["actor"],
471 recipients: data["to"] ++ data["cc"]
472 }
473 end
474
475 def question_activity_factory(attrs \\ %{}) do
476 user = attrs[:user] || insert(:user)
477 question = attrs[:question] || insert(:question, user: user)
478
479 data_attrs = attrs[:data_attrs] || %{}
480 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
481
482 data =
483 %{
484 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
485 "type" => "Create",
486 "actor" => question.data["actor"],
487 "to" => question.data["to"],
488 "object" => question.data["id"],
489 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
490 "context" => question.data["context"]
491 }
492 |> Map.merge(data_attrs)
493
494 %Pleroma.Activity{
495 data: data,
496 actor: data["actor"],
497 recipients: data["to"]
498 }
499 |> Map.merge(attrs)
500 end
501
502 def oauth_app_factory do
503 %Pleroma.Web.OAuth.App{
504 client_name: sequence(:client_name, &"Some client #{&1}"),
505 redirect_uris: "https://example.com/callback",
506 scopes: ["read", "write", "follow", "push", "admin"],
507 website: "https://example.com",
508 client_id: Ecto.UUID.generate(),
509 client_secret: "aaa;/&bbb"
510 }
511 end
512
513 def instance_factory do
514 %Pleroma.Instances.Instance{
515 host: "domain.com",
516 unreachable_since: nil
517 }
518 end
519
520 def oauth_token_factory(attrs \\ %{}) do
521 scopes = Map.get(attrs, :scopes, ["read"])
522 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
523 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
524
525 valid_until =
526 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
527
528 %Pleroma.Web.OAuth.Token{
529 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
530 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
531 scopes: scopes,
532 user: user,
533 app: oauth_app,
534 valid_until: valid_until
535 }
536 end
537
538 def oauth_admin_token_factory(attrs \\ %{}) do
539 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
540
541 scopes =
542 attrs
543 |> Map.get(:scopes, ["admin"])
544 |> Kernel.++(["admin"])
545 |> Enum.uniq()
546
547 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
548 oauth_token_factory(attrs)
549 end
550
551 def oauth_authorization_factory do
552 %Pleroma.Web.OAuth.Authorization{
553 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
554 scopes: ["read", "write", "follow", "push"],
555 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
556 user: build(:user),
557 app: build(:oauth_app)
558 }
559 end
560
561 def push_subscription_factory do
562 %Pleroma.Web.Push.Subscription{
563 user: build(:user),
564 token: build(:oauth_token),
565 endpoint: "https://example.com/example/1234",
566 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
567 key_p256dh:
568 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
569 data: %{}
570 }
571 end
572
573 def notification_factory do
574 %Pleroma.Notification{
575 user: build(:user)
576 }
577 end
578
579 def scheduled_activity_factory do
580 %Pleroma.ScheduledActivity{
581 user: build(:user),
582 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
583 params: build(:note) |> Map.from_struct() |> Map.get(:data)
584 }
585 end
586
587 def registration_factory do
588 user = insert(:user)
589
590 %Pleroma.Registration{
591 user: user,
592 provider: "twitter",
593 uid: "171799000",
594 info: %{
595 "name" => "John Doe",
596 "email" => "john@doe.com",
597 "nickname" => "johndoe",
598 "description" => "My bio"
599 }
600 }
601 end
602
603 def config_factory(attrs \\ %{}) do
604 %Pleroma.ConfigDB{
605 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
606 group: :pleroma,
607 value:
608 sequence(
609 :value,
610 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
611 )
612 }
613 |> merge_attributes(attrs)
614 end
615
616 def marker_factory do
617 %Pleroma.Marker{
618 user: build(:user),
619 timeline: "notifications",
620 lock_version: 0,
621 last_read_id: "1"
622 }
623 end
624
625 def mfa_token_factory do
626 %Pleroma.MFA.Token{
627 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
628 authorization: build(:oauth_authorization),
629 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
630 user: build(:user)
631 }
632 end
633
634 def filter_factory do
635 %Pleroma.Filter{
636 user: build(:user),
637 filter_id: sequence(:filter_id, & &1),
638 phrase: "cofe",
639 context: ["home"]
640 }
641 end
642
643 def announcement_factory(params \\ %{}) do
644 data = Map.get(params, :data, %{})
645
646 {_, params} = Map.pop(params, :data)
647
648 %Pleroma.Announcement{
649 data: Map.merge(%{"content" => "test announcement", "all_day" => false}, data)
650 }
651 |> Map.merge(params)
652 |> Pleroma.Announcement.add_rendered_properties()
653 end
654 end