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