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