ActivityPub Utils: Greatly speed up the follow / block activity fetching.
[akkoma] / test / web / activity_pub / activity_pub_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
6 use Pleroma.DataCase
7 alias Pleroma.Activity
8 alias Pleroma.Builders.ActivityBuilder
9 alias Pleroma.Instances
10 alias Pleroma.Object
11 alias Pleroma.User
12 alias Pleroma.Web.ActivityPub.ActivityPub
13 alias Pleroma.Web.ActivityPub.Utils
14 alias Pleroma.Web.CommonAPI
15
16 import Pleroma.Factory
17 import Tesla.Mock
18 import Mock
19
20 setup do
21 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
22 :ok
23 end
24
25 describe "fetching restricted by visibility" do
26 test "it restricts by the appropriate visibility" do
27 user = insert(:user)
28
29 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
30
31 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
32
33 {:ok, unlisted_activity} =
34 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
35
36 {:ok, private_activity} =
37 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
38
39 activities =
40 ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})
41
42 assert activities == [direct_activity]
43
44 activities =
45 ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})
46
47 assert activities == [unlisted_activity]
48
49 activities =
50 ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})
51
52 assert activities == [private_activity]
53
54 activities =
55 ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
56
57 assert activities == [public_activity]
58
59 activities =
60 ActivityPub.fetch_activities([], %{
61 :visibility => ~w[private public],
62 "actor_id" => user.ap_id
63 })
64
65 assert activities == [public_activity, private_activity]
66 end
67 end
68
69 describe "building a user from his ap id" do
70 test "it returns a user" do
71 user_id = "http://mastodon.example.org/users/admin"
72 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
73 assert user.ap_id == user_id
74 assert user.nickname == "admin@mastodon.example.org"
75 assert user.info.source_data
76 assert user.info.ap_enabled
77 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
78 end
79
80 test "it fetches the appropriate tag-restricted posts" do
81 user = insert(:user)
82
83 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
84 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
85 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
86
87 fetch_one = ActivityPub.fetch_activities([], %{"tag" => "test"})
88 fetch_two = ActivityPub.fetch_activities([], %{"tag" => ["test", "essais"]})
89
90 fetch_three =
91 ActivityPub.fetch_activities([], %{
92 "tag" => ["test", "essais"],
93 "tag_reject" => ["reject"]
94 })
95
96 fetch_four =
97 ActivityPub.fetch_activities([], %{
98 "tag" => ["test"],
99 "tag_all" => ["test", "reject"]
100 })
101
102 assert fetch_one == [status_one, status_three]
103 assert fetch_two == [status_one, status_two, status_three]
104 assert fetch_three == [status_one, status_two]
105 assert fetch_four == [status_three]
106 end
107 end
108
109 describe "insertion" do
110 test "drops activities beyond a certain limit" do
111 limit = Pleroma.Config.get([:instance, :remote_limit])
112
113 random_text =
114 :crypto.strong_rand_bytes(limit + 1)
115 |> Base.encode64()
116 |> binary_part(0, limit + 1)
117
118 data = %{
119 "ok" => true,
120 "object" => %{
121 "content" => random_text
122 }
123 }
124
125 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
126 end
127
128 test "doesn't drop activities with content being null" do
129 data = %{
130 "ok" => true,
131 "object" => %{
132 "content" => nil
133 }
134 }
135
136 assert {:ok, _} = ActivityPub.insert(data)
137 end
138
139 test "returns the activity if one with the same id is already in" do
140 activity = insert(:note_activity)
141 {:ok, new_activity} = ActivityPub.insert(activity.data)
142
143 assert activity.id == new_activity.id
144 end
145
146 test "inserts a given map into the activity database, giving it an id if it has none." do
147 data = %{
148 "ok" => true
149 }
150
151 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
152 assert activity.data["ok"] == data["ok"]
153 assert is_binary(activity.data["id"])
154
155 given_id = "bla"
156
157 data = %{
158 "ok" => true,
159 "id" => given_id,
160 "context" => "blabla"
161 }
162
163 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
164 assert activity.data["ok"] == data["ok"]
165 assert activity.data["id"] == given_id
166 assert activity.data["context"] == "blabla"
167 assert activity.data["context_id"]
168 end
169
170 test "adds a context when none is there" do
171 data = %{
172 "id" => "some_id",
173 "object" => %{
174 "id" => "object_id"
175 }
176 }
177
178 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
179
180 assert is_binary(activity.data["context"])
181 assert is_binary(activity.data["object"]["context"])
182 assert activity.data["context_id"]
183 assert activity.data["object"]["context_id"]
184 end
185
186 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
187 data = %{
188 "object" => %{
189 "type" => "Note",
190 "ok" => true
191 }
192 }
193
194 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
195 assert is_binary(activity.data["object"]["id"])
196 assert %Object{} = Object.get_by_ap_id(activity.data["object"]["id"])
197 end
198 end
199
200 describe "create activities" do
201 test "removes doubled 'to' recipients" do
202 user = insert(:user)
203
204 {:ok, activity} =
205 ActivityPub.create(%{
206 to: ["user1", "user1", "user2"],
207 actor: user,
208 context: "",
209 object: %{}
210 })
211
212 assert activity.data["to"] == ["user1", "user2"]
213 assert activity.actor == user.ap_id
214 assert activity.recipients == ["user1", "user2", user.ap_id]
215 end
216
217 test "increases user note count only for public activities" do
218 user = insert(:user)
219
220 {:ok, _} =
221 CommonAPI.post(Repo.get(User, user.id), %{"status" => "1", "visibility" => "public"})
222
223 {:ok, _} =
224 CommonAPI.post(Repo.get(User, user.id), %{"status" => "2", "visibility" => "unlisted"})
225
226 {:ok, _} =
227 CommonAPI.post(Repo.get(User, user.id), %{"status" => "2", "visibility" => "private"})
228
229 {:ok, _} =
230 CommonAPI.post(Repo.get(User, user.id), %{"status" => "3", "visibility" => "direct"})
231
232 user = Repo.get(User, user.id)
233 assert user.info.note_count == 2
234 end
235
236 test "increases replies count" do
237 user = insert(:user)
238 user2 = insert(:user)
239
240 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
241 ap_id = activity.data["id"]
242 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
243
244 # public
245 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
246 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
247 assert data["object"]["repliesCount"] == 1
248 assert object.data["repliesCount"] == 1
249
250 # unlisted
251 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
252 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
253 assert data["object"]["repliesCount"] == 2
254 assert object.data["repliesCount"] == 2
255
256 # private
257 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
258 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
259 assert data["object"]["repliesCount"] == 2
260 assert object.data["repliesCount"] == 2
261
262 # direct
263 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
264 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
265 assert data["object"]["repliesCount"] == 2
266 assert object.data["repliesCount"] == 2
267 end
268 end
269
270 describe "fetch activities for recipients" do
271 test "retrieve the activities for certain recipients" do
272 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
273 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
274 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
275
276 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
277 assert length(activities) == 2
278 assert activities == [activity_one, activity_two]
279 end
280 end
281
282 describe "fetch activities in context" do
283 test "retrieves activities that have a given context" do
284 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
285 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
286 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
287 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
288 activity_five = insert(:note_activity)
289 user = insert(:user)
290
291 {:ok, user} = User.block(user, %{ap_id: activity_five.data["actor"]})
292
293 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
294 assert activities == [activity_two, activity]
295 end
296 end
297
298 test "doesn't return blocked activities" do
299 activity_one = insert(:note_activity)
300 activity_two = insert(:note_activity)
301 activity_three = insert(:note_activity)
302 user = insert(:user)
303 booster = insert(:user)
304 {:ok, user} = User.block(user, %{ap_id: activity_one.data["actor"]})
305
306 activities =
307 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
308
309 assert Enum.member?(activities, activity_two)
310 assert Enum.member?(activities, activity_three)
311 refute Enum.member?(activities, activity_one)
312
313 {:ok, user} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
314
315 activities =
316 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
317
318 assert Enum.member?(activities, activity_two)
319 assert Enum.member?(activities, activity_three)
320 assert Enum.member?(activities, activity_one)
321
322 {:ok, user} = User.block(user, %{ap_id: activity_three.data["actor"]})
323 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
324 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
325 activity_three = Repo.get(Activity, activity_three.id)
326
327 activities =
328 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
329
330 assert Enum.member?(activities, activity_two)
331 refute Enum.member?(activities, activity_three)
332 refute Enum.member?(activities, boost_activity)
333 assert Enum.member?(activities, activity_one)
334
335 activities =
336 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
337
338 assert Enum.member?(activities, activity_two)
339 assert Enum.member?(activities, activity_three)
340 assert Enum.member?(activities, boost_activity)
341 assert Enum.member?(activities, activity_one)
342 end
343
344 test "doesn't return muted activities" do
345 activity_one = insert(:note_activity)
346 activity_two = insert(:note_activity)
347 activity_three = insert(:note_activity)
348 user = insert(:user)
349 booster = insert(:user)
350 {:ok, user} = User.mute(user, %User{ap_id: activity_one.data["actor"]})
351
352 activities =
353 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
354
355 assert Enum.member?(activities, activity_two)
356 assert Enum.member?(activities, activity_three)
357 refute Enum.member?(activities, activity_one)
358
359 # Calling with 'with_muted' will deliver muted activities, too.
360 activities =
361 ActivityPub.fetch_activities([], %{
362 "muting_user" => user,
363 "with_muted" => true,
364 "skip_preload" => true
365 })
366
367 assert Enum.member?(activities, activity_two)
368 assert Enum.member?(activities, activity_three)
369 assert Enum.member?(activities, activity_one)
370
371 {:ok, user} = User.unmute(user, %User{ap_id: activity_one.data["actor"]})
372
373 activities =
374 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
375
376 assert Enum.member?(activities, activity_two)
377 assert Enum.member?(activities, activity_three)
378 assert Enum.member?(activities, activity_one)
379
380 {:ok, user} = User.mute(user, %User{ap_id: activity_three.data["actor"]})
381 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
382 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
383 activity_three = Repo.get(Activity, activity_three.id)
384
385 activities =
386 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
387
388 assert Enum.member?(activities, activity_two)
389 refute Enum.member?(activities, activity_three)
390 refute Enum.member?(activities, boost_activity)
391 assert Enum.member?(activities, activity_one)
392
393 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
394
395 assert Enum.member?(activities, activity_two)
396 assert Enum.member?(activities, activity_three)
397 assert Enum.member?(activities, boost_activity)
398 assert Enum.member?(activities, activity_one)
399 end
400
401 test "does include announces on request" do
402 activity_three = insert(:note_activity)
403 user = insert(:user)
404 booster = insert(:user)
405
406 {:ok, user} = User.follow(user, booster)
407
408 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
409
410 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | user.following])
411
412 assert announce_activity.id == announce.id
413 end
414
415 test "excludes reblogs on request" do
416 user = insert(:user)
417 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
418 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
419
420 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
421
422 assert activity == expected_activity
423 end
424
425 describe "public fetch activities" do
426 test "doesn't retrieve unlisted activities" do
427 user = insert(:user)
428
429 {:ok, _unlisted_activity} =
430 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
431
432 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
433
434 [activity] = ActivityPub.fetch_public_activities()
435
436 assert activity == listed_activity
437 end
438
439 test "retrieves public activities" do
440 _activities = ActivityPub.fetch_public_activities()
441
442 %{public: public} = ActivityBuilder.public_and_non_public()
443
444 activities = ActivityPub.fetch_public_activities()
445 assert length(activities) == 1
446 assert Enum.at(activities, 0) == public
447 end
448
449 test "retrieves a maximum of 20 activities" do
450 activities = ActivityBuilder.insert_list(30)
451 last_expected = List.last(activities)
452
453 activities = ActivityPub.fetch_public_activities()
454 last = List.last(activities)
455
456 assert length(activities) == 20
457 assert last == last_expected
458 end
459
460 test "retrieves ids starting from a since_id" do
461 activities = ActivityBuilder.insert_list(30)
462 later_activities = ActivityBuilder.insert_list(10)
463 since_id = List.last(activities).id
464 last_expected = List.last(later_activities)
465
466 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
467 last = List.last(activities)
468
469 assert length(activities) == 10
470 assert last == last_expected
471 end
472
473 test "retrieves ids up to max_id" do
474 _first_activities = ActivityBuilder.insert_list(10)
475 activities = ActivityBuilder.insert_list(20)
476 later_activities = ActivityBuilder.insert_list(10)
477 max_id = List.first(later_activities).id
478 last_expected = List.last(activities)
479
480 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
481 last = List.last(activities)
482
483 assert length(activities) == 20
484 assert last == last_expected
485 end
486
487 test "doesn't return reblogs for users for whom reblogs have been muted" do
488 activity = insert(:note_activity)
489 user = insert(:user)
490 booster = insert(:user)
491 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
492
493 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
494
495 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
496
497 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
498 end
499
500 test "returns reblogs for users for whom reblogs have not been muted" do
501 activity = insert(:note_activity)
502 user = insert(:user)
503 booster = insert(:user)
504 {:ok, user} = CommonAPI.hide_reblogs(user, booster)
505 {:ok, user} = CommonAPI.show_reblogs(user, booster)
506
507 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
508
509 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
510
511 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
512 end
513 end
514
515 describe "like an object" do
516 test "adds a like activity to the db" do
517 note_activity = insert(:note_activity)
518 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
519 user = insert(:user)
520 user_two = insert(:user)
521
522 {:ok, like_activity, object} = ActivityPub.like(user, object)
523
524 assert like_activity.data["actor"] == user.ap_id
525 assert like_activity.data["type"] == "Like"
526 assert like_activity.data["object"] == object.data["id"]
527 assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
528 assert like_activity.data["context"] == object.data["context"]
529 assert object.data["like_count"] == 1
530 assert object.data["likes"] == [user.ap_id]
531
532 # Just return the original activity if the user already liked it.
533 {:ok, same_like_activity, object} = ActivityPub.like(user, object)
534
535 assert like_activity == same_like_activity
536 assert object.data["likes"] == [user.ap_id]
537
538 [note_activity] = Activity.get_all_create_by_object_ap_id(object.data["id"])
539 assert note_activity.data["object"]["like_count"] == 1
540
541 {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
542 assert object.data["like_count"] == 2
543 end
544 end
545
546 describe "unliking" do
547 test "unliking a previously liked object" do
548 note_activity = insert(:note_activity)
549 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
550 user = insert(:user)
551
552 # Unliking something that hasn't been liked does nothing
553 {:ok, object} = ActivityPub.unlike(user, object)
554 assert object.data["like_count"] == 0
555
556 {:ok, like_activity, object} = ActivityPub.like(user, object)
557 assert object.data["like_count"] == 1
558
559 {:ok, _, _, object} = ActivityPub.unlike(user, object)
560 assert object.data["like_count"] == 0
561
562 assert Repo.get(Activity, like_activity.id) == nil
563 end
564 end
565
566 describe "announcing an object" do
567 test "adds an announce activity to the db" do
568 note_activity = insert(:note_activity)
569 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
570 user = insert(:user)
571
572 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
573 assert object.data["announcement_count"] == 1
574 assert object.data["announcements"] == [user.ap_id]
575
576 assert announce_activity.data["to"] == [
577 User.ap_followers(user),
578 note_activity.data["actor"]
579 ]
580
581 assert announce_activity.data["object"] == object.data["id"]
582 assert announce_activity.data["actor"] == user.ap_id
583 assert announce_activity.data["context"] == object.data["context"]
584 end
585 end
586
587 describe "unannouncing an object" do
588 test "unannouncing a previously announced object" do
589 note_activity = insert(:note_activity)
590 object = Object.get_by_ap_id(note_activity.data["object"]["id"])
591 user = insert(:user)
592
593 # Unannouncing an object that is not announced does nothing
594 # {:ok, object} = ActivityPub.unannounce(user, object)
595 # assert object.data["announcement_count"] == 0
596
597 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
598 assert object.data["announcement_count"] == 1
599
600 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
601 assert object.data["announcement_count"] == 0
602
603 assert unannounce_activity.data["to"] == [
604 User.ap_followers(user),
605 announce_activity.data["actor"]
606 ]
607
608 assert unannounce_activity.data["type"] == "Undo"
609 assert unannounce_activity.data["object"] == announce_activity.data
610 assert unannounce_activity.data["actor"] == user.ap_id
611 assert unannounce_activity.data["context"] == announce_activity.data["context"]
612
613 assert Repo.get(Activity, announce_activity.id) == nil
614 end
615 end
616
617 describe "uploading files" do
618 test "copies the file to the configured folder" do
619 file = %Plug.Upload{
620 content_type: "image/jpg",
621 path: Path.absname("test/fixtures/image.jpg"),
622 filename: "an_image.jpg"
623 }
624
625 {:ok, %Object{} = object} = ActivityPub.upload(file)
626 assert object.data["name"] == "an_image.jpg"
627 end
628
629 test "works with base64 encoded images" do
630 file = %{
631 "img" => data_uri()
632 }
633
634 {:ok, %Object{}} = ActivityPub.upload(file)
635 end
636 end
637
638 describe "fetching an object" do
639 test "it fetches an object" do
640 {:ok, object} =
641 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
642
643 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
644 assert activity.data["id"]
645
646 {:ok, object_again} =
647 ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
648
649 assert [attachment] = object.data["attachment"]
650 assert is_list(attachment["url"])
651
652 assert object == object_again
653 end
654
655 test "it works with objects only available via Ostatus" do
656 {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
657 assert activity = Activity.get_create_by_object_ap_id(object.data["id"])
658 assert activity.data["id"]
659
660 {:ok, object_again} =
661 ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
662
663 assert object == object_again
664 end
665
666 test "it correctly stitches up conversations between ostatus and ap" do
667 last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
668 {:ok, object} = ActivityPub.fetch_object_from_id(last)
669
670 object = Object.get_by_ap_id(object.data["inReplyTo"])
671 assert object
672 end
673 end
674
675 describe "following / unfollowing" do
676 test "creates a follow activity" do
677 follower = insert(:user)
678 followed = insert(:user)
679
680 {:ok, activity} = ActivityPub.follow(follower, followed)
681 assert activity.data["type"] == "Follow"
682 assert activity.data["actor"] == follower.ap_id
683 assert activity.data["object"] == followed.ap_id
684 end
685
686 test "creates an undo activity for the last follow" do
687 follower = insert(:user)
688 followed = insert(:user)
689
690 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
691 {:ok, activity} = ActivityPub.unfollow(follower, followed)
692
693 assert activity.data["type"] == "Undo"
694 assert activity.data["actor"] == follower.ap_id
695
696 assert is_map(activity.data["object"])
697 assert activity.data["object"]["type"] == "Follow"
698 assert activity.data["object"]["object"] == followed.ap_id
699 assert activity.data["object"]["id"] == follow_activity.data["id"]
700 end
701 end
702
703 describe "blocking / unblocking" do
704 test "creates a block activity" do
705 blocker = insert(:user)
706 blocked = insert(:user)
707
708 {:ok, activity} = ActivityPub.block(blocker, blocked)
709
710 assert activity.data["type"] == "Block"
711 assert activity.data["actor"] == blocker.ap_id
712 assert activity.data["object"] == blocked.ap_id
713 end
714
715 test "creates an undo activity for the last block" do
716 blocker = insert(:user)
717 blocked = insert(:user)
718
719 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
720 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
721
722 assert activity.data["type"] == "Undo"
723 assert activity.data["actor"] == blocker.ap_id
724
725 assert is_map(activity.data["object"])
726 assert activity.data["object"]["type"] == "Block"
727 assert activity.data["object"]["object"] == blocked.ap_id
728 assert activity.data["object"]["id"] == block_activity.data["id"]
729 end
730 end
731
732 describe "deletion" do
733 test "it creates a delete activity and deletes the original object" do
734 note = insert(:note_activity)
735 object = Object.get_by_ap_id(note.data["object"]["id"])
736 {:ok, delete} = ActivityPub.delete(object)
737
738 assert delete.data["type"] == "Delete"
739 assert delete.data["actor"] == note.data["actor"]
740 assert delete.data["object"] == note.data["object"]["id"]
741
742 assert Repo.get(Activity, delete.id) != nil
743
744 assert Repo.get(Object, object.id).data["type"] == "Tombstone"
745 end
746
747 test "decrements user note count only for public activities" do
748 user = insert(:user, info: %{note_count: 10})
749
750 {:ok, a1} =
751 CommonAPI.post(Repo.get(User, user.id), %{"status" => "yeah", "visibility" => "public"})
752
753 {:ok, a2} =
754 CommonAPI.post(Repo.get(User, user.id), %{"status" => "yeah", "visibility" => "unlisted"})
755
756 {:ok, a3} =
757 CommonAPI.post(Repo.get(User, user.id), %{"status" => "yeah", "visibility" => "private"})
758
759 {:ok, a4} =
760 CommonAPI.post(Repo.get(User, user.id), %{"status" => "yeah", "visibility" => "direct"})
761
762 {:ok, _} = a1.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
763 {:ok, _} = a2.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
764 {:ok, _} = a3.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
765 {:ok, _} = a4.data["object"]["id"] |> Object.get_by_ap_id() |> ActivityPub.delete()
766
767 user = Repo.get(User, user.id)
768 assert user.info.note_count == 10
769 end
770
771 test "it creates a delete activity and checks that it is also sent to users mentioned by the deleted object" do
772 user = insert(:user)
773 note = insert(:note_activity)
774
775 {:ok, object} =
776 Object.get_by_ap_id(note.data["object"]["id"])
777 |> Object.change(%{
778 data: %{
779 "actor" => note.data["object"]["actor"],
780 "id" => note.data["object"]["id"],
781 "to" => [user.ap_id],
782 "type" => "Note"
783 }
784 })
785 |> Object.update_and_set_cache()
786
787 {:ok, delete} = ActivityPub.delete(object)
788
789 assert user.ap_id in delete.data["to"]
790 end
791
792 test "decreases reply count" do
793 user = insert(:user)
794 user2 = insert(:user)
795
796 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
797 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
798 ap_id = activity.data["id"]
799
800 {:ok, public_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
801 {:ok, unlisted_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
802 {:ok, private_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
803 {:ok, direct_reply} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
804
805 _ = CommonAPI.delete(direct_reply.id, user2)
806 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
807 assert data["object"]["repliesCount"] == 2
808 assert object.data["repliesCount"] == 2
809
810 _ = CommonAPI.delete(private_reply.id, user2)
811 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
812 assert data["object"]["repliesCount"] == 2
813 assert object.data["repliesCount"] == 2
814
815 _ = CommonAPI.delete(public_reply.id, user2)
816 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
817 assert data["object"]["repliesCount"] == 1
818 assert object.data["repliesCount"] == 1
819
820 _ = CommonAPI.delete(unlisted_reply.id, user2)
821 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
822 assert data["object"]["repliesCount"] == 0
823 assert object.data["repliesCount"] == 0
824 end
825 end
826
827 describe "timeline post-processing" do
828 test "it filters broken threads" do
829 user1 = insert(:user)
830 user2 = insert(:user)
831 user3 = insert(:user)
832
833 {:ok, user1} = User.follow(user1, user3)
834 assert User.following?(user1, user3)
835
836 {:ok, user2} = User.follow(user2, user3)
837 assert User.following?(user2, user3)
838
839 {:ok, user3} = User.follow(user3, user2)
840 assert User.following?(user3, user2)
841
842 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
843
844 {:ok, private_activity_1} =
845 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
846
847 {:ok, private_activity_2} =
848 CommonAPI.post(user2, %{
849 "status" => "hi 3",
850 "visibility" => "private",
851 "in_reply_to_status_id" => private_activity_1.id
852 })
853
854 {:ok, private_activity_3} =
855 CommonAPI.post(user3, %{
856 "status" => "hi 4",
857 "visibility" => "private",
858 "in_reply_to_status_id" => private_activity_2.id
859 })
860
861 activities = ActivityPub.fetch_activities([user1.ap_id | user1.following])
862
863 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
864 assert [public_activity, private_activity_1, private_activity_3] == activities
865 assert length(activities) == 3
866
867 activities = ActivityPub.contain_timeline(activities, user1)
868
869 assert [public_activity, private_activity_1] == activities
870 assert length(activities) == 2
871 end
872 end
873
874 test "it can fetch plume articles" do
875 {:ok, object} =
876 ActivityPub.fetch_object_from_id(
877 "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
878 )
879
880 assert object
881 end
882
883 describe "update" do
884 test "it creates an update activity with the new user data" do
885 user = insert(:user)
886 {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
887 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
888
889 {:ok, update} =
890 ActivityPub.update(%{
891 actor: user_data["id"],
892 to: [user.follower_address],
893 cc: [],
894 object: user_data
895 })
896
897 assert update.data["actor"] == user.ap_id
898 assert update.data["to"] == [user.follower_address]
899 assert update.data["object"]["id"] == user_data["id"]
900 assert update.data["object"]["type"] == user_data["type"]
901 end
902 end
903
904 test "it can fetch peertube videos" do
905 {:ok, object} =
906 ActivityPub.fetch_object_from_id(
907 "https://peertube.moe/videos/watch/df5f464b-be8d-46fb-ad81-2d4c2d1630e3"
908 )
909
910 assert object
911 end
912
913 test "returned pinned statuses" do
914 Pleroma.Config.put([:instance, :max_pinned_statuses], 3)
915 user = insert(:user)
916
917 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
918 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
919 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
920
921 CommonAPI.pin(activity_one.id, user)
922 user = refresh_record(user)
923
924 CommonAPI.pin(activity_two.id, user)
925 user = refresh_record(user)
926
927 CommonAPI.pin(activity_three.id, user)
928 user = refresh_record(user)
929
930 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
931
932 assert 3 = length(activities)
933 end
934
935 test "it can create a Flag activity" do
936 reporter = insert(:user)
937 target_account = insert(:user)
938 {:ok, activity} = CommonAPI.post(target_account, %{"status" => "foobar"})
939 context = Utils.generate_context_id()
940 content = "foobar"
941
942 reporter_ap_id = reporter.ap_id
943 target_ap_id = target_account.ap_id
944 activity_ap_id = activity.data["id"]
945
946 assert {:ok, activity} =
947 ActivityPub.flag(%{
948 actor: reporter,
949 context: context,
950 account: target_account,
951 statuses: [activity],
952 content: content
953 })
954
955 assert %Activity{
956 actor: ^reporter_ap_id,
957 data: %{
958 "type" => "Flag",
959 "content" => ^content,
960 "context" => ^context,
961 "object" => [^target_ap_id, ^activity_ap_id]
962 }
963 } = activity
964 end
965
966 describe "publish_one/1" do
967 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is not specified",
968 Instances,
969 [:passthrough],
970 [] do
971 actor = insert(:user)
972 inbox = "http://200.site/users/nick1/inbox"
973
974 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
975
976 assert called(Instances.set_reachable(inbox))
977 end
978
979 test_with_mock "calls `Instances.set_reachable` on successful federation if `unreachable_since` is set",
980 Instances,
981 [:passthrough],
982 [] do
983 actor = insert(:user)
984 inbox = "http://200.site/users/nick1/inbox"
985
986 assert {:ok, _} =
987 ActivityPub.publish_one(%{
988 inbox: inbox,
989 json: "{}",
990 actor: actor,
991 id: 1,
992 unreachable_since: NaiveDateTime.utc_now()
993 })
994
995 assert called(Instances.set_reachable(inbox))
996 end
997
998 test_with_mock "does NOT call `Instances.set_reachable` on successful federation if `unreachable_since` is nil",
999 Instances,
1000 [:passthrough],
1001 [] do
1002 actor = insert(:user)
1003 inbox = "http://200.site/users/nick1/inbox"
1004
1005 assert {:ok, _} =
1006 ActivityPub.publish_one(%{
1007 inbox: inbox,
1008 json: "{}",
1009 actor: actor,
1010 id: 1,
1011 unreachable_since: nil
1012 })
1013
1014 refute called(Instances.set_reachable(inbox))
1015 end
1016
1017 test_with_mock "calls `Instances.set_unreachable` on target inbox on non-2xx HTTP response code",
1018 Instances,
1019 [:passthrough],
1020 [] do
1021 actor = insert(:user)
1022 inbox = "http://404.site/users/nick1/inbox"
1023
1024 assert {:error, _} =
1025 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1026
1027 assert called(Instances.set_unreachable(inbox))
1028 end
1029
1030 test_with_mock "it calls `Instances.set_unreachable` on target inbox on request error of any kind",
1031 Instances,
1032 [:passthrough],
1033 [] do
1034 actor = insert(:user)
1035 inbox = "http://connrefused.site/users/nick1/inbox"
1036
1037 assert {:error, _} =
1038 ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1039
1040 assert called(Instances.set_unreachable(inbox))
1041 end
1042
1043 test_with_mock "does NOT call `Instances.set_unreachable` if target is reachable",
1044 Instances,
1045 [:passthrough],
1046 [] do
1047 actor = insert(:user)
1048 inbox = "http://200.site/users/nick1/inbox"
1049
1050 assert {:ok, _} = ActivityPub.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1})
1051
1052 refute called(Instances.set_unreachable(inbox))
1053 end
1054
1055 test_with_mock "does NOT call `Instances.set_unreachable` if target instance has non-nil `unreachable_since`",
1056 Instances,
1057 [:passthrough],
1058 [] do
1059 actor = insert(:user)
1060 inbox = "http://connrefused.site/users/nick1/inbox"
1061
1062 assert {:error, _} =
1063 ActivityPub.publish_one(%{
1064 inbox: inbox,
1065 json: "{}",
1066 actor: actor,
1067 id: 1,
1068 unreachable_since: NaiveDateTime.utc_now()
1069 })
1070
1071 refute called(Instances.set_unreachable(inbox))
1072 end
1073 end
1074
1075 def data_uri do
1076 File.read!("test/fixtures/avatar_data_uri")
1077 end
1078 end