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