MastodonAPI: Support poll notification
[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 "attachment" => [],
212 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
213 "cc" => [user.follower_address],
214 "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
215 "oneOf" => [
216 %{
217 "type" => "Note",
218 "name" => "chocolate",
219 "replies" => %{"totalItems" => 0, "type" => "Collection"}
220 },
221 %{
222 "type" => "Note",
223 "name" => "vanilla",
224 "replies" => %{"totalItems" => 0, "type" => "Collection"}
225 }
226 ]
227 }
228
229 %Pleroma.Object{
230 data: merge_attributes(data, Map.get(attrs, :data, %{}))
231 }
232 end
233
234 def direct_note_activity_factory do
235 dm = insert(:direct_note)
236
237 data = %{
238 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
239 "type" => "Create",
240 "actor" => dm.data["actor"],
241 "to" => dm.data["to"],
242 "object" => dm.data,
243 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
244 "context" => dm.data["context"]
245 }
246
247 %Pleroma.Activity{
248 data: data,
249 actor: data["actor"],
250 recipients: data["to"]
251 }
252 end
253
254 def note_activity_factory(attrs \\ %{}) do
255 user = attrs[:user] || insert(:user)
256 note = attrs[:note] || insert(:note, user: user)
257
258 data_attrs = attrs[:data_attrs] || %{}
259 attrs = Map.drop(attrs, [:user, :note, :data_attrs])
260
261 data =
262 %{
263 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
264 "type" => "Create",
265 "actor" => note.data["actor"],
266 "to" => note.data["to"],
267 "object" => note.data["id"],
268 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
269 "context" => note.data["context"]
270 }
271 |> Map.merge(data_attrs)
272
273 %Pleroma.Activity{
274 data: data,
275 actor: data["actor"],
276 recipients: data["to"]
277 }
278 |> Map.merge(attrs)
279 end
280
281 def article_activity_factory do
282 article = insert(:article)
283
284 data = %{
285 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
286 "type" => "Create",
287 "actor" => article.data["actor"],
288 "to" => article.data["to"],
289 "object" => article.data,
290 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
291 "context" => article.data["context"]
292 }
293
294 %Pleroma.Activity{
295 data: data,
296 actor: data["actor"],
297 recipients: data["to"]
298 }
299 end
300
301 def announce_activity_factory(attrs \\ %{}) do
302 note_activity = attrs[:note_activity] || insert(:note_activity)
303 user = attrs[:user] || insert(:user)
304
305 data = %{
306 "type" => "Announce",
307 "actor" => note_activity.actor,
308 "object" => note_activity.data["id"],
309 "to" => [user.follower_address, note_activity.data["actor"]],
310 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
311 "context" => note_activity.data["context"]
312 }
313
314 %Pleroma.Activity{
315 data: data,
316 actor: user.ap_id,
317 recipients: data["to"]
318 }
319 end
320
321 def like_activity_factory(attrs \\ %{}) do
322 note_activity = attrs[:note_activity] || insert(:note_activity)
323 object = Object.normalize(note_activity, fetch: false)
324 user = insert(:user)
325
326 data =
327 %{
328 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
329 "actor" => user.ap_id,
330 "type" => "Like",
331 "object" => object.data["id"],
332 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
333 }
334 |> Map.merge(attrs[:data_attrs] || %{})
335
336 %Pleroma.Activity{
337 data: data
338 }
339 end
340
341 def follow_activity_factory do
342 follower = insert(:user)
343 followed = insert(:user)
344
345 data = %{
346 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
347 "actor" => follower.ap_id,
348 "type" => "Follow",
349 "object" => followed.ap_id,
350 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
351 }
352
353 %Pleroma.Activity{
354 data: data,
355 actor: follower.ap_id
356 }
357 end
358
359 def report_activity_factory(attrs \\ %{}) do
360 user = attrs[:user] || insert(:user)
361 activity = attrs[:activity] || insert(:note_activity)
362 state = attrs[:state] || "open"
363
364 data = %{
365 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
366 "actor" => user.ap_id,
367 "type" => "Flag",
368 "object" => [activity.actor, activity.data["id"]],
369 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
370 "to" => [],
371 "cc" => [activity.actor],
372 "context" => activity.data["context"],
373 "state" => state
374 }
375
376 %Pleroma.Activity{
377 data: data,
378 actor: data["actor"],
379 recipients: data["to"] ++ data["cc"]
380 }
381 end
382
383 def question_activity_factory(attrs \\ %{}) do
384 user = attrs[:user] || insert(:user)
385 question = attrs[:question] || insert(:question, user: user)
386
387 data_attrs = attrs[:data_attrs] || %{}
388 attrs = Map.drop(attrs, [:user, :question, :data_attrs])
389
390 data =
391 %{
392 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
393 "type" => "Create",
394 "actor" => question.data["actor"],
395 "to" => question.data["to"],
396 "object" => question.data["id"],
397 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
398 "context" => question.data["context"]
399 }
400 |> Map.merge(data_attrs)
401
402 %Pleroma.Activity{
403 data: data,
404 actor: data["actor"],
405 recipients: data["to"]
406 }
407 |> Map.merge(attrs)
408 end
409
410 def oauth_app_factory do
411 %Pleroma.Web.OAuth.App{
412 client_name: sequence(:client_name, &"Some client #{&1}"),
413 redirect_uris: "https://example.com/callback",
414 scopes: ["read", "write", "follow", "push", "admin"],
415 website: "https://example.com",
416 client_id: Ecto.UUID.generate(),
417 client_secret: "aaa;/&bbb"
418 }
419 end
420
421 def instance_factory do
422 %Pleroma.Instances.Instance{
423 host: "domain.com",
424 unreachable_since: nil
425 }
426 end
427
428 def oauth_token_factory(attrs \\ %{}) do
429 scopes = Map.get(attrs, :scopes, ["read"])
430 oauth_app = Map.get_lazy(attrs, :app, fn -> insert(:oauth_app, scopes: scopes) end)
431 user = Map.get_lazy(attrs, :user, fn -> build(:user) end)
432
433 valid_until =
434 Map.get(attrs, :valid_until, NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10))
435
436 %Pleroma.Web.OAuth.Token{
437 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
438 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
439 scopes: scopes,
440 user: user,
441 app: oauth_app,
442 valid_until: valid_until
443 }
444 end
445
446 def oauth_admin_token_factory(attrs \\ %{}) do
447 user = Map.get_lazy(attrs, :user, fn -> build(:user, is_admin: true) end)
448
449 scopes =
450 attrs
451 |> Map.get(:scopes, ["admin"])
452 |> Kernel.++(["admin"])
453 |> Enum.uniq()
454
455 attrs = Map.merge(attrs, %{user: user, scopes: scopes})
456 oauth_token_factory(attrs)
457 end
458
459 def oauth_authorization_factory do
460 %Pleroma.Web.OAuth.Authorization{
461 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
462 scopes: ["read", "write", "follow", "push"],
463 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
464 user: build(:user),
465 app: build(:oauth_app)
466 }
467 end
468
469 def push_subscription_factory do
470 %Pleroma.Web.Push.Subscription{
471 user: build(:user),
472 token: build(:oauth_token),
473 endpoint: "https://example.com/example/1234",
474 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
475 key_p256dh:
476 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
477 data: %{}
478 }
479 end
480
481 def notification_factory do
482 %Pleroma.Notification{
483 user: build(:user)
484 }
485 end
486
487 def scheduled_activity_factory do
488 %Pleroma.ScheduledActivity{
489 user: build(:user),
490 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
491 params: build(:note) |> Map.from_struct() |> Map.get(:data)
492 }
493 end
494
495 def registration_factory do
496 user = insert(:user)
497
498 %Pleroma.Registration{
499 user: user,
500 provider: "twitter",
501 uid: "171799000",
502 info: %{
503 "name" => "John Doe",
504 "email" => "john@doe.com",
505 "nickname" => "johndoe",
506 "description" => "My bio"
507 }
508 }
509 end
510
511 def config_factory(attrs \\ %{}) do
512 %Pleroma.ConfigDB{
513 key: sequence(:key, &String.to_atom("some_key_#{&1}")),
514 group: :pleroma,
515 value:
516 sequence(
517 :value,
518 &%{another_key: "#{&1}somevalue", another: "#{&1}somevalue"}
519 )
520 }
521 |> merge_attributes(attrs)
522 end
523
524 def marker_factory do
525 %Pleroma.Marker{
526 user: build(:user),
527 timeline: "notifications",
528 lock_version: 0,
529 last_read_id: "1"
530 }
531 end
532
533 def mfa_token_factory do
534 %Pleroma.MFA.Token{
535 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
536 authorization: build(:oauth_authorization),
537 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
538 user: build(:user)
539 }
540 end
541
542 def filter_factory do
543 %Pleroma.Filter{
544 user: build(:user),
545 filter_id: sequence(:filter_id, & &1),
546 phrase: "cofe",
547 context: ["home"]
548 }
549 end
550 end