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