Add unretweet TwAPI endpoint and cleanup AP.unannounce
[akkoma] / test / web / activity_pub / activity_pub_test.exs
1 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
2 use Pleroma.DataCase
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.Web.ActivityPub.Utils
5 alias Pleroma.Web.CommonAPI
6 alias Pleroma.{Activity, Object, User}
7 alias Pleroma.Builders.ActivityBuilder
8
9 import Pleroma.Factory
10
11 describe "building a user from his ap id" do
12 test "it returns a user" do
13 user_id = "http://mastodon.example.org/users/admin"
14 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
15 assert user.ap_id == user_id
16 assert user.nickname == "admin@mastodon.example.org"
17 assert user.info["source_data"]
18 assert user.info["ap_enabled"]
19 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
20 end
21 end
22
23 describe "insertion" do
24 test "returns the activity if one with the same id is already in" do
25 activity = insert(:note_activity)
26 {:ok, new_activity} = ActivityPub.insert(activity.data)
27
28 assert activity == new_activity
29 end
30
31 test "inserts a given map into the activity database, giving it an id if it has none." do
32 data = %{
33 "ok" => true
34 }
35
36 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
37 assert activity.data["ok"] == data["ok"]
38 assert is_binary(activity.data["id"])
39
40 given_id = "bla"
41
42 data = %{
43 "ok" => true,
44 "id" => given_id,
45 "context" => "blabla"
46 }
47
48 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
49 assert activity.data["ok"] == data["ok"]
50 assert activity.data["id"] == given_id
51 assert activity.data["context"] == "blabla"
52 assert activity.data["context_id"]
53 end
54
55 test "adds a context when none is there" do
56 data = %{
57 "id" => "some_id",
58 "object" => %{
59 "id" => "object_id"
60 }
61 }
62
63 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
64
65 assert is_binary(activity.data["context"])
66 assert is_binary(activity.data["object"]["context"])
67 assert activity.data["context_id"]
68 assert activity.data["object"]["context_id"]
69 end
70
71 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
72 data = %{
73 "object" => %{
74 "type" => "Note",
75 "ok" => true
76 }
77 }
78
79 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
80 assert is_binary(activity.data["object"]["id"])
81 assert %Object{} = Object.get_by_ap_id(activity.data["object"]["id"])
82 end
83 end
84
85 describe "create activities" do
86 test "removes doubled 'to' recipients" do
87 user = insert(:user)
88
89 {:ok, activity} =
90 ActivityPub.create(%{
91 to: ["user1", "user1", "user2"],
92 actor: user,
93 context: "",
94 object: %{}
95 })
96
97 assert activity.data["to"] == ["user1", "user2"]
98 assert activity.actor == user.ap_id
99 assert activity.recipients == ["user1", "user2"]
100 end
101 end
102
103 describe "fetch activities for recipients" do
104 test "retrieve the activities for certain recipients" do
105 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
106 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
107 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
108
109 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
110 assert length(activities) == 2
111 assert activities == [activity_one, activity_two]
112 end
113 end
114
115 describe "fetch activities in context" do
116 test "retrieves activities that have a given context" do
117 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
118 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
119 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
120 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
121 activity_five = insert(:note_activity)
122 user = insert(:user)
123
124 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
125
126 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
127 assert activities == [activity_two, activity]
128 end
129 end
130
131 test "doesn't return blocked activities" do
132 activity_one = insert(:note_activity)
133 activity_two = insert(:note_activity)
134 activity_three = insert(:note_activity)
135 user = insert(:user)
136 booster = insert(:user)
137 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
138
139 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
140
141 assert Enum.member?(activities, activity_two)
142 assert Enum.member?(activities, activity_three)
143 refute Enum.member?(activities, activity_one)
144
145 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
146
147 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
148
149 assert Enum.member?(activities, activity_two)
150 assert Enum.member?(activities, activity_three)
151 assert Enum.member?(activities, activity_one)
152
153 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
154 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
155 %Activity{} = boost_activity = Activity.get_create_activity_by_object_ap_id(id)
156 activity_three = Repo.get(Activity, activity_three.id)
157
158 activities = ActivityPub.fetch_activities([], %{"blocking_user" => user})
159
160 assert Enum.member?(activities, activity_two)
161 refute Enum.member?(activities, activity_three)
162 refute Enum.member?(activities, boost_activity)
163 assert Enum.member?(activities, activity_one)
164
165 activities = ActivityPub.fetch_activities([], %{"blocking_user" => nil})
166
167 assert Enum.member?(activities, activity_two)
168 assert Enum.member?(activities, activity_three)
169 assert Enum.member?(activities, boost_activity)
170 assert Enum.member?(activities, activity_one)
171 end
172
173 describe "public fetch activities" do
174 test "doesn't retrieve unlisted activities" do
175 user = insert(:user)
176
177 {:ok, unlisted_activity} =
178 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
179
180 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
181
182 [activity] = ActivityPub.fetch_public_activities()
183
184 assert activity == listed_activity
185 end
186
187 test "retrieves public activities" do
188 _activities = ActivityPub.fetch_public_activities()
189
190 %{public: public} = ActivityBuilder.public_and_non_public()
191
192 activities = ActivityPub.fetch_public_activities()
193 assert length(activities) == 1
194 assert Enum.at(activities, 0) == public
195 end
196
197 test "retrieves a maximum of 20 activities" do
198 activities = ActivityBuilder.insert_list(30)
199 last_expected = List.last(activities)
200
201 activities = ActivityPub.fetch_public_activities()
202 last = List.last(activities)
203
204 assert length(activities) == 20
205 assert last == last_expected
206 end
207
208 test "retrieves ids starting from a since_id" do
209 activities = ActivityBuilder.insert_list(30)
210 later_activities = ActivityBuilder.insert_list(10)
211 since_id = List.last(activities).id
212 last_expected = List.last(later_activities)
213
214 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
215 last = List.last(activities)
216
217 assert length(activities) == 10
218 assert last == last_expected
219 end
220
221 test "retrieves ids up to max_id" do
222 _first_activities = ActivityBuilder.insert_list(10)
223 activities = ActivityBuilder.insert_list(20)
224 later_activities = ActivityBuilder.insert_list(10)
225 max_id = List.first(later_activities).id
226 last_expected = List.last(activities)
227
228 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
229 last = List.last(activities)
230
231 assert length(activities) == 20
232 assert last == last_expected
233 end
234 end
235
236 describe "like an object" do
237 test "adds a like activity to the db" do
238 note_activity = insert(:note_activity)
239 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
240 user = insert(:user)
241 user_two = insert(:user)
242
243 {:ok, like_activity, object} = ActivityPub.like(user, object)
244
245 assert like_activity.data["actor"] == user.ap_id
246 assert like_activity.data["type"] == "Like"
247 assert like_activity.data["object"] == object.data["id"]
248 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
249 assert like_activity.data["context"] == object.data["context"]
250 assert object.data["like_count"] == 1
251 assert object.data["likes"] == [user.ap_id]
252
253 # Just return the original activity if the user already liked it.
254 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
255
256 assert like_activity == same_like_activity
257 assert object.data["likes"] == [user.ap_id]
258
259 [note_activity] = Activity.all_by_object_ap_id(object.data["id"])
260 assert note_activity.data["object"]["like_count"] == 1
261
262 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
263 assert object.data["like_count"] == 2
264 end
265 end
266
267 describe "unliking" do
268 test "unliking a previously liked object" do
269 note_activity = insert(:note_activity)
270 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
271 user = insert(:user)
272
273 # Unliking something that hasn't been liked does nothing
274 {:ok, object} = ActivityPub.unlike(user, object)
275 assert object.data["like_count"] == 0
276
277 {:ok, like_activity, object} = ActivityPub.like(user, object)
278 assert object.data["like_count"] == 1
279
280 {:ok, _, _, object} = ActivityPub.unlike(user, object)
281 assert object.data["like_count"] == 0
282
283 assert Repo.get(Activity, like_activity.id) == nil
284 end
285 end
286
287 describe "announcing an object" do
288 test "adds an announce activity to the db" do
289 note_activity = insert(:note_activity)
290 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
291 user = insert(:user)
292
293 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
294 assert object.data["announcement_count"] == 1
295 assert object.data["announcements"] == [user.ap_id]
296
297 assert announce_activity.data["to"] == [
298 User.ap_followers(user),
299 note_activity.data["actor"]
300 ]
301
302 assert announce_activity.data["object"] == object.data["id"]
303 assert announce_activity.data["actor"] == user.ap_id
304 assert announce_activity.data["context"] == object.data["context"]
305 end
306 end
307
308 describe "unannouncing an object" do
309 test "unannouncing a previously announced object" do
310 note_activity = insert(:note_activity)
311 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
312 user = insert(:user)
313
314 # Unannouncing an object that is not announced does nothing
315 # {:ok, object} = ActivityPub.unannounce(user, object)
316 # assert object.data["announcement_count"] == 0
317
318 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
319 assert object.data["announcement_count"] == 1
320
321 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
322 assert object.data["announcement_count"] == 0
323
324 assert unannounce_activity.data["to"] == [
325 User.ap_followers(user),
326 announce_activity.data["actor"]
327 ]
328
329 assert unannounce_activity.data["type"] == "Undo"
330 assert unannounce_activity.data["object"] == announce_activity.data
331 assert unannounce_activity.data["actor"] == user.ap_id
332 assert unannounce_activity.data["context"] == announce_activity.data["context"]
333
334 assert Repo.get(Activity, announce_activity.id) == nil
335 end
336 end
337
338 describe "uploading files" do
339 test "copies the file to the configured folder" do
340 file = %Plug.Upload{
341 content_type: "image/jpg",
342 path: Path.absname("test/fixtures/image.jpg"),
343 filename: "an_image.jpg"
344 }
345
346 {:ok, %Object{} = object} = ActivityPub.upload(file)
347 assert object.data["name"] == "an_image.jpg"
348 end
349
350 test "works with base64 encoded images" do
351 file = %{
352 "img" => data_uri()
353 }
354
355 {:ok, %Object{}} = ActivityPub.upload(file)
356 end
357 end
358
359 describe "fetch the latest Follow" do
360 test "fetches the latest Follow activity" do
361 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
362 follower = Repo.get_by(User, ap_id: activity.data["actor"])
363 followed = Repo.get_by(User, ap_id: activity.data["object"])
364
365 assert activity == Utils.fetch_latest_follow(follower, followed)
366 end
367 end
368
369 describe "fetching an object" do
370 test "it fetches an object" do
371 {:ok, object} =
372 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
373
374 assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
375 assert activity.data["id"]
376
377 {:ok, object_again} =
378 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
379
380 assert [attachment] = object.data["attachment"]
381 assert is_list(attachment["url"])
382
383 assert object == object_again
384 end
385
386 test "it works with objects only available via Ostatus" do
387 {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
388 assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
389 assert activity.data["id"]
390
391 {:ok, object_again} =
392 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
393
394 assert object == object_again
395 end
396
397 test "it correctly stitches up conversations between ostatus and ap" do
398 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
399 {:ok, object} = ActivityPub.fetch_object_from_id(last)
400
401 object = Object.get_by_ap_id(object.data["inReplyTo"])
402 assert object
403 end
404 end
405
406 describe "following / unfollowing" do
407 test "creates a follow activity" do
408 follower = insert(:user)
409 followed = insert(:user)
410
411 {:ok, activity} = ActivityPub.follow(follower, followed)
412 assert activity.data["type"] == "Follow"
413 assert activity.data["actor"] == follower.ap_id
414 assert activity.data["object"] == followed.ap_id
415 end
416
417 test "creates an undo activity for the last follow" do
418 follower = insert(:user)
419 followed = insert(:user)
420
421 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
422 {:ok, activity} = ActivityPub.unfollow(follower, followed)
423
424 assert activity.data["type"] == "Undo"
425 assert activity.data["actor"] == follower.ap_id
426
427 assert is_map(activity.data["object"])
428 assert activity.data["object"]["type"] == "Follow"
429 assert activity.data["object"]["object"] == followed.ap_id
430 assert activity.data["object"]["id"] == follow_activity.data["id"]
431 end
432 end
433
434 describe "blocking / unblocking" do
435 test "creates a block activity" do
436 blocker = insert(:user)
437 blocked = insert(:user)
438
439 {:ok, activity} = ActivityPub.block(blocker, blocked)
440
441 assert activity.data["type"] == "Block"
442 assert activity.data["actor"] == blocker.ap_id
443 assert activity.data["object"] == blocked.ap_id
444 end
445
446 test "creates an undo activity for the last block" do
447 blocker = insert(:user)
448 blocked = insert(:user)
449
450 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
451 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
452
453 assert activity.data["type"] == "Undo"
454 assert activity.data["actor"] == blocker.ap_id
455
456 assert is_map(activity.data["object"])
457 assert activity.data["object"]["type"] == "Block"
458 assert activity.data["object"]["object"] == blocked.ap_id
459 assert activity.data["object"]["id"] == block_activity.data["id"]
460 end
461 end
462
463 describe "deletion" do
464 test "it creates a delete activity and deletes the original object" do
465 note = insert(:note_activity)
466 object = Object.get_by_ap_id(note.data["object"]["id"])
467 {:ok, delete} = ActivityPub.delete(object)
468
469 assert delete.data["type"] == "Delete"
470 assert delete.data["actor"] == note.data["actor"]
471 assert delete.data["object"] == note.data["object"]["id"]
472
473 assert Repo.get(Activity, delete.id) != nil
474
475 assert Repo.get(Object, object.id) == nil
476 end
477 end
478
479 describe "update" do
480 test "it creates an update activity with the new user data" do
481 user = insert(:user)
482 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
483 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
484
485 {:ok, update} =
486 ActivityPub.update(%{
487 actor: user_data["id"],
488 to: [user.follower_address],
489 cc: [],
490 object: user_data
491 })
492
493 assert update.data["actor"] == user.ap_id
494 assert update.data["to"] == [user.follower_address]
495 assert update.data["object"]["id"] == user_data["id"]
496 assert update.data["object"]["type"] == user_data["type"]
497 end
498 end
499
500 def data_uri do
501 File.read!("test/fixtures/avatar_data_uri")
502 end
503 end