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