Add Reports to Admin API
[akkoma] / test / support / factory.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 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
8 def participation_factory do
9 conversation = insert(:conversation)
10 user = insert(:user)
11
12 %Pleroma.Conversation.Participation{
13 conversation: conversation,
14 user: user,
15 read: false
16 }
17 end
18
19 def conversation_factory do
20 %Pleroma.Conversation{
21 ap_id: sequence(:ap_id, &"https://some_conversation/#{&1}")
22 }
23 end
24
25 def user_factory do
26 user = %Pleroma.User{
27 name: sequence(:name, &"Test テスト User #{&1}"),
28 email: sequence(:email, &"user#{&1}@example.com"),
29 nickname: sequence(:nickname, &"nick#{&1}"),
30 password_hash: Comeonin.Pbkdf2.hashpwsalt("test"),
31 bio: sequence(:bio, &"Tester Number #{&1}"),
32 info: %{}
33 }
34
35 %{
36 user
37 | ap_id: Pleroma.User.ap_id(user),
38 follower_address: Pleroma.User.ap_followers(user),
39 following: [Pleroma.User.ap_id(user)]
40 }
41 end
42
43 def note_factory(attrs \\ %{}) do
44 text = sequence(:text, &"This is :moominmamma: note #{&1}")
45
46 user = attrs[:user] || insert(:user)
47
48 data = %{
49 "type" => "Note",
50 "content" => text,
51 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
52 "actor" => user.ap_id,
53 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
54 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
55 "likes" => [],
56 "like_count" => 0,
57 "context" => "2hu",
58 "summary" => "2hu",
59 "tag" => ["2hu"],
60 "emoji" => %{
61 "2hu" => "corndog.png"
62 }
63 }
64
65 %Pleroma.Object{
66 data: merge_attributes(data, Map.get(attrs, :data, %{}))
67 }
68 end
69
70 def direct_note_factory do
71 user2 = insert(:user)
72
73 %Pleroma.Object{data: data} = note_factory()
74 %Pleroma.Object{data: Map.merge(data, %{"to" => [user2.ap_id]})}
75 end
76
77 def article_factory do
78 note_factory()
79 |> Map.put("type", "Article")
80 end
81
82 def tombstone_factory do
83 data = %{
84 "type" => "Tombstone",
85 "id" => Pleroma.Web.ActivityPub.Utils.generate_object_id(),
86 "formerType" => "Note",
87 "deleted" => DateTime.utc_now() |> DateTime.to_iso8601()
88 }
89
90 %Pleroma.Object{
91 data: data
92 }
93 end
94
95 def direct_note_activity_factory do
96 dm = insert(:direct_note)
97
98 data = %{
99 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
100 "type" => "Create",
101 "actor" => dm.data["actor"],
102 "to" => dm.data["to"],
103 "object" => dm.data,
104 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
105 "context" => dm.data["context"]
106 }
107
108 %Pleroma.Activity{
109 data: data,
110 actor: data["actor"],
111 recipients: data["to"]
112 }
113 end
114
115 def note_activity_factory(attrs \\ %{}) do
116 user = attrs[:user] || insert(:user)
117 note = attrs[:note] || insert(:note, user: user)
118
119 data = %{
120 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
121 "type" => "Create",
122 "actor" => note.data["actor"],
123 "to" => note.data["to"],
124 "object" => note.data,
125 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
126 "context" => note.data["context"]
127 }
128
129 %Pleroma.Activity{
130 data: data,
131 actor: data["actor"],
132 recipients: data["to"]
133 }
134 end
135
136 def article_activity_factory do
137 article = insert(:article)
138
139 data = %{
140 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
141 "type" => "Create",
142 "actor" => article.data["actor"],
143 "to" => article.data["to"],
144 "object" => article.data,
145 "published" => DateTime.utc_now() |> DateTime.to_iso8601(),
146 "context" => article.data["context"]
147 }
148
149 %Pleroma.Activity{
150 data: data,
151 actor: data["actor"],
152 recipients: data["to"]
153 }
154 end
155
156 def announce_activity_factory(attrs \\ %{}) do
157 note_activity = attrs[:note_activity] || insert(:note_activity)
158 user = attrs[:user] || insert(:user)
159
160 data = %{
161 "type" => "Announce",
162 "actor" => note_activity.actor,
163 "object" => note_activity.data["id"],
164 "to" => [user.follower_address, note_activity.data["actor"]],
165 "cc" => ["https://www.w3.org/ns/activitystreams#Public"],
166 "context" => note_activity.data["context"]
167 }
168
169 %Pleroma.Activity{
170 data: data,
171 actor: user.ap_id,
172 recipients: data["to"]
173 }
174 end
175
176 def like_activity_factory do
177 note_activity = insert(:note_activity)
178 user = insert(:user)
179
180 data = %{
181 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
182 "actor" => user.ap_id,
183 "type" => "Like",
184 "object" => note_activity.data["object"]["id"],
185 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
186 }
187
188 %Pleroma.Activity{
189 data: data
190 }
191 end
192
193 def follow_activity_factory do
194 follower = insert(:user)
195 followed = insert(:user)
196
197 data = %{
198 "id" => Pleroma.Web.ActivityPub.Utils.generate_activity_id(),
199 "actor" => follower.ap_id,
200 "type" => "Follow",
201 "object" => followed.ap_id,
202 "published_at" => DateTime.utc_now() |> DateTime.to_iso8601()
203 }
204
205 %Pleroma.Activity{
206 data: data,
207 actor: follower.ap_id
208 }
209 end
210
211 def websub_subscription_factory do
212 %Pleroma.Web.Websub.WebsubServerSubscription{
213 topic: "http://example.org",
214 callback: "http://example.org/callback",
215 secret: "here's a secret",
216 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 100),
217 state: "requested"
218 }
219 end
220
221 def websub_client_subscription_factory do
222 %Pleroma.Web.Websub.WebsubClientSubscription{
223 topic: "http://example.org",
224 secret: "here's a secret",
225 valid_until: nil,
226 state: "requested",
227 subscribers: []
228 }
229 end
230
231 def oauth_app_factory do
232 %Pleroma.Web.OAuth.App{
233 client_name: "Some client",
234 redirect_uris: "https://example.com/callback",
235 scopes: ["read", "write", "follow", "push"],
236 website: "https://example.com",
237 client_id: Ecto.UUID.generate(),
238 client_secret: "aaa;/&bbb"
239 }
240 end
241
242 def instance_factory do
243 %Pleroma.Instances.Instance{
244 host: "domain.com",
245 unreachable_since: nil
246 }
247 end
248
249 def oauth_token_factory do
250 oauth_app = insert(:oauth_app)
251
252 %Pleroma.Web.OAuth.Token{
253 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
254 refresh_token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(),
255 user: build(:user),
256 app_id: oauth_app.id,
257 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10)
258 }
259 end
260
261 def oauth_authorization_factory do
262 %Pleroma.Web.OAuth.Authorization{
263 token: :crypto.strong_rand_bytes(32) |> Base.url_encode64(padding: false),
264 scopes: ["read", "write", "follow", "push"],
265 valid_until: NaiveDateTime.add(NaiveDateTime.utc_now(), 60 * 10),
266 user: build(:user),
267 app: build(:oauth_app)
268 }
269 end
270
271 def push_subscription_factory do
272 %Pleroma.Web.Push.Subscription{
273 user: build(:user),
274 token: build(:oauth_token),
275 endpoint: "https://example.com/example/1234",
276 key_auth: "8eDyX_uCN0XRhSbY5hs7Hg==",
277 key_p256dh:
278 "BCIWgsnyXDv1VkhqL2P7YRBvdeuDnlwAPT2guNhdIoW3IP7GmHh1SMKPLxRf7x8vJy6ZFK3ol2ohgn_-0yP7QQA=",
279 data: %{}
280 }
281 end
282
283 def notification_factory do
284 %Pleroma.Notification{
285 user: build(:user)
286 }
287 end
288
289 def scheduled_activity_factory do
290 %Pleroma.ScheduledActivity{
291 user: build(:user),
292 scheduled_at: NaiveDateTime.add(NaiveDateTime.utc_now(), :timer.minutes(60), :millisecond),
293 params: build(:note) |> Map.from_struct() |> Map.get(:data)
294 }
295 end
296
297 def registration_factory do
298 user = insert(:user)
299
300 %Pleroma.Registration{
301 user: user,
302 provider: "twitter",
303 uid: "171799000",
304 info: %{
305 "name" => "John Doe",
306 "email" => "john@doe.com",
307 "nickname" => "johndoe",
308 "description" => "My bio"
309 }
310 }
311 end
312 end