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