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