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