2018-02-15 14:00:06 -05:00
|
|
|
defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
|
|
|
|
use Pleroma.DataCase
|
|
|
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
|
|
|
alias Pleroma.Activity
|
2018-02-17 08:55:44 -05:00
|
|
|
alias Pleroma.User
|
2018-02-17 14:13:12 -05:00
|
|
|
alias Pleroma.Repo
|
|
|
|
import Ecto.Query
|
|
|
|
|
2018-02-17 08:11:20 -05:00
|
|
|
import Pleroma.Factory
|
|
|
|
alias Pleroma.Web.CommonAPI
|
2018-02-15 14:00:06 -05:00
|
|
|
|
|
|
|
describe "handle_incoming" do
|
2018-02-23 02:49:57 -05:00
|
|
|
|
|
|
|
test "it correctly connects AP and non-AP statuses" do
|
|
|
|
last = "https://mstdn.io/users/mayuutann/statuses/99568293732299394"
|
|
|
|
end
|
|
|
|
|
2018-02-19 11:37:45 -05:00
|
|
|
test "it ignores an incoming notice if we already have it" do
|
|
|
|
activity = insert(:note_activity)
|
|
|
|
|
|
|
|
data = File.read!("test/fixtures/mastodon-post-activity.json")
|
|
|
|
|> Poison.decode!
|
|
|
|
|> Map.put("object", activity.data["object"])
|
|
|
|
|
|
|
|
{:ok, returned_activity} = Transmogrifier.handle_incoming(data)
|
|
|
|
|
|
|
|
assert activity == returned_activity
|
|
|
|
end
|
|
|
|
|
2018-02-21 09:22:24 -05:00
|
|
|
test "it fetches replied-to activities if we don't have them" do
|
|
|
|
data = File.read!("test/fixtures/mastodon-post-activity.json")
|
|
|
|
|> Poison.decode!
|
|
|
|
|
|
|
|
object = data["object"]
|
|
|
|
|> Map.put("inReplyTo", "https://shitposter.club/notice/2827873")
|
|
|
|
|
|
|
|
data = data
|
|
|
|
|> Map.put("object", object)
|
|
|
|
|
|
|
|
{:ok, returned_activity} = Transmogrifier.handle_incoming(data)
|
|
|
|
|
|
|
|
assert Activity.get_create_activity_by_object_ap_id("tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment")
|
|
|
|
end
|
|
|
|
|
2018-02-15 14:00:06 -05:00
|
|
|
test "it works for incoming notices" do
|
|
|
|
data = File.read!("test/fixtures/mastodon-post-activity.json") |> Poison.decode!
|
|
|
|
|
|
|
|
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
|
|
|
|
assert data["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822/activity"
|
|
|
|
assert data["context"] == "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
|
|
|
|
assert data["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
|
|
|
|
assert data["cc"] == [
|
|
|
|
"http://mastodon.example.org/users/admin/followers",
|
|
|
|
"http://localtesting.pleroma.lol/users/lain"
|
|
|
|
]
|
|
|
|
assert data["actor"] == "http://mastodon.example.org/users/admin"
|
|
|
|
|
|
|
|
object = data["object"]
|
|
|
|
assert object["id"] == "http://mastodon.example.org/users/admin/statuses/99512778738411822"
|
|
|
|
|
|
|
|
assert object["to"] == ["https://www.w3.org/ns/activitystreams#Public"]
|
|
|
|
assert object["cc"] == [
|
|
|
|
"http://mastodon.example.org/users/admin/followers",
|
|
|
|
"http://localtesting.pleroma.lol/users/lain"
|
|
|
|
]
|
|
|
|
assert object["actor"] == "http://mastodon.example.org/users/admin"
|
|
|
|
assert object["attributedTo"] == "http://mastodon.example.org/users/admin"
|
2018-02-19 04:39:03 -05:00
|
|
|
assert object["context"] == "tag:mastodon.example.org,2018-02-12:objectId=20:objectType=Conversation"
|
2018-02-18 08:14:16 -05:00
|
|
|
assert object["sensitive"] == true
|
2018-02-15 14:00:06 -05:00
|
|
|
end
|
2018-02-17 08:55:44 -05:00
|
|
|
|
|
|
|
test "it works for incoming follow requests" do
|
|
|
|
user = insert(:user)
|
|
|
|
data = File.read!("test/fixtures/mastodon-follow-activity.json") |> Poison.decode!
|
|
|
|
|> Map.put("object", user.ap_id)
|
|
|
|
|
|
|
|
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
|
|
|
|
|
|
|
|
assert data["actor"] == "http://mastodon.example.org/users/admin"
|
|
|
|
assert data["type"] == "Follow"
|
|
|
|
assert data["id"] == "http://mastodon.example.org/users/admin#follows/2"
|
|
|
|
assert User.following?(User.get_by_ap_id(data["actor"]), user)
|
2018-02-17 14:13:12 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it works for incoming likes" do
|
|
|
|
user = insert(:user)
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "hello"})
|
2018-02-17 08:55:44 -05:00
|
|
|
|
2018-02-17 14:13:12 -05:00
|
|
|
data = File.read!("test/fixtures/mastodon-like.json") |> Poison.decode!
|
|
|
|
|> Map.put("object", activity.data["object"]["id"])
|
|
|
|
|
|
|
|
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
|
|
|
|
|
|
|
|
assert data["actor"] == "http://mastodon.example.org/users/admin"
|
|
|
|
assert data["type"] == "Like"
|
|
|
|
assert data["id"] == "http://mastodon.example.org/users/admin#likes/2"
|
|
|
|
assert data["object"] == activity.data["object"]["id"]
|
2018-02-17 08:55:44 -05:00
|
|
|
end
|
2018-02-17 15:57:31 -05:00
|
|
|
|
|
|
|
test "it works for incoming announces" do
|
|
|
|
data = File.read!("test/fixtures/mastodon-announce.json") |> Poison.decode!
|
|
|
|
|
|
|
|
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
|
|
|
|
|
|
|
|
assert data["actor"] == "http://mastodon.example.org/users/admin"
|
|
|
|
assert data["type"] == "Announce"
|
|
|
|
assert data["id"] == "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
|
|
|
|
assert data["object"] == "http://mastodon.example.org/users/admin/statuses/99541947525187367"
|
2018-02-18 05:24:54 -05:00
|
|
|
|
|
|
|
assert Activity.get_create_activity_by_object_ap_id(data["object"])
|
|
|
|
end
|
|
|
|
|
|
|
|
test "it works for incoming announces with an existing activity" do
|
|
|
|
user = insert(:user)
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
|
|
|
|
|
|
|
|
data = File.read!("test/fixtures/mastodon-announce.json")
|
|
|
|
|> Poison.decode!
|
|
|
|
|> Map.put("object", activity.data["object"]["id"])
|
|
|
|
|
|
|
|
{:ok, %Activity{data: data, local: false}} = Transmogrifier.handle_incoming(data)
|
|
|
|
|
|
|
|
assert data["actor"] == "http://mastodon.example.org/users/admin"
|
|
|
|
assert data["type"] == "Announce"
|
|
|
|
assert data["id"] == "http://mastodon.example.org/users/admin/statuses/99542391527669785/activity"
|
|
|
|
assert data["object"] == activity.data["object"]["id"]
|
|
|
|
|
|
|
|
assert Activity.get_create_activity_by_object_ap_id(data["object"]).id == activity.id
|
2018-02-17 15:57:31 -05:00
|
|
|
end
|
2018-02-15 14:00:06 -05:00
|
|
|
end
|
2018-02-17 08:11:20 -05:00
|
|
|
|
|
|
|
describe "prepare outgoing" do
|
|
|
|
test "it turns mentions into tags" do
|
|
|
|
user = insert(:user)
|
|
|
|
other_user = insert(:user)
|
|
|
|
|
2018-02-18 07:51:03 -05:00
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "hey, @#{other_user.nickname}, how are ya? #2hu"})
|
2018-02-17 08:11:20 -05:00
|
|
|
|
|
|
|
{:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
|
|
|
|
object = modified["object"]
|
|
|
|
|
2018-02-18 07:51:03 -05:00
|
|
|
expected_mention = %{
|
2018-02-17 08:11:20 -05:00
|
|
|
"href" => other_user.ap_id,
|
|
|
|
"name" => "@#{other_user.nickname}",
|
2018-02-17 08:20:53 -05:00
|
|
|
"type" => "Mention"
|
2018-02-17 08:11:20 -05:00
|
|
|
}
|
2018-02-19 04:39:03 -05:00
|
|
|
|
2018-02-18 07:51:03 -05:00
|
|
|
expected_tag = %{
|
|
|
|
"href" => Pleroma.Web.Endpoint.url <> "/tags/2hu",
|
|
|
|
"type" => "Hashtag",
|
|
|
|
"name" => "#2hu"
|
|
|
|
}
|
2018-02-17 08:11:20 -05:00
|
|
|
|
2018-02-17 08:20:53 -05:00
|
|
|
assert Enum.member?(object["tag"], expected_tag)
|
2018-02-18 07:51:03 -05:00
|
|
|
assert Enum.member?(object["tag"], expected_mention)
|
2018-02-17 08:11:20 -05:00
|
|
|
end
|
|
|
|
|
2018-02-18 08:07:13 -05:00
|
|
|
test "it adds the sensitive property" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "#nsfw hey"})
|
|
|
|
{:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
|
|
|
|
|
|
|
|
assert modified["object"]["sensitive"]
|
|
|
|
end
|
|
|
|
|
2018-02-18 07:58:52 -05:00
|
|
|
test "it adds the json-ld context and the conversation property" do
|
2018-02-17 08:11:20 -05:00
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
|
|
|
|
{:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
|
|
|
|
|
|
|
|
assert modified["@context"] == "https://www.w3.org/ns/activitystreams"
|
2018-02-18 07:58:52 -05:00
|
|
|
assert modified["object"]["conversation"] == modified["context"]
|
2018-02-17 08:11:20 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
test "it sets the 'attributedTo' property to the actor of the object if it doesn't have one" do
|
|
|
|
user = insert(:user)
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "hey"})
|
|
|
|
{:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
|
|
|
|
|
|
|
|
assert modified["object"]["actor"] == modified["object"]["attributedTo"]
|
|
|
|
end
|
|
|
|
end
|
2018-02-21 16:21:40 -05:00
|
|
|
|
|
|
|
describe "user upgrade" do
|
|
|
|
test "it upgrades a user to activitypub" do
|
|
|
|
user = insert(:user, %{local: false, ap_id: "https://niu.moe/users/rye", follower_address: "..."})
|
|
|
|
user_two = insert(:user, %{following: [user.follower_address]})
|
|
|
|
|
|
|
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "test"})
|
|
|
|
{:ok, unrelated_activity} = CommonAPI.post(user_two, %{"status" => "test"})
|
|
|
|
assert "..." in activity.recipients
|
|
|
|
|
|
|
|
user = Repo.get(User, user.id)
|
|
|
|
assert user.info["note_count"] == 1
|
|
|
|
|
|
|
|
{:ok, user} = Transmogrifier.upgrade_user_from_ap_id("https://niu.moe/users/rye")
|
|
|
|
assert user.info["ap_enabled"]
|
|
|
|
assert user.info["note_count"] == 1
|
|
|
|
assert user.follower_address == "https://niu.moe/users/rye/followers"
|
|
|
|
|
|
|
|
# Wait for the background task
|
|
|
|
:timer.sleep(1000)
|
|
|
|
|
|
|
|
user = Repo.get(User, user.id)
|
|
|
|
assert user.info["note_count"] == 1
|
|
|
|
|
|
|
|
activity = Repo.get(Activity, activity.id)
|
|
|
|
assert user.follower_address in activity.recipients
|
2018-02-22 02:14:15 -05:00
|
|
|
assert %{"url" => [%{"href" => "https://cdn.niu.moe/accounts/avatars/000/033/323/original/fd7f8ae0b3ffedc9.jpeg"}]} = user.avatar
|
|
|
|
assert %{"url" => [%{"href" => "https://cdn.niu.moe/accounts/headers/000/033/323/original/850b3448fa5fd477.png"}]} = user.info["banner"]
|
2018-02-21 16:21:40 -05:00
|
|
|
refute "..." in activity.recipients
|
|
|
|
|
|
|
|
unrelated_activity = Repo.get(Activity, unrelated_activity.id)
|
|
|
|
refute user.follower_address in unrelated_activity.recipients
|
|
|
|
|
|
|
|
user_two = Repo.get(User, user_two.id)
|
|
|
|
assert user.follower_address in user_two.following
|
|
|
|
refute "..." in user_two.following
|
|
|
|
end
|
|
|
|
end
|
2018-02-15 14:00:06 -05:00
|
|
|
end
|