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