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