Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into feature/emojireactv...
[akkoma] / test / web / activity_pub / activity_pub_test.exs
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 use Oban.Testing, repo: Pleroma.Repo
8
9 alias Pleroma.Activity
10 alias Pleroma.Builders.ActivityBuilder
11 alias Pleroma.Config
12 alias Pleroma.Notification
13 alias Pleroma.Object
14 alias Pleroma.User
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.Utils
17 alias Pleroma.Web.AdminAPI.AccountView
18 alias Pleroma.Web.CommonAPI
19 alias Pleroma.Web.Federator
20
21 import ExUnit.CaptureLog
22 import Mock
23 import Pleroma.Factory
24 import Tesla.Mock
25
26 setup do
27 mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
28 :ok
29 end
30
31 setup do: clear_config([:instance, :federating])
32
33 describe "streaming out participations" do
34 test "it streams them out" do
35 user = insert(:user)
36 {:ok, activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
37
38 {:ok, conversation} = Pleroma.Conversation.create_or_bump_for(activity)
39
40 participations =
41 conversation.participations
42 |> Repo.preload(:user)
43
44 with_mock Pleroma.Web.Streamer,
45 stream: fn _, _ -> nil end do
46 ActivityPub.stream_out_participations(conversation.participations)
47
48 assert called(Pleroma.Web.Streamer.stream("participation", participations))
49 end
50 end
51
52 test "streams them out on activity creation" do
53 user_one = insert(:user)
54 user_two = insert(:user)
55
56 with_mock Pleroma.Web.Streamer,
57 stream: fn _, _ -> nil end do
58 {:ok, activity} =
59 CommonAPI.post(user_one, %{
60 "status" => "@#{user_two.nickname}",
61 "visibility" => "direct"
62 })
63
64 conversation =
65 activity.data["context"]
66 |> Pleroma.Conversation.get_for_ap_id()
67 |> Repo.preload(participations: :user)
68
69 assert called(Pleroma.Web.Streamer.stream("participation", conversation.participations))
70 end
71 end
72 end
73
74 describe "fetching restricted by visibility" do
75 test "it restricts by the appropriate visibility" do
76 user = insert(:user)
77
78 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
79
80 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
81
82 {:ok, unlisted_activity} =
83 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
84
85 {:ok, private_activity} =
86 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
87
88 activities =
89 ActivityPub.fetch_activities([], %{:visibility => "direct", "actor_id" => user.ap_id})
90
91 assert activities == [direct_activity]
92
93 activities =
94 ActivityPub.fetch_activities([], %{:visibility => "unlisted", "actor_id" => user.ap_id})
95
96 assert activities == [unlisted_activity]
97
98 activities =
99 ActivityPub.fetch_activities([], %{:visibility => "private", "actor_id" => user.ap_id})
100
101 assert activities == [private_activity]
102
103 activities =
104 ActivityPub.fetch_activities([], %{:visibility => "public", "actor_id" => user.ap_id})
105
106 assert activities == [public_activity]
107
108 activities =
109 ActivityPub.fetch_activities([], %{
110 :visibility => ~w[private public],
111 "actor_id" => user.ap_id
112 })
113
114 assert activities == [public_activity, private_activity]
115 end
116 end
117
118 describe "fetching excluded by visibility" do
119 test "it excludes by the appropriate visibility" do
120 user = insert(:user)
121
122 {:ok, public_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "public"})
123
124 {:ok, direct_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "direct"})
125
126 {:ok, unlisted_activity} =
127 CommonAPI.post(user, %{"status" => ".", "visibility" => "unlisted"})
128
129 {:ok, private_activity} =
130 CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
131
132 activities =
133 ActivityPub.fetch_activities([], %{
134 "exclude_visibilities" => "direct",
135 "actor_id" => user.ap_id
136 })
137
138 assert public_activity in activities
139 assert unlisted_activity in activities
140 assert private_activity in activities
141 refute direct_activity in activities
142
143 activities =
144 ActivityPub.fetch_activities([], %{
145 "exclude_visibilities" => "unlisted",
146 "actor_id" => user.ap_id
147 })
148
149 assert public_activity in activities
150 refute unlisted_activity in activities
151 assert private_activity in activities
152 assert direct_activity in activities
153
154 activities =
155 ActivityPub.fetch_activities([], %{
156 "exclude_visibilities" => "private",
157 "actor_id" => user.ap_id
158 })
159
160 assert public_activity in activities
161 assert unlisted_activity in activities
162 refute private_activity in activities
163 assert direct_activity in activities
164
165 activities =
166 ActivityPub.fetch_activities([], %{
167 "exclude_visibilities" => "public",
168 "actor_id" => user.ap_id
169 })
170
171 refute public_activity in activities
172 assert unlisted_activity in activities
173 assert private_activity in activities
174 assert direct_activity in activities
175 end
176 end
177
178 describe "building a user from his ap id" do
179 test "it returns a user" do
180 user_id = "http://mastodon.example.org/users/admin"
181 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
182 assert user.ap_id == user_id
183 assert user.nickname == "admin@mastodon.example.org"
184 assert user.ap_enabled
185 assert user.follower_address == "http://mastodon.example.org/users/admin/followers"
186 end
187
188 test "it returns a user that is invisible" do
189 user_id = "http://mastodon.example.org/users/relay"
190 {:ok, user} = ActivityPub.make_user_from_ap_id(user_id)
191 assert User.invisible?(user)
192 end
193
194 test "it fetches the appropriate tag-restricted posts" do
195 user = insert(:user)
196
197 {:ok, status_one} = CommonAPI.post(user, %{"status" => ". #test"})
198 {:ok, status_two} = CommonAPI.post(user, %{"status" => ". #essais"})
199 {:ok, status_three} = CommonAPI.post(user, %{"status" => ". #test #reject"})
200
201 fetch_one = ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => "test"})
202
203 fetch_two =
204 ActivityPub.fetch_activities([], %{"type" => "Create", "tag" => ["test", "essais"]})
205
206 fetch_three =
207 ActivityPub.fetch_activities([], %{
208 "type" => "Create",
209 "tag" => ["test", "essais"],
210 "tag_reject" => ["reject"]
211 })
212
213 fetch_four =
214 ActivityPub.fetch_activities([], %{
215 "type" => "Create",
216 "tag" => ["test"],
217 "tag_all" => ["test", "reject"]
218 })
219
220 assert fetch_one == [status_one, status_three]
221 assert fetch_two == [status_one, status_two, status_three]
222 assert fetch_three == [status_one, status_two]
223 assert fetch_four == [status_three]
224 end
225 end
226
227 describe "insertion" do
228 test "drops activities beyond a certain limit" do
229 limit = Config.get([:instance, :remote_limit])
230
231 random_text =
232 :crypto.strong_rand_bytes(limit + 1)
233 |> Base.encode64()
234 |> binary_part(0, limit + 1)
235
236 data = %{
237 "ok" => true,
238 "object" => %{
239 "content" => random_text
240 }
241 }
242
243 assert {:error, {:remote_limit_error, _}} = ActivityPub.insert(data)
244 end
245
246 test "doesn't drop activities with content being null" do
247 user = insert(:user)
248
249 data = %{
250 "actor" => user.ap_id,
251 "to" => [],
252 "object" => %{
253 "actor" => user.ap_id,
254 "to" => [],
255 "type" => "Note",
256 "content" => nil
257 }
258 }
259
260 assert {:ok, _} = ActivityPub.insert(data)
261 end
262
263 test "returns the activity if one with the same id is already in" do
264 activity = insert(:note_activity)
265 {:ok, new_activity} = ActivityPub.insert(activity.data)
266
267 assert activity.id == new_activity.id
268 end
269
270 test "inserts a given map into the activity database, giving it an id if it has none." do
271 user = insert(:user)
272
273 data = %{
274 "actor" => user.ap_id,
275 "to" => [],
276 "object" => %{
277 "actor" => user.ap_id,
278 "to" => [],
279 "type" => "Note",
280 "content" => "hey"
281 }
282 }
283
284 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
285 assert activity.data["ok"] == data["ok"]
286 assert is_binary(activity.data["id"])
287
288 given_id = "bla"
289
290 data = %{
291 "id" => given_id,
292 "actor" => user.ap_id,
293 "to" => [],
294 "context" => "blabla",
295 "object" => %{
296 "actor" => user.ap_id,
297 "to" => [],
298 "type" => "Note",
299 "content" => "hey"
300 }
301 }
302
303 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
304 assert activity.data["ok"] == data["ok"]
305 assert activity.data["id"] == given_id
306 assert activity.data["context"] == "blabla"
307 assert activity.data["context_id"]
308 end
309
310 test "adds a context when none is there" do
311 user = insert(:user)
312
313 data = %{
314 "actor" => user.ap_id,
315 "to" => [],
316 "object" => %{
317 "actor" => user.ap_id,
318 "to" => [],
319 "type" => "Note",
320 "content" => "hey"
321 }
322 }
323
324 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
325 object = Pleroma.Object.normalize(activity)
326
327 assert is_binary(activity.data["context"])
328 assert is_binary(object.data["context"])
329 assert activity.data["context_id"]
330 assert object.data["context_id"]
331 end
332
333 test "adds an id to a given object if it lacks one and is a note and inserts it to the object database" do
334 user = insert(:user)
335
336 data = %{
337 "actor" => user.ap_id,
338 "to" => [],
339 "object" => %{
340 "actor" => user.ap_id,
341 "to" => [],
342 "type" => "Note",
343 "content" => "hey"
344 }
345 }
346
347 {:ok, %Activity{} = activity} = ActivityPub.insert(data)
348 assert object = Object.normalize(activity)
349 assert is_binary(object.data["id"])
350 end
351 end
352
353 describe "listen activities" do
354 test "does not increase user note count" do
355 user = insert(:user)
356
357 {:ok, activity} =
358 ActivityPub.listen(%{
359 to: ["https://www.w3.org/ns/activitystreams#Public"],
360 actor: user,
361 context: "",
362 object: %{
363 "actor" => user.ap_id,
364 "to" => ["https://www.w3.org/ns/activitystreams#Public"],
365 "artist" => "lain",
366 "title" => "lain radio episode 1",
367 "length" => 180_000,
368 "type" => "Audio"
369 }
370 })
371
372 assert activity.actor == user.ap_id
373
374 user = User.get_cached_by_id(user.id)
375 assert user.note_count == 0
376 end
377
378 test "can be fetched into a timeline" do
379 _listen_activity_1 = insert(:listen)
380 _listen_activity_2 = insert(:listen)
381 _listen_activity_3 = insert(:listen)
382
383 timeline = ActivityPub.fetch_activities([], %{"type" => ["Listen"]})
384
385 assert length(timeline) == 3
386 end
387 end
388
389 describe "create activities" do
390 test "it reverts create" do
391 user = insert(:user)
392
393 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
394 assert {:error, :reverted} =
395 ActivityPub.create(%{
396 to: ["user1", "user2"],
397 actor: user,
398 context: "",
399 object: %{
400 "to" => ["user1", "user2"],
401 "type" => "Note",
402 "content" => "testing"
403 }
404 })
405 end
406
407 assert Repo.aggregate(Activity, :count, :id) == 0
408 assert Repo.aggregate(Object, :count, :id) == 0
409 end
410
411 test "removes doubled 'to' recipients" do
412 user = insert(:user)
413
414 {:ok, activity} =
415 ActivityPub.create(%{
416 to: ["user1", "user1", "user2"],
417 actor: user,
418 context: "",
419 object: %{
420 "to" => ["user1", "user1", "user2"],
421 "type" => "Note",
422 "content" => "testing"
423 }
424 })
425
426 assert activity.data["to"] == ["user1", "user2"]
427 assert activity.actor == user.ap_id
428 assert activity.recipients == ["user1", "user2", user.ap_id]
429 end
430
431 test "increases user note count only for public activities" do
432 user = insert(:user)
433
434 {:ok, _} =
435 CommonAPI.post(User.get_cached_by_id(user.id), %{
436 "status" => "1",
437 "visibility" => "public"
438 })
439
440 {:ok, _} =
441 CommonAPI.post(User.get_cached_by_id(user.id), %{
442 "status" => "2",
443 "visibility" => "unlisted"
444 })
445
446 {:ok, _} =
447 CommonAPI.post(User.get_cached_by_id(user.id), %{
448 "status" => "2",
449 "visibility" => "private"
450 })
451
452 {:ok, _} =
453 CommonAPI.post(User.get_cached_by_id(user.id), %{
454 "status" => "3",
455 "visibility" => "direct"
456 })
457
458 user = User.get_cached_by_id(user.id)
459 assert user.note_count == 2
460 end
461
462 test "increases replies count" do
463 user = insert(:user)
464 user2 = insert(:user)
465
466 {:ok, activity} = CommonAPI.post(user, %{"status" => "1", "visibility" => "public"})
467 ap_id = activity.data["id"]
468 reply_data = %{"status" => "1", "in_reply_to_status_id" => activity.id}
469
470 # public
471 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "public"))
472 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
473 assert object.data["repliesCount"] == 1
474
475 # unlisted
476 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "unlisted"))
477 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
478 assert object.data["repliesCount"] == 2
479
480 # private
481 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "private"))
482 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
483 assert object.data["repliesCount"] == 2
484
485 # direct
486 {:ok, _} = CommonAPI.post(user2, Map.put(reply_data, "visibility", "direct"))
487 assert %{data: data, object: object} = Activity.get_by_ap_id_with_object(ap_id)
488 assert object.data["repliesCount"] == 2
489 end
490 end
491
492 describe "fetch activities for recipients" do
493 test "retrieve the activities for certain recipients" do
494 {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
495 {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
496 {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
497
498 activities = ActivityPub.fetch_activities(["someone", "someone_else"])
499 assert length(activities) == 2
500 assert activities == [activity_one, activity_two]
501 end
502 end
503
504 describe "fetch activities in context" do
505 test "retrieves activities that have a given context" do
506 {:ok, activity} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
507 {:ok, activity_two} = ActivityBuilder.insert(%{"type" => "Create", "context" => "2hu"})
508 {:ok, _activity_three} = ActivityBuilder.insert(%{"type" => "Create", "context" => "3hu"})
509 {:ok, _activity_four} = ActivityBuilder.insert(%{"type" => "Announce", "context" => "2hu"})
510 activity_five = insert(:note_activity)
511 user = insert(:user)
512
513 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_five.data["actor"]})
514
515 activities = ActivityPub.fetch_activities_for_context("2hu", %{"blocking_user" => user})
516 assert activities == [activity_two, activity]
517 end
518 end
519
520 test "doesn't return blocked activities" do
521 activity_one = insert(:note_activity)
522 activity_two = insert(:note_activity)
523 activity_three = insert(:note_activity)
524 user = insert(:user)
525 booster = insert(:user)
526 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_one.data["actor"]})
527
528 activities =
529 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
530
531 assert Enum.member?(activities, activity_two)
532 assert Enum.member?(activities, activity_three)
533 refute Enum.member?(activities, activity_one)
534
535 {:ok, _user_block} = User.unblock(user, %{ap_id: activity_one.data["actor"]})
536
537 activities =
538 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
539
540 assert Enum.member?(activities, activity_two)
541 assert Enum.member?(activities, activity_three)
542 assert Enum.member?(activities, activity_one)
543
544 {:ok, _user_relationship} = User.block(user, %{ap_id: activity_three.data["actor"]})
545 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
546 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
547 activity_three = Activity.get_by_id(activity_three.id)
548
549 activities =
550 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
551
552 assert Enum.member?(activities, activity_two)
553 refute Enum.member?(activities, activity_three)
554 refute Enum.member?(activities, boost_activity)
555 assert Enum.member?(activities, activity_one)
556
557 activities =
558 ActivityPub.fetch_activities([], %{"blocking_user" => nil, "skip_preload" => true})
559
560 assert Enum.member?(activities, activity_two)
561 assert Enum.member?(activities, activity_three)
562 assert Enum.member?(activities, boost_activity)
563 assert Enum.member?(activities, activity_one)
564 end
565
566 test "doesn't return transitive interactions concerning blocked users" do
567 blocker = insert(:user)
568 blockee = insert(:user)
569 friend = insert(:user)
570
571 {:ok, _user_relationship} = User.block(blocker, blockee)
572
573 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
574
575 {:ok, activity_two} = CommonAPI.post(friend, %{"status" => "hey! @#{blockee.nickname}"})
576
577 {:ok, activity_three} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
578
579 {:ok, activity_four} = CommonAPI.post(blockee, %{"status" => "hey! @#{blocker.nickname}"})
580
581 activities = ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
582
583 assert Enum.member?(activities, activity_one)
584 refute Enum.member?(activities, activity_two)
585 refute Enum.member?(activities, activity_three)
586 refute Enum.member?(activities, activity_four)
587 end
588
589 test "doesn't return announce activities concerning blocked users" do
590 blocker = insert(:user)
591 blockee = insert(:user)
592 friend = insert(:user)
593
594 {:ok, _user_relationship} = User.block(blocker, blockee)
595
596 {:ok, activity_one} = CommonAPI.post(friend, %{"status" => "hey!"})
597
598 {:ok, activity_two} = CommonAPI.post(blockee, %{"status" => "hey! @#{friend.nickname}"})
599
600 {:ok, activity_three, _} = CommonAPI.repeat(activity_two.id, friend)
601
602 activities =
603 ActivityPub.fetch_activities([], %{"blocking_user" => blocker})
604 |> Enum.map(fn act -> act.id end)
605
606 assert Enum.member?(activities, activity_one.id)
607 refute Enum.member?(activities, activity_two.id)
608 refute Enum.member?(activities, activity_three.id)
609 end
610
611 test "doesn't return activities from blocked domains" do
612 domain = "dogwhistle.zone"
613 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
614 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
615 activity = insert(:note_activity, %{note: note})
616 user = insert(:user)
617 {:ok, user} = User.block_domain(user, domain)
618
619 activities =
620 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
621
622 refute activity in activities
623
624 followed_user = insert(:user)
625 ActivityPub.follow(user, followed_user)
626 {:ok, repeat_activity, _} = CommonAPI.repeat(activity.id, followed_user)
627
628 activities =
629 ActivityPub.fetch_activities([], %{"blocking_user" => user, "skip_preload" => true})
630
631 refute repeat_activity in activities
632 end
633
634 test "does return activities from followed users on blocked domains" do
635 domain = "meanies.social"
636 domain_user = insert(:user, %{ap_id: "https://#{domain}/@pundit"})
637 blocker = insert(:user)
638
639 {:ok, blocker} = User.follow(blocker, domain_user)
640 {:ok, blocker} = User.block_domain(blocker, domain)
641
642 assert User.following?(blocker, domain_user)
643 assert User.blocks_domain?(blocker, domain_user)
644 refute User.blocks?(blocker, domain_user)
645
646 note = insert(:note, %{data: %{"actor" => domain_user.ap_id}})
647 activity = insert(:note_activity, %{note: note})
648
649 activities =
650 ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true})
651
652 assert activity in activities
653
654 # And check that if the guy we DO follow boosts someone else from their domain,
655 # that should be hidden
656 another_user = insert(:user, %{ap_id: "https://#{domain}/@meanie2"})
657 bad_note = insert(:note, %{data: %{"actor" => another_user.ap_id}})
658 bad_activity = insert(:note_activity, %{note: bad_note})
659 {:ok, repeat_activity, _} = CommonAPI.repeat(bad_activity.id, domain_user)
660
661 activities =
662 ActivityPub.fetch_activities([], %{"blocking_user" => blocker, "skip_preload" => true})
663
664 refute repeat_activity in activities
665 end
666
667 test "doesn't return muted activities" do
668 activity_one = insert(:note_activity)
669 activity_two = insert(:note_activity)
670 activity_three = insert(:note_activity)
671 user = insert(:user)
672 booster = insert(:user)
673
674 activity_one_actor = User.get_by_ap_id(activity_one.data["actor"])
675 {:ok, _user_relationships} = User.mute(user, activity_one_actor)
676
677 activities =
678 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
679
680 assert Enum.member?(activities, activity_two)
681 assert Enum.member?(activities, activity_three)
682 refute Enum.member?(activities, activity_one)
683
684 # Calling with 'with_muted' will deliver muted activities, too.
685 activities =
686 ActivityPub.fetch_activities([], %{
687 "muting_user" => user,
688 "with_muted" => true,
689 "skip_preload" => true
690 })
691
692 assert Enum.member?(activities, activity_two)
693 assert Enum.member?(activities, activity_three)
694 assert Enum.member?(activities, activity_one)
695
696 {:ok, _user_mute} = User.unmute(user, activity_one_actor)
697
698 activities =
699 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
700
701 assert Enum.member?(activities, activity_two)
702 assert Enum.member?(activities, activity_three)
703 assert Enum.member?(activities, activity_one)
704
705 activity_three_actor = User.get_by_ap_id(activity_three.data["actor"])
706 {:ok, _user_relationships} = User.mute(user, activity_three_actor)
707 {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(activity_three.id, booster)
708 %Activity{} = boost_activity = Activity.get_create_by_object_ap_id(id)
709 activity_three = Activity.get_by_id(activity_three.id)
710
711 activities =
712 ActivityPub.fetch_activities([], %{"muting_user" => user, "skip_preload" => true})
713
714 assert Enum.member?(activities, activity_two)
715 refute Enum.member?(activities, activity_three)
716 refute Enum.member?(activities, boost_activity)
717 assert Enum.member?(activities, activity_one)
718
719 activities = ActivityPub.fetch_activities([], %{"muting_user" => nil, "skip_preload" => true})
720
721 assert Enum.member?(activities, activity_two)
722 assert Enum.member?(activities, activity_three)
723 assert Enum.member?(activities, boost_activity)
724 assert Enum.member?(activities, activity_one)
725 end
726
727 test "doesn't return thread muted activities" do
728 user = insert(:user)
729 _activity_one = insert(:note_activity)
730 note_two = insert(:note, data: %{"context" => "suya.."})
731 activity_two = insert(:note_activity, note: note_two)
732
733 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
734
735 assert [_activity_one] = ActivityPub.fetch_activities([], %{"muting_user" => user})
736 end
737
738 test "returns thread muted activities when with_muted is set" do
739 user = insert(:user)
740 _activity_one = insert(:note_activity)
741 note_two = insert(:note, data: %{"context" => "suya.."})
742 activity_two = insert(:note_activity, note: note_two)
743
744 {:ok, _activity_two} = CommonAPI.add_mute(user, activity_two)
745
746 assert [_activity_two, _activity_one] =
747 ActivityPub.fetch_activities([], %{"muting_user" => user, "with_muted" => true})
748 end
749
750 test "does include announces on request" do
751 activity_three = insert(:note_activity)
752 user = insert(:user)
753 booster = insert(:user)
754
755 {:ok, user} = User.follow(user, booster)
756
757 {:ok, announce, _object} = CommonAPI.repeat(activity_three.id, booster)
758
759 [announce_activity] = ActivityPub.fetch_activities([user.ap_id | User.following(user)])
760
761 assert announce_activity.id == announce.id
762 end
763
764 test "excludes reblogs on request" do
765 user = insert(:user)
766 {:ok, expected_activity} = ActivityBuilder.insert(%{"type" => "Create"}, %{:user => user})
767 {:ok, _} = ActivityBuilder.insert(%{"type" => "Announce"}, %{:user => user})
768
769 [activity] = ActivityPub.fetch_user_activities(user, nil, %{"exclude_reblogs" => "true"})
770
771 assert activity == expected_activity
772 end
773
774 describe "public fetch activities" do
775 test "doesn't retrieve unlisted activities" do
776 user = insert(:user)
777
778 {:ok, _unlisted_activity} =
779 CommonAPI.post(user, %{"status" => "yeah", "visibility" => "unlisted"})
780
781 {:ok, listed_activity} = CommonAPI.post(user, %{"status" => "yeah"})
782
783 [activity] = ActivityPub.fetch_public_activities()
784
785 assert activity == listed_activity
786 end
787
788 test "retrieves public activities" do
789 _activities = ActivityPub.fetch_public_activities()
790
791 %{public: public} = ActivityBuilder.public_and_non_public()
792
793 activities = ActivityPub.fetch_public_activities()
794 assert length(activities) == 1
795 assert Enum.at(activities, 0) == public
796 end
797
798 test "retrieves a maximum of 20 activities" do
799 ActivityBuilder.insert_list(10)
800 expected_activities = ActivityBuilder.insert_list(20)
801
802 activities = ActivityPub.fetch_public_activities()
803
804 assert collect_ids(activities) == collect_ids(expected_activities)
805 assert length(activities) == 20
806 end
807
808 test "retrieves ids starting from a since_id" do
809 activities = ActivityBuilder.insert_list(30)
810 expected_activities = ActivityBuilder.insert_list(10)
811 since_id = List.last(activities).id
812
813 activities = ActivityPub.fetch_public_activities(%{"since_id" => since_id})
814
815 assert collect_ids(activities) == collect_ids(expected_activities)
816 assert length(activities) == 10
817 end
818
819 test "retrieves ids up to max_id" do
820 ActivityBuilder.insert_list(10)
821 expected_activities = ActivityBuilder.insert_list(20)
822
823 %{id: max_id} =
824 10
825 |> ActivityBuilder.insert_list()
826 |> List.first()
827
828 activities = ActivityPub.fetch_public_activities(%{"max_id" => max_id})
829
830 assert length(activities) == 20
831 assert collect_ids(activities) == collect_ids(expected_activities)
832 end
833
834 test "paginates via offset/limit" do
835 _first_part_activities = ActivityBuilder.insert_list(10)
836 second_part_activities = ActivityBuilder.insert_list(10)
837
838 later_activities = ActivityBuilder.insert_list(10)
839
840 activities =
841 ActivityPub.fetch_public_activities(%{"page" => "2", "page_size" => "20"}, :offset)
842
843 assert length(activities) == 20
844
845 assert collect_ids(activities) ==
846 collect_ids(second_part_activities) ++ collect_ids(later_activities)
847 end
848
849 test "doesn't return reblogs for users for whom reblogs have been muted" do
850 activity = insert(:note_activity)
851 user = insert(:user)
852 booster = insert(:user)
853 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
854
855 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
856
857 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
858
859 refute Enum.any?(activities, fn %{id: id} -> id == activity.id end)
860 end
861
862 test "returns reblogs for users for whom reblogs have not been muted" do
863 activity = insert(:note_activity)
864 user = insert(:user)
865 booster = insert(:user)
866 {:ok, _reblog_mute} = CommonAPI.hide_reblogs(user, booster)
867 {:ok, _reblog_mute} = CommonAPI.show_reblogs(user, booster)
868
869 {:ok, activity, _} = CommonAPI.repeat(activity.id, booster)
870
871 activities = ActivityPub.fetch_activities([], %{"muting_user" => user})
872
873 assert Enum.any?(activities, fn %{id: id} -> id == activity.id end)
874 end
875 end
876
877 describe "unreacting to an object" do
878 test_with_mock "sends an activity to federation", Federator, [:passthrough], [] do
879 Config.put([:instance, :federating], true)
880 user = insert(:user)
881 reactor = insert(:user)
882 {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
883 assert object = Object.normalize(activity)
884
885 {:ok, reaction_activity} = CommonAPI.react_with_emoji(activity.id, reactor, "🔥")
886
887 assert called(Federator.publish(reaction_activity))
888
889 {:ok, unreaction_activity, _object} =
890 ActivityPub.unreact_with_emoji(reactor, reaction_activity.data["id"])
891
892 assert called(Federator.publish(unreaction_activity))
893 end
894
895 test "adds an undo activity to the db" do
896 user = insert(:user)
897 reactor = insert(:user)
898 {:ok, activity} = CommonAPI.post(user, %{"status" => "YASSSS queen slay"})
899 assert object = Object.normalize(activity)
900
901 {:ok, reaction_activity} = CommonAPI.react_with_emoji(activity.id, reactor, "🔥")
902
903 {:ok, unreaction_activity, _object} =
904 ActivityPub.unreact_with_emoji(reactor, reaction_activity.data["id"])
905
906 assert unreaction_activity.actor == reactor.ap_id
907 assert unreaction_activity.data["object"] == reaction_activity.data["id"]
908
909 object = Object.get_by_ap_id(object.data["id"])
910 assert object.data["reaction_count"] == 0
911 assert object.data["reactions"] == []
912 end
913
914 test "reverts emoji unreact on error" do
915 [user, reactor] = insert_list(2, :user)
916 {:ok, activity} = CommonAPI.post(user, %{"status" => "Status"})
917 object = Object.normalize(activity)
918
919 {:ok, reaction_activity} = CommonAPI.react_with_emoji(activity.id, reactor, "😀")
920
921 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
922 assert {:error, :reverted} =
923 ActivityPub.unreact_with_emoji(reactor, reaction_activity.data["id"])
924 end
925
926 object = Object.get_by_ap_id(object.data["id"])
927
928 assert object.data["reaction_count"] == 1
929 assert object.data["reactions"] == [["😀", [reactor.ap_id]]]
930 end
931 end
932
933 describe "unliking" do
934 test_with_mock "sends an activity to federation", Federator, [:passthrough], [] do
935 Config.put([:instance, :federating], true)
936
937 note_activity = insert(:note_activity)
938 object = Object.normalize(note_activity)
939 user = insert(:user)
940
941 {:ok, object} = ActivityPub.unlike(user, object)
942 refute called(Federator.publish())
943
944 {:ok, _like_activity} = CommonAPI.favorite(user, note_activity.id)
945 object = Object.get_by_id(object.id)
946 assert object.data["like_count"] == 1
947
948 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
949 assert object.data["like_count"] == 0
950
951 assert called(Federator.publish(unlike_activity))
952 end
953
954 test "reverts unliking on error" do
955 note_activity = insert(:note_activity)
956 user = insert(:user)
957
958 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
959 object = Object.normalize(note_activity)
960 assert object.data["like_count"] == 1
961
962 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
963 assert {:error, :reverted} = ActivityPub.unlike(user, object)
964 end
965
966 assert Object.get_by_ap_id(object.data["id"]) == object
967 assert object.data["like_count"] == 1
968 assert Activity.get_by_id(like_activity.id)
969 end
970
971 test "unliking a previously liked object" do
972 note_activity = insert(:note_activity)
973 object = Object.normalize(note_activity)
974 user = insert(:user)
975
976 # Unliking something that hasn't been liked does nothing
977 {:ok, object} = ActivityPub.unlike(user, object)
978 assert object.data["like_count"] == 0
979
980 {:ok, like_activity} = CommonAPI.favorite(user, note_activity.id)
981
982 object = Object.get_by_id(object.id)
983 assert object.data["like_count"] == 1
984
985 {:ok, unlike_activity, _, object} = ActivityPub.unlike(user, object)
986 assert object.data["like_count"] == 0
987
988 assert Activity.get_by_id(like_activity.id) == nil
989 assert note_activity.actor in unlike_activity.recipients
990 end
991 end
992
993 describe "announcing an object" do
994 test "adds an announce activity to the db" do
995 note_activity = insert(:note_activity)
996 object = Object.normalize(note_activity)
997 user = insert(:user)
998
999 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
1000 assert object.data["announcement_count"] == 1
1001 assert object.data["announcements"] == [user.ap_id]
1002
1003 assert announce_activity.data["to"] == [
1004 User.ap_followers(user),
1005 note_activity.data["actor"]
1006 ]
1007
1008 assert announce_activity.data["object"] == object.data["id"]
1009 assert announce_activity.data["actor"] == user.ap_id
1010 assert announce_activity.data["context"] == object.data["context"]
1011 end
1012
1013 test "reverts annouce from object on error" do
1014 note_activity = insert(:note_activity)
1015 object = Object.normalize(note_activity)
1016 user = insert(:user)
1017
1018 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1019 assert {:error, :reverted} = ActivityPub.announce(user, object)
1020 end
1021
1022 reloaded_object = Object.get_by_ap_id(object.data["id"])
1023 assert reloaded_object == object
1024 refute reloaded_object.data["announcement_count"]
1025 refute reloaded_object.data["announcements"]
1026 end
1027 end
1028
1029 describe "announcing a private object" do
1030 test "adds an announce activity to the db if the audience is not widened" do
1031 user = insert(:user)
1032 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
1033 object = Object.normalize(note_activity)
1034
1035 {:ok, announce_activity, object} = ActivityPub.announce(user, object, nil, true, false)
1036
1037 assert announce_activity.data["to"] == [User.ap_followers(user)]
1038
1039 assert announce_activity.data["object"] == object.data["id"]
1040 assert announce_activity.data["actor"] == user.ap_id
1041 assert announce_activity.data["context"] == object.data["context"]
1042 end
1043
1044 test "does not add an announce activity to the db if the audience is widened" do
1045 user = insert(:user)
1046 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
1047 object = Object.normalize(note_activity)
1048
1049 assert {:error, _} = ActivityPub.announce(user, object, nil, true, true)
1050 end
1051
1052 test "does not add an announce activity to the db if the announcer is not the author" do
1053 user = insert(:user)
1054 announcer = insert(:user)
1055 {:ok, note_activity} = CommonAPI.post(user, %{"status" => ".", "visibility" => "private"})
1056 object = Object.normalize(note_activity)
1057
1058 assert {:error, _} = ActivityPub.announce(announcer, object, nil, true, false)
1059 end
1060 end
1061
1062 describe "unannouncing an object" do
1063 test "unannouncing a previously announced object" do
1064 note_activity = insert(:note_activity)
1065 object = Object.normalize(note_activity)
1066 user = insert(:user)
1067
1068 # Unannouncing an object that is not announced does nothing
1069 {:ok, object} = ActivityPub.unannounce(user, object)
1070 refute object.data["announcement_count"]
1071
1072 {:ok, announce_activity, object} = ActivityPub.announce(user, object)
1073 assert object.data["announcement_count"] == 1
1074
1075 {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
1076 assert object.data["announcement_count"] == 0
1077
1078 assert unannounce_activity.data["to"] == [
1079 User.ap_followers(user),
1080 object.data["actor"]
1081 ]
1082
1083 assert unannounce_activity.data["type"] == "Undo"
1084 assert unannounce_activity.data["object"] == announce_activity.data
1085 assert unannounce_activity.data["actor"] == user.ap_id
1086 assert unannounce_activity.data["context"] == announce_activity.data["context"]
1087
1088 assert Activity.get_by_id(announce_activity.id) == nil
1089 end
1090
1091 test "reverts unannouncing on error" do
1092 note_activity = insert(:note_activity)
1093 object = Object.normalize(note_activity)
1094 user = insert(:user)
1095
1096 {:ok, _announce_activity, object} = ActivityPub.announce(user, object)
1097 assert object.data["announcement_count"] == 1
1098
1099 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1100 assert {:error, :reverted} = ActivityPub.unannounce(user, object)
1101 end
1102
1103 object = Object.get_by_ap_id(object.data["id"])
1104 assert object.data["announcement_count"] == 1
1105 end
1106 end
1107
1108 describe "uploading files" do
1109 test "copies the file to the configured folder" do
1110 file = %Plug.Upload{
1111 content_type: "image/jpg",
1112 path: Path.absname("test/fixtures/image.jpg"),
1113 filename: "an_image.jpg"
1114 }
1115
1116 {:ok, %Object{} = object} = ActivityPub.upload(file)
1117 assert object.data["name"] == "an_image.jpg"
1118 end
1119
1120 test "works with base64 encoded images" do
1121 file = %{
1122 "img" => data_uri()
1123 }
1124
1125 {:ok, %Object{}} = ActivityPub.upload(file)
1126 end
1127 end
1128
1129 describe "fetch the latest Follow" do
1130 test "fetches the latest Follow activity" do
1131 %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
1132 follower = Repo.get_by(User, ap_id: activity.data["actor"])
1133 followed = Repo.get_by(User, ap_id: activity.data["object"])
1134
1135 assert activity == Utils.fetch_latest_follow(follower, followed)
1136 end
1137 end
1138
1139 describe "following / unfollowing" do
1140 test "it reverts follow activity" do
1141 follower = insert(:user)
1142 followed = insert(:user)
1143
1144 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1145 assert {:error, :reverted} = ActivityPub.follow(follower, followed)
1146 end
1147
1148 assert Repo.aggregate(Activity, :count, :id) == 0
1149 assert Repo.aggregate(Object, :count, :id) == 0
1150 end
1151
1152 test "it reverts unfollow activity" do
1153 follower = insert(:user)
1154 followed = insert(:user)
1155
1156 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1157
1158 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1159 assert {:error, :reverted} = ActivityPub.unfollow(follower, followed)
1160 end
1161
1162 activity = Activity.get_by_id(follow_activity.id)
1163 assert activity.data["type"] == "Follow"
1164 assert activity.data["actor"] == follower.ap_id
1165
1166 assert activity.data["object"] == followed.ap_id
1167 end
1168
1169 test "creates a follow activity" do
1170 follower = insert(:user)
1171 followed = insert(:user)
1172
1173 {:ok, activity} = ActivityPub.follow(follower, followed)
1174 assert activity.data["type"] == "Follow"
1175 assert activity.data["actor"] == follower.ap_id
1176 assert activity.data["object"] == followed.ap_id
1177 end
1178
1179 test "creates an undo activity for the last follow" do
1180 follower = insert(:user)
1181 followed = insert(:user)
1182
1183 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1184 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1185
1186 assert activity.data["type"] == "Undo"
1187 assert activity.data["actor"] == follower.ap_id
1188
1189 embedded_object = activity.data["object"]
1190 assert is_map(embedded_object)
1191 assert embedded_object["type"] == "Follow"
1192 assert embedded_object["object"] == followed.ap_id
1193 assert embedded_object["id"] == follow_activity.data["id"]
1194 end
1195
1196 test "creates an undo activity for a pending follow request" do
1197 follower = insert(:user)
1198 followed = insert(:user, %{locked: true})
1199
1200 {:ok, follow_activity} = ActivityPub.follow(follower, followed)
1201 {:ok, activity} = ActivityPub.unfollow(follower, followed)
1202
1203 assert activity.data["type"] == "Undo"
1204 assert activity.data["actor"] == follower.ap_id
1205
1206 embedded_object = activity.data["object"]
1207 assert is_map(embedded_object)
1208 assert embedded_object["type"] == "Follow"
1209 assert embedded_object["object"] == followed.ap_id
1210 assert embedded_object["id"] == follow_activity.data["id"]
1211 end
1212 end
1213
1214 describe "blocking / unblocking" do
1215 test "reverts block activity on error" do
1216 [blocker, blocked] = insert_list(2, :user)
1217
1218 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1219 assert {:error, :reverted} = ActivityPub.block(blocker, blocked)
1220 end
1221
1222 assert Repo.aggregate(Activity, :count, :id) == 0
1223 assert Repo.aggregate(Object, :count, :id) == 0
1224 end
1225
1226 test "creates a block activity" do
1227 blocker = insert(:user)
1228 blocked = insert(:user)
1229
1230 {:ok, activity} = ActivityPub.block(blocker, blocked)
1231
1232 assert activity.data["type"] == "Block"
1233 assert activity.data["actor"] == blocker.ap_id
1234 assert activity.data["object"] == blocked.ap_id
1235 end
1236
1237 test "reverts unblock activity on error" do
1238 [blocker, blocked] = insert_list(2, :user)
1239 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
1240
1241 with_mock(Utils, [:passthrough], maybe_federate: fn _ -> {:error, :reverted} end) do
1242 assert {:error, :reverted} = ActivityPub.unblock(blocker, blocked)
1243 end
1244
1245 assert block_activity.data["type"] == "Block"
1246 assert block_activity.data["actor"] == blocker.ap_id
1247
1248 assert Repo.aggregate(Activity, :count, :id) == 1
1249 assert Repo.aggregate(Object, :count, :id) == 1
1250 end
1251
1252 test "creates an undo activity for the last block" do
1253 blocker = insert(:user)
1254 blocked = insert(:user)
1255
1256 {:ok, block_activity} = ActivityPub.block(blocker, blocked)
1257 {:ok, activity} = ActivityPub.unblock(blocker, blocked)
1258
1259 assert activity.data["type"] == "Undo"
1260 assert activity.data["actor"] == blocker.ap_id
1261
1262 embedded_object = activity.data["object"]
1263 assert is_map(embedded_object)
1264 assert embedded_object["type"] == "Block"
1265 assert embedded_object["object"] == blocked.ap_id
1266 assert embedded_object["id"] == block_activity.data["id"]
1267 end
1268 end
1269
1270 describe "timeline post-processing" do
1271 test "it filters broken threads" do
1272 user1 = insert(:user)
1273 user2 = insert(:user)
1274 user3 = insert(:user)
1275
1276 {:ok, user1} = User.follow(user1, user3)
1277 assert User.following?(user1, user3)
1278
1279 {:ok, user2} = User.follow(user2, user3)
1280 assert User.following?(user2, user3)
1281
1282 {:ok, user3} = User.follow(user3, user2)
1283 assert User.following?(user3, user2)
1284
1285 {:ok, public_activity} = CommonAPI.post(user3, %{"status" => "hi 1"})
1286
1287 {:ok, private_activity_1} =
1288 CommonAPI.post(user3, %{"status" => "hi 2", "visibility" => "private"})
1289
1290 {:ok, private_activity_2} =
1291 CommonAPI.post(user2, %{
1292 "status" => "hi 3",
1293 "visibility" => "private",
1294 "in_reply_to_status_id" => private_activity_1.id
1295 })
1296
1297 {:ok, private_activity_3} =
1298 CommonAPI.post(user3, %{
1299 "status" => "hi 4",
1300 "visibility" => "private",
1301 "in_reply_to_status_id" => private_activity_2.id
1302 })
1303
1304 activities =
1305 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)])
1306 |> Enum.map(fn a -> a.id end)
1307
1308 private_activity_1 = Activity.get_by_ap_id_with_object(private_activity_1.data["id"])
1309
1310 assert [public_activity.id, private_activity_1.id, private_activity_3.id] == activities
1311
1312 assert length(activities) == 3
1313
1314 activities =
1315 ActivityPub.fetch_activities([user1.ap_id | User.following(user1)], %{"user" => user1})
1316 |> Enum.map(fn a -> a.id end)
1317
1318 assert [public_activity.id, private_activity_1.id] == activities
1319 assert length(activities) == 2
1320 end
1321 end
1322
1323 describe "update" do
1324 setup do: clear_config([:instance, :max_pinned_statuses])
1325
1326 test "it creates an update activity with the new user data" do
1327 user = insert(:user)
1328 {:ok, user} = User.ensure_keys_present(user)
1329 user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
1330
1331 {:ok, update} =
1332 ActivityPub.update(%{
1333 actor: user_data["id"],
1334 to: [user.follower_address],
1335 cc: [],
1336 object: user_data
1337 })
1338
1339 assert update.data["actor"] == user.ap_id
1340 assert update.data["to"] == [user.follower_address]
1341 assert embedded_object = update.data["object"]
1342 assert embedded_object["id"] == user_data["id"]
1343 assert embedded_object["type"] == user_data["type"]
1344 end
1345 end
1346
1347 test "returned pinned statuses" do
1348 Config.put([:instance, :max_pinned_statuses], 3)
1349 user = insert(:user)
1350
1351 {:ok, activity_one} = CommonAPI.post(user, %{"status" => "HI!!!"})
1352 {:ok, activity_two} = CommonAPI.post(user, %{"status" => "HI!!!"})
1353 {:ok, activity_three} = CommonAPI.post(user, %{"status" => "HI!!!"})
1354
1355 CommonAPI.pin(activity_one.id, user)
1356 user = refresh_record(user)
1357
1358 CommonAPI.pin(activity_two.id, user)
1359 user = refresh_record(user)
1360
1361 CommonAPI.pin(activity_three.id, user)
1362 user = refresh_record(user)
1363
1364 activities = ActivityPub.fetch_user_activities(user, nil, %{"pinned" => "true"})
1365
1366 assert 3 = length(activities)
1367 end
1368
1369 describe "flag/1" do
1370 setup do
1371 reporter = insert(:user)
1372 target_account = insert(:user)
1373 content = "foobar"
1374 {:ok, activity} = CommonAPI.post(target_account, %{"status" => content})
1375 context = Utils.generate_context_id()
1376
1377 reporter_ap_id = reporter.ap_id
1378 target_ap_id = target_account.ap_id
1379 activity_ap_id = activity.data["id"]
1380
1381 activity_with_object = Activity.get_by_ap_id_with_object(activity_ap_id)
1382
1383 {:ok,
1384 %{
1385 reporter: reporter,
1386 context: context,
1387 target_account: target_account,
1388 reported_activity: activity,
1389 content: content,
1390 activity_ap_id: activity_ap_id,
1391 activity_with_object: activity_with_object,
1392 reporter_ap_id: reporter_ap_id,
1393 target_ap_id: target_ap_id
1394 }}
1395 end
1396
1397 test "it can create a Flag activity",
1398 %{
1399 reporter: reporter,
1400 context: context,
1401 target_account: target_account,
1402 reported_activity: reported_activity,
1403 content: content,
1404 activity_ap_id: activity_ap_id,
1405 activity_with_object: activity_with_object,
1406 reporter_ap_id: reporter_ap_id,
1407 target_ap_id: target_ap_id
1408 } do
1409 assert {:ok, activity} =
1410 ActivityPub.flag(%{
1411 actor: reporter,
1412 context: context,
1413 account: target_account,
1414 statuses: [reported_activity],
1415 content: content
1416 })
1417
1418 note_obj = %{
1419 "type" => "Note",
1420 "id" => activity_ap_id,
1421 "content" => content,
1422 "published" => activity_with_object.object.data["published"],
1423 "actor" => AccountView.render("show.json", %{user: target_account})
1424 }
1425
1426 assert %Activity{
1427 actor: ^reporter_ap_id,
1428 data: %{
1429 "type" => "Flag",
1430 "content" => ^content,
1431 "context" => ^context,
1432 "object" => [^target_ap_id, ^note_obj]
1433 }
1434 } = activity
1435 end
1436
1437 test_with_mock "strips status data from Flag, before federating it",
1438 %{
1439 reporter: reporter,
1440 context: context,
1441 target_account: target_account,
1442 reported_activity: reported_activity,
1443 content: content
1444 },
1445 Utils,
1446 [:passthrough],
1447 [] do
1448 {:ok, activity} =
1449 ActivityPub.flag(%{
1450 actor: reporter,
1451 context: context,
1452 account: target_account,
1453 statuses: [reported_activity],
1454 content: content
1455 })
1456
1457 new_data =
1458 put_in(activity.data, ["object"], [target_account.ap_id, reported_activity.data["id"]])
1459
1460 assert_called(Utils.maybe_federate(%{activity | data: new_data}))
1461 end
1462 end
1463
1464 test "fetch_activities/2 returns activities addressed to a list " do
1465 user = insert(:user)
1466 member = insert(:user)
1467 {:ok, list} = Pleroma.List.create("foo", user)
1468 {:ok, list} = Pleroma.List.follow(list, member)
1469
1470 {:ok, activity} =
1471 CommonAPI.post(user, %{"status" => "foobar", "visibility" => "list:#{list.id}"})
1472
1473 activity = Repo.preload(activity, :bookmark)
1474 activity = %Activity{activity | thread_muted?: !!activity.thread_muted?}
1475
1476 assert ActivityPub.fetch_activities([], %{"user" => user}) == [activity]
1477 end
1478
1479 def data_uri do
1480 File.read!("test/fixtures/avatar_data_uri")
1481 end
1482
1483 describe "fetch_activities_bounded" do
1484 test "fetches private posts for followed users" do
1485 user = insert(:user)
1486
1487 {:ok, activity} =
1488 CommonAPI.post(user, %{
1489 "status" => "thought I looked cute might delete later :3",
1490 "visibility" => "private"
1491 })
1492
1493 [result] = ActivityPub.fetch_activities_bounded([user.follower_address], [])
1494 assert result.id == activity.id
1495 end
1496
1497 test "fetches only public posts for other users" do
1498 user = insert(:user)
1499 {:ok, activity} = CommonAPI.post(user, %{"status" => "#cofe", "visibility" => "public"})
1500
1501 {:ok, _private_activity} =
1502 CommonAPI.post(user, %{
1503 "status" => "why is tenshi eating a corndog so cute?",
1504 "visibility" => "private"
1505 })
1506
1507 [result] = ActivityPub.fetch_activities_bounded([], [user.follower_address])
1508 assert result.id == activity.id
1509 end
1510 end
1511
1512 describe "fetch_follow_information_for_user" do
1513 test "syncronizes following/followers counters" do
1514 user =
1515 insert(:user,
1516 local: false,
1517 follower_address: "http://localhost:4001/users/fuser2/followers",
1518 following_address: "http://localhost:4001/users/fuser2/following"
1519 )
1520
1521 {:ok, info} = ActivityPub.fetch_follow_information_for_user(user)
1522 assert info.follower_count == 527
1523 assert info.following_count == 267
1524 end
1525
1526 test "detects hidden followers" do
1527 mock(fn env ->
1528 case env.url do
1529 "http://localhost:4001/users/masto_closed/followers?page=1" ->
1530 %Tesla.Env{status: 403, body: ""}
1531
1532 _ ->
1533 apply(HttpRequestMock, :request, [env])
1534 end
1535 end)
1536
1537 user =
1538 insert(:user,
1539 local: false,
1540 follower_address: "http://localhost:4001/users/masto_closed/followers",
1541 following_address: "http://localhost:4001/users/masto_closed/following"
1542 )
1543
1544 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1545 assert follow_info.hide_followers == true
1546 assert follow_info.hide_follows == false
1547 end
1548
1549 test "detects hidden follows" do
1550 mock(fn env ->
1551 case env.url do
1552 "http://localhost:4001/users/masto_closed/following?page=1" ->
1553 %Tesla.Env{status: 403, body: ""}
1554
1555 _ ->
1556 apply(HttpRequestMock, :request, [env])
1557 end
1558 end)
1559
1560 user =
1561 insert(:user,
1562 local: false,
1563 follower_address: "http://localhost:4001/users/masto_closed/followers",
1564 following_address: "http://localhost:4001/users/masto_closed/following"
1565 )
1566
1567 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1568 assert follow_info.hide_followers == false
1569 assert follow_info.hide_follows == true
1570 end
1571
1572 test "detects hidden follows/followers for friendica" do
1573 user =
1574 insert(:user,
1575 local: false,
1576 follower_address: "http://localhost:8080/followers/fuser3",
1577 following_address: "http://localhost:8080/following/fuser3"
1578 )
1579
1580 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1581 assert follow_info.hide_followers == true
1582 assert follow_info.follower_count == 296
1583 assert follow_info.following_count == 32
1584 assert follow_info.hide_follows == true
1585 end
1586
1587 test "doesn't crash when follower and following counters are hidden" do
1588 mock(fn env ->
1589 case env.url do
1590 "http://localhost:4001/users/masto_hidden_counters/following" ->
1591 json(%{
1592 "@context" => "https://www.w3.org/ns/activitystreams",
1593 "id" => "http://localhost:4001/users/masto_hidden_counters/followers"
1594 })
1595
1596 "http://localhost:4001/users/masto_hidden_counters/following?page=1" ->
1597 %Tesla.Env{status: 403, body: ""}
1598
1599 "http://localhost:4001/users/masto_hidden_counters/followers" ->
1600 json(%{
1601 "@context" => "https://www.w3.org/ns/activitystreams",
1602 "id" => "http://localhost:4001/users/masto_hidden_counters/following"
1603 })
1604
1605 "http://localhost:4001/users/masto_hidden_counters/followers?page=1" ->
1606 %Tesla.Env{status: 403, body: ""}
1607 end
1608 end)
1609
1610 user =
1611 insert(:user,
1612 local: false,
1613 follower_address: "http://localhost:4001/users/masto_hidden_counters/followers",
1614 following_address: "http://localhost:4001/users/masto_hidden_counters/following"
1615 )
1616
1617 {:ok, follow_info} = ActivityPub.fetch_follow_information_for_user(user)
1618
1619 assert follow_info.hide_followers == true
1620 assert follow_info.follower_count == 0
1621 assert follow_info.hide_follows == true
1622 assert follow_info.following_count == 0
1623 end
1624 end
1625
1626 describe "fetch_favourites/3" do
1627 test "returns a favourite activities sorted by adds to favorite" do
1628 user = insert(:user)
1629 other_user = insert(:user)
1630 user1 = insert(:user)
1631 user2 = insert(:user)
1632 {:ok, a1} = CommonAPI.post(user1, %{"status" => "bla"})
1633 {:ok, _a2} = CommonAPI.post(user2, %{"status" => "traps are happy"})
1634 {:ok, a3} = CommonAPI.post(user2, %{"status" => "Trees Are "})
1635 {:ok, a4} = CommonAPI.post(user2, %{"status" => "Agent Smith "})
1636 {:ok, a5} = CommonAPI.post(user1, %{"status" => "Red or Blue "})
1637
1638 {:ok, _} = CommonAPI.favorite(user, a4.id)
1639 {:ok, _} = CommonAPI.favorite(other_user, a3.id)
1640 {:ok, _} = CommonAPI.favorite(user, a3.id)
1641 {:ok, _} = CommonAPI.favorite(other_user, a5.id)
1642 {:ok, _} = CommonAPI.favorite(user, a5.id)
1643 {:ok, _} = CommonAPI.favorite(other_user, a4.id)
1644 {:ok, _} = CommonAPI.favorite(user, a1.id)
1645 {:ok, _} = CommonAPI.favorite(other_user, a1.id)
1646 result = ActivityPub.fetch_favourites(user)
1647
1648 assert Enum.map(result, & &1.id) == [a1.id, a5.id, a3.id, a4.id]
1649
1650 result = ActivityPub.fetch_favourites(user, %{"limit" => 2})
1651 assert Enum.map(result, & &1.id) == [a1.id, a5.id]
1652 end
1653 end
1654
1655 describe "Move activity" do
1656 test "create" do
1657 %{ap_id: old_ap_id} = old_user = insert(:user)
1658 %{ap_id: new_ap_id} = new_user = insert(:user, also_known_as: [old_ap_id])
1659 follower = insert(:user)
1660 follower_move_opted_out = insert(:user, allow_following_move: false)
1661
1662 User.follow(follower, old_user)
1663 User.follow(follower_move_opted_out, old_user)
1664
1665 assert User.following?(follower, old_user)
1666 assert User.following?(follower_move_opted_out, old_user)
1667
1668 assert {:ok, activity} = ActivityPub.move(old_user, new_user)
1669
1670 assert %Activity{
1671 actor: ^old_ap_id,
1672 data: %{
1673 "actor" => ^old_ap_id,
1674 "object" => ^old_ap_id,
1675 "target" => ^new_ap_id,
1676 "type" => "Move"
1677 },
1678 local: true
1679 } = activity
1680
1681 params = %{
1682 "op" => "move_following",
1683 "origin_id" => old_user.id,
1684 "target_id" => new_user.id
1685 }
1686
1687 assert_enqueued(worker: Pleroma.Workers.BackgroundWorker, args: params)
1688
1689 Pleroma.Workers.BackgroundWorker.perform(params, nil)
1690
1691 refute User.following?(follower, old_user)
1692 assert User.following?(follower, new_user)
1693
1694 assert User.following?(follower_move_opted_out, old_user)
1695 refute User.following?(follower_move_opted_out, new_user)
1696
1697 activity = %Activity{activity | object: nil}
1698
1699 assert [%Notification{activity: ^activity}] = Notification.for_user(follower)
1700
1701 assert [%Notification{activity: ^activity}] = Notification.for_user(follower_move_opted_out)
1702 end
1703
1704 test "old user must be in the new user's `also_known_as` list" do
1705 old_user = insert(:user)
1706 new_user = insert(:user)
1707
1708 assert {:error, "Target account must have the origin in `alsoKnownAs`"} =
1709 ActivityPub.move(old_user, new_user)
1710 end
1711 end
1712
1713 test "doesn't retrieve replies activities with exclude_replies" do
1714 user = insert(:user)
1715
1716 {:ok, activity} = CommonAPI.post(user, %{"status" => "yeah"})
1717
1718 {:ok, _reply} =
1719 CommonAPI.post(user, %{"status" => "yeah", "in_reply_to_status_id" => activity.id})
1720
1721 [result] = ActivityPub.fetch_public_activities(%{"exclude_replies" => "true"})
1722
1723 assert result.id == activity.id
1724
1725 assert length(ActivityPub.fetch_public_activities()) == 2
1726 end
1727
1728 describe "replies filtering with public messages" do
1729 setup :public_messages
1730
1731 test "public timeline", %{users: %{u1: user}} do
1732 activities_ids =
1733 %{}
1734 |> Map.put("type", ["Create", "Announce"])
1735 |> Map.put("local_only", false)
1736 |> Map.put("blocking_user", user)
1737 |> Map.put("muting_user", user)
1738 |> Map.put("reply_filtering_user", user)
1739 |> ActivityPub.fetch_public_activities()
1740 |> Enum.map(& &1.id)
1741
1742 assert length(activities_ids) == 16
1743 end
1744
1745 test "public timeline with reply_visibility `following`", %{
1746 users: %{u1: user},
1747 u1: u1,
1748 u2: u2,
1749 u3: u3,
1750 u4: u4,
1751 activities: activities
1752 } do
1753 activities_ids =
1754 %{}
1755 |> Map.put("type", ["Create", "Announce"])
1756 |> Map.put("local_only", false)
1757 |> Map.put("blocking_user", user)
1758 |> Map.put("muting_user", user)
1759 |> Map.put("reply_visibility", "following")
1760 |> Map.put("reply_filtering_user", user)
1761 |> ActivityPub.fetch_public_activities()
1762 |> Enum.map(& &1.id)
1763
1764 assert length(activities_ids) == 14
1765
1766 visible_ids =
1767 Map.values(u1) ++ Map.values(u2) ++ Map.values(u4) ++ Map.values(activities) ++ [u3[:r1]]
1768
1769 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1770 end
1771
1772 test "public timeline with reply_visibility `self`", %{
1773 users: %{u1: user},
1774 u1: u1,
1775 u2: u2,
1776 u3: u3,
1777 u4: u4,
1778 activities: activities
1779 } do
1780 activities_ids =
1781 %{}
1782 |> Map.put("type", ["Create", "Announce"])
1783 |> Map.put("local_only", false)
1784 |> Map.put("blocking_user", user)
1785 |> Map.put("muting_user", user)
1786 |> Map.put("reply_visibility", "self")
1787 |> Map.put("reply_filtering_user", user)
1788 |> ActivityPub.fetch_public_activities()
1789 |> Enum.map(& &1.id)
1790
1791 assert length(activities_ids) == 10
1792 visible_ids = Map.values(u1) ++ [u2[:r1], u3[:r1], u4[:r1]] ++ Map.values(activities)
1793 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1794 end
1795
1796 test "home timeline", %{
1797 users: %{u1: user},
1798 activities: activities,
1799 u1: u1,
1800 u2: u2,
1801 u3: u3,
1802 u4: u4
1803 } do
1804 params =
1805 %{}
1806 |> Map.put("type", ["Create", "Announce"])
1807 |> Map.put("blocking_user", user)
1808 |> Map.put("muting_user", user)
1809 |> Map.put("user", user)
1810 |> Map.put("reply_filtering_user", user)
1811
1812 activities_ids =
1813 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1814 |> Enum.map(& &1.id)
1815
1816 assert length(activities_ids) == 13
1817
1818 visible_ids =
1819 Map.values(u1) ++
1820 Map.values(u3) ++
1821 [
1822 activities[:a1],
1823 activities[:a2],
1824 activities[:a4],
1825 u2[:r1],
1826 u2[:r3],
1827 u4[:r1],
1828 u4[:r2]
1829 ]
1830
1831 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1832 end
1833
1834 test "home timeline with reply_visibility `following`", %{
1835 users: %{u1: user},
1836 activities: activities,
1837 u1: u1,
1838 u2: u2,
1839 u3: u3,
1840 u4: u4
1841 } do
1842 params =
1843 %{}
1844 |> Map.put("type", ["Create", "Announce"])
1845 |> Map.put("blocking_user", user)
1846 |> Map.put("muting_user", user)
1847 |> Map.put("user", user)
1848 |> Map.put("reply_visibility", "following")
1849 |> Map.put("reply_filtering_user", user)
1850
1851 activities_ids =
1852 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1853 |> Enum.map(& &1.id)
1854
1855 assert length(activities_ids) == 11
1856
1857 visible_ids =
1858 Map.values(u1) ++
1859 [
1860 activities[:a1],
1861 activities[:a2],
1862 activities[:a4],
1863 u2[:r1],
1864 u2[:r3],
1865 u3[:r1],
1866 u4[:r1],
1867 u4[:r2]
1868 ]
1869
1870 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1871 end
1872
1873 test "home timeline with reply_visibility `self`", %{
1874 users: %{u1: user},
1875 activities: activities,
1876 u1: u1,
1877 u2: u2,
1878 u3: u3,
1879 u4: u4
1880 } do
1881 params =
1882 %{}
1883 |> Map.put("type", ["Create", "Announce"])
1884 |> Map.put("blocking_user", user)
1885 |> Map.put("muting_user", user)
1886 |> Map.put("user", user)
1887 |> Map.put("reply_visibility", "self")
1888 |> Map.put("reply_filtering_user", user)
1889
1890 activities_ids =
1891 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1892 |> Enum.map(& &1.id)
1893
1894 assert length(activities_ids) == 9
1895
1896 visible_ids =
1897 Map.values(u1) ++
1898 [
1899 activities[:a1],
1900 activities[:a2],
1901 activities[:a4],
1902 u2[:r1],
1903 u3[:r1],
1904 u4[:r1]
1905 ]
1906
1907 assert Enum.all?(visible_ids, &(&1 in activities_ids))
1908 end
1909 end
1910
1911 describe "replies filtering with private messages" do
1912 setup :private_messages
1913
1914 test "public timeline", %{users: %{u1: user}} do
1915 activities_ids =
1916 %{}
1917 |> Map.put("type", ["Create", "Announce"])
1918 |> Map.put("local_only", false)
1919 |> Map.put("blocking_user", user)
1920 |> Map.put("muting_user", user)
1921 |> Map.put("user", user)
1922 |> ActivityPub.fetch_public_activities()
1923 |> Enum.map(& &1.id)
1924
1925 assert activities_ids == []
1926 end
1927
1928 test "public timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1929 activities_ids =
1930 %{}
1931 |> Map.put("type", ["Create", "Announce"])
1932 |> Map.put("local_only", false)
1933 |> Map.put("blocking_user", user)
1934 |> Map.put("muting_user", user)
1935 |> Map.put("reply_visibility", "following")
1936 |> Map.put("reply_filtering_user", user)
1937 |> Map.put("user", user)
1938 |> ActivityPub.fetch_public_activities()
1939 |> Enum.map(& &1.id)
1940
1941 assert activities_ids == []
1942 end
1943
1944 test "public timeline with default reply_visibility `self`", %{users: %{u1: user}} do
1945 activities_ids =
1946 %{}
1947 |> Map.put("type", ["Create", "Announce"])
1948 |> Map.put("local_only", false)
1949 |> Map.put("blocking_user", user)
1950 |> Map.put("muting_user", user)
1951 |> Map.put("reply_visibility", "self")
1952 |> Map.put("reply_filtering_user", user)
1953 |> Map.put("user", user)
1954 |> ActivityPub.fetch_public_activities()
1955 |> Enum.map(& &1.id)
1956
1957 assert activities_ids == []
1958 end
1959
1960 test "home timeline", %{users: %{u1: user}} do
1961 params =
1962 %{}
1963 |> Map.put("type", ["Create", "Announce"])
1964 |> Map.put("blocking_user", user)
1965 |> Map.put("muting_user", user)
1966 |> Map.put("user", user)
1967
1968 activities_ids =
1969 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1970 |> Enum.map(& &1.id)
1971
1972 assert length(activities_ids) == 12
1973 end
1974
1975 test "home timeline with default reply_visibility `following`", %{users: %{u1: user}} do
1976 params =
1977 %{}
1978 |> Map.put("type", ["Create", "Announce"])
1979 |> Map.put("blocking_user", user)
1980 |> Map.put("muting_user", user)
1981 |> Map.put("user", user)
1982 |> Map.put("reply_visibility", "following")
1983 |> Map.put("reply_filtering_user", user)
1984
1985 activities_ids =
1986 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
1987 |> Enum.map(& &1.id)
1988
1989 assert length(activities_ids) == 12
1990 end
1991
1992 test "home timeline with default reply_visibility `self`", %{
1993 users: %{u1: user},
1994 activities: activities,
1995 u1: u1,
1996 u2: u2,
1997 u3: u3,
1998 u4: u4
1999 } do
2000 params =
2001 %{}
2002 |> Map.put("type", ["Create", "Announce"])
2003 |> Map.put("blocking_user", user)
2004 |> Map.put("muting_user", user)
2005 |> Map.put("user", user)
2006 |> Map.put("reply_visibility", "self")
2007 |> Map.put("reply_filtering_user", user)
2008
2009 activities_ids =
2010 ActivityPub.fetch_activities([user.ap_id | User.following(user)], params)
2011 |> Enum.map(& &1.id)
2012
2013 assert length(activities_ids) == 10
2014
2015 visible_ids =
2016 Map.values(u1) ++ Map.values(u4) ++ [u2[:r1], u3[:r1]] ++ Map.values(activities)
2017
2018 assert Enum.all?(visible_ids, &(&1 in activities_ids))
2019 end
2020 end
2021
2022 defp public_messages(_) do
2023 [u1, u2, u3, u4] = insert_list(4, :user)
2024 {:ok, u1} = User.follow(u1, u2)
2025 {:ok, u2} = User.follow(u2, u1)
2026 {:ok, u1} = User.follow(u1, u4)
2027 {:ok, u4} = User.follow(u4, u1)
2028
2029 {:ok, u2} = User.follow(u2, u3)
2030 {:ok, u3} = User.follow(u3, u2)
2031
2032 {:ok, a1} = CommonAPI.post(u1, %{"status" => "Status"})
2033
2034 {:ok, r1_1} =
2035 CommonAPI.post(u2, %{
2036 "status" => "@#{u1.nickname} reply from u2 to u1",
2037 "in_reply_to_status_id" => a1.id
2038 })
2039
2040 {:ok, r1_2} =
2041 CommonAPI.post(u3, %{
2042 "status" => "@#{u1.nickname} reply from u3 to u1",
2043 "in_reply_to_status_id" => a1.id
2044 })
2045
2046 {:ok, r1_3} =
2047 CommonAPI.post(u4, %{
2048 "status" => "@#{u1.nickname} reply from u4 to u1",
2049 "in_reply_to_status_id" => a1.id
2050 })
2051
2052 {:ok, a2} = CommonAPI.post(u2, %{"status" => "Status"})
2053
2054 {:ok, r2_1} =
2055 CommonAPI.post(u1, %{
2056 "status" => "@#{u2.nickname} reply from u1 to u2",
2057 "in_reply_to_status_id" => a2.id
2058 })
2059
2060 {:ok, r2_2} =
2061 CommonAPI.post(u3, %{
2062 "status" => "@#{u2.nickname} reply from u3 to u2",
2063 "in_reply_to_status_id" => a2.id
2064 })
2065
2066 {:ok, r2_3} =
2067 CommonAPI.post(u4, %{
2068 "status" => "@#{u2.nickname} reply from u4 to u2",
2069 "in_reply_to_status_id" => a2.id
2070 })
2071
2072 {:ok, a3} = CommonAPI.post(u3, %{"status" => "Status"})
2073
2074 {:ok, r3_1} =
2075 CommonAPI.post(u1, %{
2076 "status" => "@#{u3.nickname} reply from u1 to u3",
2077 "in_reply_to_status_id" => a3.id
2078 })
2079
2080 {:ok, r3_2} =
2081 CommonAPI.post(u2, %{
2082 "status" => "@#{u3.nickname} reply from u2 to u3",
2083 "in_reply_to_status_id" => a3.id
2084 })
2085
2086 {:ok, r3_3} =
2087 CommonAPI.post(u4, %{
2088 "status" => "@#{u3.nickname} reply from u4 to u3",
2089 "in_reply_to_status_id" => a3.id
2090 })
2091
2092 {:ok, a4} = CommonAPI.post(u4, %{"status" => "Status"})
2093
2094 {:ok, r4_1} =
2095 CommonAPI.post(u1, %{
2096 "status" => "@#{u4.nickname} reply from u1 to u4",
2097 "in_reply_to_status_id" => a4.id
2098 })
2099
2100 {:ok, r4_2} =
2101 CommonAPI.post(u2, %{
2102 "status" => "@#{u4.nickname} reply from u2 to u4",
2103 "in_reply_to_status_id" => a4.id
2104 })
2105
2106 {:ok, r4_3} =
2107 CommonAPI.post(u3, %{
2108 "status" => "@#{u4.nickname} reply from u3 to u4",
2109 "in_reply_to_status_id" => a4.id
2110 })
2111
2112 {:ok,
2113 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2114 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2115 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2116 u2: %{r1: r2_1.id, r2: r2_2.id, r3: r2_3.id},
2117 u3: %{r1: r3_1.id, r2: r3_2.id, r3: r3_3.id},
2118 u4: %{r1: r4_1.id, r2: r4_2.id, r3: r4_3.id}}
2119 end
2120
2121 defp private_messages(_) do
2122 [u1, u2, u3, u4] = insert_list(4, :user)
2123 {:ok, u1} = User.follow(u1, u2)
2124 {:ok, u2} = User.follow(u2, u1)
2125 {:ok, u1} = User.follow(u1, u3)
2126 {:ok, u3} = User.follow(u3, u1)
2127 {:ok, u1} = User.follow(u1, u4)
2128 {:ok, u4} = User.follow(u4, u1)
2129
2130 {:ok, u2} = User.follow(u2, u3)
2131 {:ok, u3} = User.follow(u3, u2)
2132
2133 {:ok, a1} = CommonAPI.post(u1, %{"status" => "Status", "visibility" => "private"})
2134
2135 {:ok, r1_1} =
2136 CommonAPI.post(u2, %{
2137 "status" => "@#{u1.nickname} reply from u2 to u1",
2138 "in_reply_to_status_id" => a1.id,
2139 "visibility" => "private"
2140 })
2141
2142 {:ok, r1_2} =
2143 CommonAPI.post(u3, %{
2144 "status" => "@#{u1.nickname} reply from u3 to u1",
2145 "in_reply_to_status_id" => a1.id,
2146 "visibility" => "private"
2147 })
2148
2149 {:ok, r1_3} =
2150 CommonAPI.post(u4, %{
2151 "status" => "@#{u1.nickname} reply from u4 to u1",
2152 "in_reply_to_status_id" => a1.id,
2153 "visibility" => "private"
2154 })
2155
2156 {:ok, a2} = CommonAPI.post(u2, %{"status" => "Status", "visibility" => "private"})
2157
2158 {:ok, r2_1} =
2159 CommonAPI.post(u1, %{
2160 "status" => "@#{u2.nickname} reply from u1 to u2",
2161 "in_reply_to_status_id" => a2.id,
2162 "visibility" => "private"
2163 })
2164
2165 {:ok, r2_2} =
2166 CommonAPI.post(u3, %{
2167 "status" => "@#{u2.nickname} reply from u3 to u2",
2168 "in_reply_to_status_id" => a2.id,
2169 "visibility" => "private"
2170 })
2171
2172 {:ok, a3} = CommonAPI.post(u3, %{"status" => "Status", "visibility" => "private"})
2173
2174 {:ok, r3_1} =
2175 CommonAPI.post(u1, %{
2176 "status" => "@#{u3.nickname} reply from u1 to u3",
2177 "in_reply_to_status_id" => a3.id,
2178 "visibility" => "private"
2179 })
2180
2181 {:ok, r3_2} =
2182 CommonAPI.post(u2, %{
2183 "status" => "@#{u3.nickname} reply from u2 to u3",
2184 "in_reply_to_status_id" => a3.id,
2185 "visibility" => "private"
2186 })
2187
2188 {:ok, a4} = CommonAPI.post(u4, %{"status" => "Status", "visibility" => "private"})
2189
2190 {:ok, r4_1} =
2191 CommonAPI.post(u1, %{
2192 "status" => "@#{u4.nickname} reply from u1 to u4",
2193 "in_reply_to_status_id" => a4.id,
2194 "visibility" => "private"
2195 })
2196
2197 {:ok,
2198 users: %{u1: u1, u2: u2, u3: u3, u4: u4},
2199 activities: %{a1: a1.id, a2: a2.id, a3: a3.id, a4: a4.id},
2200 u1: %{r1: r1_1.id, r2: r1_2.id, r3: r1_3.id},
2201 u2: %{r1: r2_1.id, r2: r2_2.id},
2202 u3: %{r1: r3_1.id, r2: r3_2.id},
2203 u4: %{r1: r4_1.id}}
2204 end
2205
2206 describe "maybe_update_follow_information/1" do
2207 setup do
2208 clear_config([:instance, :external_user_synchronization], true)
2209
2210 user = %{
2211 local: false,
2212 ap_id: "https://gensokyo.2hu/users/raymoo",
2213 following_address: "https://gensokyo.2hu/users/following",
2214 follower_address: "https://gensokyo.2hu/users/followers",
2215 type: "Person"
2216 }
2217
2218 %{user: user}
2219 end
2220
2221 test "logs an error when it can't fetch the info", %{user: user} do
2222 assert capture_log(fn ->
2223 ActivityPub.maybe_update_follow_information(user)
2224 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2225 end
2226
2227 test "just returns the input if the user type is Application", %{
2228 user: user
2229 } do
2230 user =
2231 user
2232 |> Map.put(:type, "Application")
2233
2234 refute capture_log(fn ->
2235 assert ^user = ActivityPub.maybe_update_follow_information(user)
2236 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2237 end
2238
2239 test "it just returns the input if the user has no following/follower addresses", %{
2240 user: user
2241 } do
2242 user =
2243 user
2244 |> Map.put(:following_address, nil)
2245 |> Map.put(:follower_address, nil)
2246
2247 refute capture_log(fn ->
2248 assert ^user = ActivityPub.maybe_update_follow_information(user)
2249 end) =~ "Follower/Following counter update for #{user.ap_id} failed"
2250 end
2251 end
2252 end