1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
6 use Oban.Testing, repo: Pleroma.Repo
10 alias Pleroma.Notification
13 alias Pleroma.Tests.ObanHelpers
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.Builder
17 alias Pleroma.Web.ActivityPub.SideEffects
18 alias Pleroma.Web.ActivityPub.Utils
19 alias Pleroma.Web.CommonAPI
22 import Pleroma.Factory
25 test "it queues a fetch of instance information" do
26 author = insert(:user, local: false, ap_id: "https://wowee.example.com/users/1")
27 recipient = insert(:user, local: true)
29 {:ok, note_data, _meta} =
30 Builder.note(%Pleroma.Web.CommonAPI.ActivityDraft{
32 to: [recipient.ap_id],
33 mentions: [recipient],
35 extra: %{"id" => "https://wowee.example.com/notes/1"}
38 {:ok, create_activity_data, _meta} =
39 Builder.create(author, note_data["id"], [recipient.ap_id])
41 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
43 {:ok, _create_activity, _meta} =
44 SideEffects.handle(create_activity, local: false, object_data: note_data)
47 worker: Pleroma.Workers.NodeInfoFetcherWorker,
48 args: %{"op" => "process", "source_url" => "https://wowee.example.com/users/1"}
53 describe "handle_after_transaction" do
54 test "it streams out notifications and streams" do
55 author = insert(:user, local: true)
56 recipient = insert(:user, local: true)
58 {:ok, note_data, _meta} =
59 Builder.note(%Pleroma.Web.CommonAPI.ActivityDraft{
61 to: [recipient.ap_id],
62 mentions: [recipient],
64 extra: %{"id" => Utils.generate_object_id()}
67 {:ok, create_activity_data, _meta} =
68 Builder.create(author, note_data["id"], [recipient.ap_id])
70 {:ok, create_activity, _meta} = ActivityPub.persist(create_activity_data, local: false)
72 {:ok, _create_activity, meta} =
73 SideEffects.handle(create_activity, local: false, object_data: note_data)
75 assert [notification] = meta[:notifications]
82 stream: fn _, _ -> nil end
93 SideEffects.handle_after_transaction(meta)
95 assert called(Pleroma.Web.Streamer.stream(["user", "user:notification"], notification))
96 assert called(Pleroma.Web.Push.send(notification))
101 describe "blocking users" do
104 blocked = insert(:user)
105 User.follow(blocked, user)
106 User.follow(user, blocked)
108 {:ok, block_data, []} = Builder.block(user, blocked)
109 {:ok, block, _meta} = ActivityPub.persist(block_data, local: true)
111 %{user: user, blocked: blocked, block: block}
114 test "it unfollows and blocks", %{user: user, blocked: blocked, block: block} do
115 assert User.following?(user, blocked)
116 assert User.following?(blocked, user)
118 {:ok, _, _} = SideEffects.handle(block)
120 refute User.following?(user, blocked)
121 refute User.following?(blocked, user)
122 assert User.blocks?(user, blocked)
125 test "it updates following relationship", %{user: user, blocked: blocked, block: block} do
126 {:ok, _, _} = SideEffects.handle(block)
128 refute Pleroma.FollowingRelationship.get(user, blocked)
129 assert User.get_follow_state(user, blocked) == nil
130 assert User.get_follow_state(blocked, user) == nil
131 assert User.get_follow_state(user, blocked, nil) == nil
132 assert User.get_follow_state(blocked, user, nil) == nil
135 test "it blocks but does not unfollow if the relevant setting is set", %{
140 clear_config([:activitypub, :unfollow_blocked], false)
141 assert User.following?(user, blocked)
142 assert User.following?(blocked, user)
144 {:ok, _, _} = SideEffects.handle(block)
146 refute User.following?(user, blocked)
147 assert User.following?(blocked, user)
148 assert User.blocks?(user, blocked)
152 describe "update users" do
154 user = insert(:user, local: false)
156 {:ok, update_data, []} =
157 Builder.update(user, %{"id" => user.ap_id, "type" => "Person", "name" => "new name!"})
159 {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
161 %{user: user, update_data: update_data, update: update}
164 test "it updates the user", %{user: user, update: update} do
165 {:ok, _, _} = SideEffects.handle(update)
166 user = User.get_by_id(user.id)
167 assert user.name == "new name!"
170 test "it uses a given changeset to update", %{user: user, update: update} do
171 changeset = Ecto.Changeset.change(user, %{default_scope: "direct"})
173 assert user.default_scope == "public"
174 {:ok, _, _} = SideEffects.handle(update, user_update_changeset: changeset)
175 user = User.get_by_id(user.id)
176 assert user.default_scope == "direct"
180 describe "update notes" do
183 Pleroma.Web.ActivityPub.Utils.make_date()
187 note = insert(:note, user: user, data: %{"published" => make_time.()})
188 _note_activity = insert(:note_activity, note: note)
192 |> Map.put("summary", "edited summary")
193 |> Map.put("content", "edited content")
194 |> Map.put("updated", make_time.())
196 {:ok, update_data, []} = Builder.update(user, updated_note)
197 {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
203 update_data: update_data,
205 updated_note: updated_note
209 test "it updates the note", %{
210 object_id: object_id,
212 updated_note: updated_note
214 {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
215 updated_time = updated_note["updated"]
217 new_note = Pleroma.Object.get_by_id(object_id)
220 "summary" => "edited summary",
221 "content" => "edited content",
222 "updated" => ^updated_time
226 test "it rejects updates with no updated attribute in object", %{
227 object_id: object_id,
229 updated_note: updated_note
231 old_note = Pleroma.Object.get_by_id(object_id)
232 updated_note = Map.drop(updated_note, ["updated"])
233 {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
234 new_note = Pleroma.Object.get_by_id(object_id)
235 assert old_note.data == new_note.data
238 test "it rejects updates with updated attribute older than what we have in the original object",
240 object_id: object_id,
242 updated_note: updated_note
244 old_note = Pleroma.Object.get_by_id(object_id)
245 {:ok, creation_time, _} = DateTime.from_iso8601(old_note.data["published"])
248 Map.put(updated_note, "updated", DateTime.to_iso8601(DateTime.add(creation_time, -10)))
250 {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
251 new_note = Pleroma.Object.get_by_id(object_id)
252 assert old_note.data == new_note.data
255 test "it rejects updates with updated attribute older than the last Update", %{
256 object_id: object_id,
258 updated_note: updated_note
260 old_note = Pleroma.Object.get_by_id(object_id)
261 {:ok, creation_time, _} = DateTime.from_iso8601(old_note.data["published"])
264 Map.put(updated_note, "updated", DateTime.to_iso8601(DateTime.add(creation_time, +10)))
266 {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
268 old_note = Pleroma.Object.get_by_id(object_id)
269 {:ok, update_time, _} = DateTime.from_iso8601(old_note.data["updated"])
272 Map.put(updated_note, "updated", DateTime.to_iso8601(DateTime.add(update_time, -5)))
274 {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
276 new_note = Pleroma.Object.get_by_id(object_id)
277 assert old_note.data == new_note.data
280 test "it updates using object_data", %{
281 object_id: object_id,
283 updated_note: updated_note
285 updated_note = Map.put(updated_note, "summary", "mew mew")
286 {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
287 new_note = Pleroma.Object.get_by_id(object_id)
288 assert %{"summary" => "mew mew", "content" => "edited content"} = new_note.data
291 test "it records the original note in formerRepresentations", %{
293 object_id: object_id,
295 updated_note: updated_note
297 {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
298 %{data: new_note} = Pleroma.Object.get_by_id(object_id)
299 assert %{"summary" => "edited summary", "content" => "edited content"} = new_note
301 assert [Map.drop(note.data, ["id", "formerRepresentations"])] ==
302 new_note["formerRepresentations"]["orderedItems"]
304 assert new_note["formerRepresentations"]["totalItems"] == 1
307 test "it puts the original note at the front of formerRepresentations", %{
310 object_id: object_id,
312 updated_note: updated_note
314 {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
315 %{data: first_edit} = Pleroma.Object.get_by_id(object_id)
317 second_updated_note =
319 |> Map.put("summary", "edited summary 2")
320 |> Map.put("content", "edited content 2")
323 first_edit["updated"]
324 |> DateTime.from_iso8601()
327 |> DateTime.to_iso8601()
330 {:ok, second_update_data, []} = Builder.update(user, second_updated_note)
331 {:ok, update, _meta} = ActivityPub.persist(second_update_data, local: true)
332 {:ok, _, _} = SideEffects.handle(update, object_data: second_updated_note)
333 %{data: new_note} = Pleroma.Object.get_by_id(object_id)
334 assert %{"summary" => "edited summary 2", "content" => "edited content 2"} = new_note
336 original_version = Map.drop(note.data, ["id", "formerRepresentations"])
337 first_edit = Map.drop(first_edit, ["id", "formerRepresentations"])
339 assert [first_edit, original_version] ==
340 new_note["formerRepresentations"]["orderedItems"]
342 assert new_note["formerRepresentations"]["totalItems"] == 2
345 test "it does not prepend to formerRepresentations if no actual changes are made", %{
347 object_id: object_id,
349 updated_note: updated_note
351 {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
352 %{data: first_edit} = Pleroma.Object.get_by_id(object_id)
358 first_edit["updated"]
359 |> DateTime.from_iso8601()
362 |> DateTime.to_iso8601()
365 {:ok, _, _} = SideEffects.handle(update, object_data: updated_note)
366 %{data: new_note} = Pleroma.Object.get_by_id(object_id)
367 assert %{"summary" => "edited summary", "content" => "edited content"} = new_note
369 original_version = Map.drop(note.data, ["id", "formerRepresentations"])
371 assert [original_version] ==
372 new_note["formerRepresentations"]["orderedItems"]
374 assert new_note["formerRepresentations"]["totalItems"] == 1
378 describe "update questions" do
385 data: %{"published" => Pleroma.Web.ActivityPub.Utils.make_date()}
388 %{user: user, data: question.data, id: question.id}
391 test "allows updating choice count without generating edit history", %{
398 |> Enum.map(fn choice -> put_in(choice, ["replies", "totalItems"], 5) end)
402 |> Map.put("oneOf", new_choices)
403 |> Map.put("updated", Pleroma.Web.ActivityPub.Utils.make_date())
405 {:ok, update_data, []} = Builder.update(user, updated_question)
406 {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
408 {:ok, _, _} = SideEffects.handle(update, object_data: updated_question)
410 %{data: new_question} = Pleroma.Object.get_by_id(id)
412 assert [%{"replies" => %{"totalItems" => 5}}, %{"replies" => %{"totalItems" => 5}}] =
413 new_question["oneOf"]
415 refute Map.has_key?(new_question, "formerRepresentations")
418 test "allows updating choice count without updated field", %{
425 |> Enum.map(fn choice -> put_in(choice, ["replies", "totalItems"], 5) end)
429 |> Map.put("oneOf", new_choices)
431 {:ok, update_data, []} = Builder.update(user, updated_question)
432 {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
434 {:ok, _, _} = SideEffects.handle(update, object_data: updated_question)
436 %{data: new_question} = Pleroma.Object.get_by_id(id)
438 assert [%{"replies" => %{"totalItems" => 5}}, %{"replies" => %{"totalItems" => 5}}] =
439 new_question["oneOf"]
441 refute Map.has_key?(new_question, "formerRepresentations")
444 test "allows updating choice count with updated field same as the creation date", %{
451 |> Enum.map(fn choice -> put_in(choice, ["replies", "totalItems"], 5) end)
455 |> Map.put("oneOf", new_choices)
456 |> Map.put("updated", data["published"])
458 {:ok, update_data, []} = Builder.update(user, updated_question)
459 {:ok, update, _meta} = ActivityPub.persist(update_data, local: true)
461 {:ok, _, _} = SideEffects.handle(update, object_data: updated_question)
463 %{data: new_question} = Pleroma.Object.get_by_id(id)
465 assert [%{"replies" => %{"totalItems" => 5}}, %{"replies" => %{"totalItems" => 5}}] =
466 new_question["oneOf"]
468 refute Map.has_key?(new_question, "formerRepresentations")
472 describe "EmojiReact objects" do
474 poster = insert(:user)
477 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
479 {:ok, emoji_react_data, []} = Builder.emoji_react(user, post.object, "👌")
480 {:ok, emoji_react, _meta} = ActivityPub.persist(emoji_react_data, local: true)
482 %{emoji_react: emoji_react, user: user, poster: poster}
485 test "adds the reaction to the object", %{emoji_react: emoji_react, user: user} do
486 {:ok, emoji_react, _} = SideEffects.handle(emoji_react)
487 object = Object.get_by_ap_id(emoji_react.data["object"])
489 assert object.data["reaction_count"] == 1
490 assert ["👌", [user.ap_id], nil] in object.data["reactions"]
493 test "creates a notification", %{emoji_react: emoji_react, poster: poster} do
494 {:ok, emoji_react, _} = SideEffects.handle(emoji_react)
495 assert Repo.get_by(Notification, user_id: poster.id, activity_id: emoji_react.id)
499 describe "Question objects" do
502 question = build(:question, user: user)
503 question_activity = build(:question_activity, question: question)
504 activity_data = Map.put(question_activity.data, "object", question.data["id"])
505 meta = [object_data: question.data, local: false]
507 {:ok, activity, meta} = ActivityPub.persist(activity_data, meta)
509 %{activity: activity, meta: meta}
512 test "enqueues the poll end", %{activity: activity, meta: meta} do
513 {:ok, activity, meta} = SideEffects.handle(activity, meta)
516 worker: Pleroma.Workers.PollWorker,
517 args: %{op: "poll_end", activity_id: activity.id},
518 scheduled_at: NaiveDateTime.from_iso8601!(meta[:object_data]["closed"])
523 describe "delete users with confirmation pending" do
525 user = insert(:user, is_confirmed: false)
526 {:ok, delete_user_data, _meta} = Builder.delete(user, user.ap_id)
527 {:ok, delete_user, _meta} = ActivityPub.persist(delete_user_data, local: true)
528 {:ok, delete: delete_user, user: user}
531 test "when activation is required", %{delete: delete, user: user} do
532 clear_config([:instance, :account_activation_required], true)
533 {:ok, _, _} = SideEffects.handle(delete)
534 ObanHelpers.perform_all()
536 refute User.get_cached_by_id(user.id)
540 describe "Undo objects" do
542 poster = insert(:user)
544 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
545 {:ok, like} = CommonAPI.favorite(user, post.id)
546 {:ok, reaction} = CommonAPI.react_with_emoji(post.id, user, "👍")
547 {:ok, announce} = CommonAPI.repeat(post.id, user)
548 {:ok, block} = CommonAPI.block(user, poster)
550 {:ok, undo_data, _meta} = Builder.undo(user, like)
551 {:ok, like_undo, _meta} = ActivityPub.persist(undo_data, local: true)
553 {:ok, undo_data, _meta} = Builder.undo(user, reaction)
554 {:ok, reaction_undo, _meta} = ActivityPub.persist(undo_data, local: true)
556 {:ok, undo_data, _meta} = Builder.undo(user, announce)
557 {:ok, announce_undo, _meta} = ActivityPub.persist(undo_data, local: true)
559 {:ok, undo_data, _meta} = Builder.undo(user, block)
560 {:ok, block_undo, _meta} = ActivityPub.persist(undo_data, local: true)
563 like_undo: like_undo,
566 reaction_undo: reaction_undo,
568 announce_undo: announce_undo,
570 block_undo: block_undo,
577 test "deletes the original block", %{
578 block_undo: block_undo,
581 {:ok, _block_undo, _meta} = SideEffects.handle(block_undo)
583 refute Activity.get_by_id(block.id)
586 test "unblocks the blocked user", %{block_undo: block_undo, block: block} do
587 blocker = User.get_by_ap_id(block.data["actor"])
588 blocked = User.get_by_ap_id(block.data["object"])
590 {:ok, _block_undo, _} = SideEffects.handle(block_undo)
591 refute User.blocks?(blocker, blocked)
594 test "an announce undo removes the announce from the object", %{
595 announce_undo: announce_undo,
598 {:ok, _announce_undo, _} = SideEffects.handle(announce_undo)
600 object = Object.get_by_ap_id(post.data["object"])
602 assert object.data["announcement_count"] == 0
603 assert object.data["announcements"] == []
606 test "deletes the original announce", %{announce_undo: announce_undo, announce: announce} do
607 {:ok, _announce_undo, _} = SideEffects.handle(announce_undo)
608 refute Activity.get_by_id(announce.id)
611 test "a reaction undo removes the reaction from the object", %{
612 reaction_undo: reaction_undo,
615 {:ok, _reaction_undo, _} = SideEffects.handle(reaction_undo)
617 object = Object.get_by_ap_id(post.data["object"])
619 assert object.data["reaction_count"] == 0
620 assert object.data["reactions"] == []
623 test "deletes the original reaction", %{reaction_undo: reaction_undo, reaction: reaction} do
624 {:ok, _reaction_undo, _} = SideEffects.handle(reaction_undo)
625 refute Activity.get_by_id(reaction.id)
628 test "a like undo removes the like from the object", %{like_undo: like_undo, post: post} do
629 {:ok, _like_undo, _} = SideEffects.handle(like_undo)
631 object = Object.get_by_ap_id(post.data["object"])
633 assert object.data["like_count"] == 0
634 assert object.data["likes"] == []
637 test "deletes the original like", %{like_undo: like_undo, like: like} do
638 {:ok, _like_undo, _} = SideEffects.handle(like_undo)
639 refute Activity.get_by_id(like.id)
643 describe "like objects" do
645 poster = insert(:user)
647 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
649 {:ok, like_data, _meta} = Builder.like(user, post.object)
650 {:ok, like, _meta} = ActivityPub.persist(like_data, local: true)
652 %{like: like, user: user, poster: poster}
655 test "add the like to the original object", %{like: like, user: user} do
656 {:ok, like, _} = SideEffects.handle(like)
657 object = Object.get_by_ap_id(like.data["object"])
658 assert object.data["like_count"] == 1
659 assert user.ap_id in object.data["likes"]
662 test "creates a notification", %{like: like, poster: poster} do
663 {:ok, like, _} = SideEffects.handle(like)
664 assert Repo.get_by(Notification, user_id: poster.id, activity_id: like.id)
668 describe "announce objects" do
670 poster = insert(:user)
672 {:ok, post} = CommonAPI.post(poster, %{status: "hey"})
673 {:ok, private_post} = CommonAPI.post(poster, %{status: "hey", visibility: "private"})
675 {:ok, announce_data, _meta} = Builder.announce(user, post.object, public: true)
677 {:ok, private_announce_data, _meta} =
678 Builder.announce(user, private_post.object, public: false)
680 {:ok, relay_announce_data, _meta} =
681 Builder.announce(Pleroma.Web.ActivityPub.Relay.get_actor(), post.object, public: true)
683 {:ok, announce, _meta} = ActivityPub.persist(announce_data, local: true)
684 {:ok, private_announce, _meta} = ActivityPub.persist(private_announce_data, local: true)
685 {:ok, relay_announce, _meta} = ActivityPub.persist(relay_announce_data, local: true)
691 private_announce: private_announce,
692 relay_announce: relay_announce
696 test "adds the announce to the original object", %{announce: announce, user: user} do
697 {:ok, announce, _} = SideEffects.handle(announce)
698 object = Object.get_by_ap_id(announce.data["object"])
699 assert object.data["announcement_count"] == 1
700 assert user.ap_id in object.data["announcements"]
703 test "does not add the announce to the original object if the actor is a service actor", %{
704 relay_announce: announce
706 {:ok, announce, _} = SideEffects.handle(announce)
707 object = Object.get_by_ap_id(announce.data["object"])
708 assert object.data["announcement_count"] == nil
711 test "creates a notification", %{announce: announce, poster: poster} do
712 {:ok, announce, _} = SideEffects.handle(announce)
713 assert Repo.get_by(Notification, user_id: poster.id, activity_id: announce.id)
716 test "it streams out the announce", %{announce: announce} do
719 Pleroma.Web.Streamer,
722 stream: fn _, _ -> nil end
729 send: fn _ -> nil end
733 {:ok, announce, _} = SideEffects.handle(announce)
736 Pleroma.Web.Streamer.stream(["user", "list", "public", "public:local"], announce)
739 assert called(Pleroma.Web.Push.send(:_))
744 describe "removing a follower" do
747 followed = insert(:user)
749 {:ok, _, _, follow_activity} = CommonAPI.follow(user, followed)
751 {:ok, reject_data, []} = Builder.reject(followed, follow_activity)
752 {:ok, reject, _meta} = ActivityPub.persist(reject_data, local: true)
754 %{user: user, followed: followed, reject: reject}
757 test "", %{user: user, followed: followed, reject: reject} do
758 assert User.following?(user, followed)
759 assert Pleroma.FollowingRelationship.get(user, followed)
761 {:ok, _, _} = SideEffects.handle(reject)
763 refute User.following?(user, followed)
764 refute Pleroma.FollowingRelationship.get(user, followed)
765 assert User.get_follow_state(user, followed) == nil
766 assert User.get_follow_state(user, followed, nil) == nil
770 describe "removing a follower from remote" do
773 followed = insert(:user, local: false)
775 # Mock a local-to-remote follow
776 {:ok, follow_data, []} = Builder.follow(user, followed)
780 |> Map.put("state", "accept")
782 {:ok, follow, _meta} = ActivityPub.persist(follow_data, local: true)
783 {:ok, _, _} = SideEffects.handle(follow)
785 # Mock a remote-to-local accept
786 {:ok, accept_data, _} = Builder.accept(followed, follow)
787 {:ok, accept, _} = ActivityPub.persist(accept_data, local: false)
788 {:ok, _, _} = SideEffects.handle(accept)
790 # Mock a remote-to-local reject
791 {:ok, reject_data, []} = Builder.reject(followed, follow)
792 {:ok, reject, _meta} = ActivityPub.persist(reject_data, local: false)
794 %{user: user, followed: followed, reject: reject}
797 test "", %{user: user, followed: followed, reject: reject} do
798 assert User.following?(user, followed)
799 assert Pleroma.FollowingRelationship.get(user, followed)
801 {:ok, _, _} = SideEffects.handle(reject)
803 refute User.following?(user, followed)
804 refute Pleroma.FollowingRelationship.get(user, followed)
806 assert Pleroma.Web.ActivityPub.Utils.fetch_latest_follow(user, followed).data["state"] ==
809 assert User.get_follow_state(user, followed) == nil
810 assert User.get_follow_state(user, followed, nil) == nil