websub: remove entirely
[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 oauth_app_factory do
285 %Pleroma.Web.OAuth.App{
286 client_name: "Some client",
287 redirect_uris: "https://example.com/callback",
288 scopes: ["read", "write", "follow", "push"],
289 website: "https://example.com",
290 client_id: Ecto.UUID.generate(),
291 client_secret: "aaa;/&bbb"
292 }
293 end
294
295 def instance_factory do
296 %Pleroma.Instances.Instance{
297 host: "domain.com",
298 unreachable_since: nil
299 }
300 end
301
302 def oauth_token_factory do
303 oauth_app = insert(:oauth_app)
304
305 %Pleroma.Web.OAuth.Token{
306 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
307 scopes: ["read"],
308 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
309 user: build(:user),
310 app_id: oauth_app.id,
311 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
312 }
313 end
314
315 def oauth_authorization_factory do
316 %Pleroma.Web.OAuth.Authorization{
317 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
318 scopes: ["read", "write", "follow", "push"],
319 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
320 user: build(:user),
321 app: build(:oauth_app)
322 }
323 end
324
325 def push_subscription_factory do
326 %Pleroma.Web.Push.Subscription{
327 user: build(:user),
328 token: build(:oauth_token),
329 endpoint: "https://example.com/example/1234",
330 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
331 key_p256dh:
332 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
333 data: %{}
334 }
335 end
336
337 def notification_factory do
338 %Pleroma.Notification{
339 user: build(:user)
340 }
341 end
342
343 def scheduled_activity_factory do
344 %Pleroma.ScheduledActivity{
345 user: build(:user),
346 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
347 params: build(:note) |> Map.from_struct() |> Map.get(:data)
348 }
349 end
350
351 def registration_factory do
352 user = insert(:user)
353
354 %Pleroma.Registration{
355 user: user,
356 provider: "twitter",
357 uid: "171799000",
358 info: %{
359 "name" => "John Doe",
360 "email" => "john@doe.com",
361 "nickname" => "johndoe",
362 "description" => "My bio"
363 }
364 }
365 end
366
367 def config_factory do
368 %Pleroma.Web.AdminAPI.Config{
369 key: sequence(:key, &"some_key_#{&1}"),
370 group: "pleroma",
371 value:
372 sequence(
373 :value,
374 fn key ->
375 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
376 end
377 )
378 }
379 end
380 end