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