Merge branch 'activity-expiration' 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 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 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
212 "actor" => user.ap_id,
213 "type" => "Like",
214 "object" => object.data["id"],
215 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
216 }
217
218 %Pleroma.Activity{
219 data: data
220 }
221 end
222
223 def follow_activity_factory do
224 follower = insert(:user)
225 followed = insert(:user)
226
227 data = %{
228 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
229 "actor" => follower.ap_id,
230 "type" => "Follow",
231 "object" => followed.ap_id,
232 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
233 }
234
235 %Pleroma.Activity{
236 data: data,
237 actor: follower.ap_id
238 }
239 end
240
241 def websub_subscription_factory do
242 %Pleroma.Web.Websub.WebsubServerSubscription{
243 topic: "http://example.org",
244 callback: "http://example.org/callback",
245 secret: "here's a secret",
246 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
247 state: "requested"
248 }
249 end
250
251 def websub_client_subscription_factory do
252 %Pleroma.Web.Websub.WebsubClientSubscription{
253 topic: "http://example.org",
254 secret: "here's a secret",
255 valid_until: nil,
256 state: "requested",
257 subscribers: []
258 }
259 end
260
261 def oauth_app_factory do
262 %Pleroma.Web.OAuth.App{
263 client_name: "Some client",
264 redirect_uris: "https://example.com/callback",
265 scopes: ["read", "write", "follow", "push"],
266 website: "https://example.com",
267 client_id: Ecto.UUID.generate(),
268 client_secret: "aaa;/&bbb"
269 }
270 end
271
272 def instance_factory do
273 %Pleroma.Instances.Instance{
274 host: "domain.com",
275 unreachable_since: nil
276 }
277 end
278
279 def oauth_token_factory do
280 oauth_app = insert(:oauth_app)
281
282 %Pleroma.Web.OAuth.Token{
283 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
284 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
285 user: build(:user),
286 app_id: oauth_app.id,
287 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
288 }
289 end
290
291 def oauth_authorization_factory do
292 %Pleroma.Web.OAuth.Authorization{
293 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
294 scopes: ["read", "write", "follow", "push"],
295 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
296 user: build(:user),
297 app: build(:oauth_app)
298 }
299 end
300
301 def push_subscription_factory do
302 %Pleroma.Web.Push.Subscription{
303 user: build(:user),
304 token: build(:oauth_token),
305 endpoint: "https://example.com/example/1234",
306 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
307 key_p256dh:
308 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
309 data: %{}
310 }
311 end
312
313 def notification_factory do
314 %Pleroma.Notification{
315 user: build(:user)
316 }
317 end
318
319 def scheduled_activity_factory do
320 %Pleroma.ScheduledActivity{
321 user: build(:user),
322 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
323 params: build(:note) |> Map.from_struct() |> Map.get(:data)
324 }
325 end
326
327 def registration_factory do
328 user = insert(:user)
329
330 %Pleroma.Registration{
331 user: user,
332 provider: "twitter",
333 uid: "171799000",
334 info: %{
335 "name" => "John Doe",
336 "email" => "john@doe.com",
337 "nickname" => "johndoe",
338 "description" => "My bio"
339 }
340 }
341 end
342
343 def config_factory do
344 %Pleroma.Web.AdminAPI.Config{
345 key: sequence(:key, &"some_key_#{&1}"),
346 group: "pleroma",
347 value:
348 sequence(
349 :value,
350 fn key ->
351 :erlang.term_to_binary(%{another_key: "#{key}somevalue", another: "#{key}somevalue"})
352 end
353 )
354 }
355 end
356 end