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