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