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