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