+
+ describe "like an object" do
+ test "adds a like activity to the db" do
+ note_activity = insert(:note_activity)
+ object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+ user = insert(:user)
+ user_two = insert(:user)
+
+ {:ok, like_activity, object} = ActivityPub.like(user, object)
+
+ assert like_activity.data["actor"] == user.ap_id
+ assert like_activity.data["type"] == "Like"
+ assert like_activity.data["object"] == object.data["id"]
+ assert like_activity.data["to"] == [User.ap_followers(user), note_activity.data["actor"]]
+ assert like_activity.data["context"] == object.data["context"]
+ assert object.data["like_count"] == 1
+ assert object.data["likes"] == [user.ap_id]
+
+ # Just return the original activity if the user already liked it.
+ {:ok, same_like_activity, object} = ActivityPub.like(user, object)
+
+ assert like_activity == same_like_activity
+ assert object.data["likes"] == [user.ap_id]
+
+ [note_activity] = Activity.all_by_object_ap_id(object.data["id"])
+ assert note_activity.data["object"]["like_count"] == 1
+
+ {:ok, _like_activity, object} = ActivityPub.like(user_two, object)
+ assert object.data["like_count"] == 2
+ end
+ end
+
+ describe "unliking" do
+ test "unliking a previously liked object" do
+ note_activity = insert(:note_activity)
+ object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+ user = insert(:user)
+
+ # Unliking something that hasn't been liked does nothing
+ {:ok, object} = ActivityPub.unlike(user, object)
+ assert object.data["like_count"] == 0
+
+ {:ok, like_activity, object} = ActivityPub.like(user, object)
+ assert object.data["like_count"] == 1
+
+ {:ok, _, _, object} = ActivityPub.unlike(user, object)
+ assert object.data["like_count"] == 0
+
+ assert Repo.get(Activity, like_activity.id) == nil
+ end
+ end
+
+ describe "announcing an object" do
+ test "adds an announce activity to the db" do
+ note_activity = insert(:note_activity)
+ object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+ user = insert(:user)
+
+ {:ok, announce_activity, object} = ActivityPub.announce(user, object)
+ assert object.data["announcement_count"] == 1
+ assert object.data["announcements"] == [user.ap_id]
+
+ assert announce_activity.data["to"] == [
+ User.ap_followers(user),
+ note_activity.data["actor"]
+ ]
+
+ assert announce_activity.data["object"] == object.data["id"]
+ assert announce_activity.data["actor"] == user.ap_id
+ assert announce_activity.data["context"] == object.data["context"]
+ end
+ end
+
+ describe "unannouncing an object" do
+ test "unannouncing a previously announced object" do
+ note_activity = insert(:note_activity)
+ object = Object.get_by_ap_id(note_activity.data["object"]["id"])
+ user = insert(:user)
+
+ # Unannouncing an object that is not announced does nothing
+ # {:ok, object} = ActivityPub.unannounce(user, object)
+ # assert object.data["announcement_count"] == 0
+
+ {:ok, announce_activity, object} = ActivityPub.announce(user, object)
+ assert object.data["announcement_count"] == 1
+
+ {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
+ assert object.data["announcement_count"] == 0
+
+ assert unannounce_activity.data["to"] == [
+ User.ap_followers(user),
+ announce_activity.data["actor"]
+ ]
+
+ assert unannounce_activity.data["type"] == "Undo"
+ assert unannounce_activity.data["object"] == announce_activity.data
+ assert unannounce_activity.data["actor"] == user.ap_id
+ assert unannounce_activity.data["context"] == announce_activity.data["context"]
+
+ assert Repo.get(Activity, announce_activity.id) == nil
+ end
+ end
+
+ describe "uploading files" do
+ test "copies the file to the configured folder" do
+ file = %Plug.Upload{
+ content_type: "image/jpg",
+ path: Path.absname("test/fixtures/image.jpg"),
+ filename: "an_image.jpg"
+ }
+
+ {:ok, %Object{} = object} = ActivityPub.upload(file)
+ assert object.data["name"] == "an_image.jpg"
+ end
+
+ test "works with base64 encoded images" do
+ file = %{
+ "img" => data_uri()
+ }
+
+ {:ok, %Object{}} = ActivityPub.upload(file)
+ end
+ end
+
+ describe "fetch the latest Follow" do
+ test "fetches the latest Follow activity" do
+ %Activity{data: %{"type" => "Follow"}} = activity = insert(:follow_activity)
+ follower = Repo.get_by(User, ap_id: activity.data["actor"])
+ followed = Repo.get_by(User, ap_id: activity.data["object"])
+
+ assert activity == Utils.fetch_latest_follow(follower, followed)
+ end
+ end
+
+ describe "fetching an object" do
+ test "it fetches an object" do
+ {:ok, object} =
+ ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
+
+ assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
+ assert activity.data["id"]
+
+ {:ok, object_again} =
+ ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
+
+ assert [attachment] = object.data["attachment"]
+ assert is_list(attachment["url"])
+
+ assert object == object_again
+ end
+
+ test "it works with objects only available via Ostatus" do
+ {:ok, object} = ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
+ assert activity = Activity.get_create_activity_by_object_ap_id(object.data["id"])
+ assert activity.data["id"]
+
+ {:ok, object_again} =
+ ActivityPub.fetch_object_from_id("https://shitposter.club/notice/2827873")
+
+ assert object == object_again
+ end
+
+ test "it correctly stitches up conversations between ostatus and ap" do
+ last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
+ {:ok, object} = ActivityPub.fetch_object_from_id(last)
+
+ object = Object.get_by_ap_id(object.data["inReplyTo"])
+ assert object
+ end
+ end
+
+ describe "following / unfollowing" do
+ test "creates a follow activity" do
+ follower = insert(:user)
+ followed = insert(:user)
+
+ {:ok, activity} = ActivityPub.follow(follower, followed)
+ assert activity.data["type"] == "Follow"
+ assert activity.data["actor"] == follower.ap_id
+ assert activity.data["object"] == followed.ap_id
+ end
+
+ test "creates an undo activity for the last follow" do
+ follower = insert(:user)
+ followed = insert(:user)
+
+ {:ok, follow_activity} = ActivityPub.follow(follower, followed)
+ {:ok, activity} = ActivityPub.unfollow(follower, followed)
+
+ assert activity.data["type"] == "Undo"
+ assert activity.data["actor"] == follower.ap_id
+
+ assert is_map(activity.data["object"])
+ assert activity.data["object"]["type"] == "Follow"
+ assert activity.data["object"]["object"] == followed.ap_id
+ assert activity.data["object"]["id"] == follow_activity.data["id"]
+ end
+ end
+
+ describe "blocking / unblocking" do
+ test "creates a block activity" do
+ blocker = insert(:user)
+ blocked = insert(:user)
+
+ {:ok, activity} = ActivityPub.block(blocker, blocked)
+
+ assert activity.data["type"] == "Block"
+ assert activity.data["actor"] == blocker.ap_id
+ assert activity.data["object"] == blocked.ap_id
+ end
+
+ test "creates an undo activity for the last block" do
+ blocker = insert(:user)
+ blocked = insert(:user)
+
+ {:ok, block_activity} = ActivityPub.block(blocker, blocked)
+ {:ok, activity} = ActivityPub.unblock(blocker, blocked)
+
+ assert activity.data["type"] == "Undo"
+ assert activity.data["actor"] == blocker.ap_id
+
+ assert is_map(activity.data["object"])
+ assert activity.data["object"]["type"] == "Block"
+ assert activity.data["object"]["object"] == blocked.ap_id
+ assert activity.data["object"]["id"] == block_activity.data["id"]
+ end
+ end
+
+ describe "deletion" do
+ test "it creates a delete activity and deletes the original object" do
+ note = insert(:note_activity)
+ object = Object.get_by_ap_id(note.data["object"]["id"])
+ {:ok, delete} = ActivityPub.delete(object)
+
+ assert delete.data["type"] == "Delete"
+ assert delete.data["actor"] == note.data["actor"]
+ assert delete.data["object"] == note.data["object"]["id"]
+
+ assert Repo.get(Activity, delete.id) != nil
+
+ assert Repo.get(Object, object.id) == nil
+ end
+ end
+
+ test "it can fetch plume articles" do
+ {:ok, object} =
+ ActivityPub.fetch_object_from_id(
+ "https://baptiste.gelez.xyz/~/PlumeDevelopment/this-month-in-plume-june-2018/"
+ )
+
+ assert object
+ end
+
+ describe "update" do
+ test "it creates an update activity with the new user data" do
+ user = insert(:user)
+ {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
+ user_data = Pleroma.Web.ActivityPub.UserView.render("user.json", %{user: user})
+
+ {:ok, update} =
+ ActivityPub.update(%{
+ actor: user_data["id"],
+ to: [user.follower_address],
+ cc: [],
+ object: user_data
+ })
+
+ assert update.data["actor"] == user.ap_id
+ assert update.data["to"] == [user.follower_address]
+ assert update.data["object"]["id"] == user_data["id"]
+ assert update.data["object"]["type"] == user_data["type"]
+ end
+ end
+
+ def data_uri do
+ File.read!("test/fixtures/avatar_data_uri")
+ end