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