end
end
+ # TODO:
+ # This will create a Create activity, which we need internally at the moment.
def fetch_object_from_id(id) do
if object = Object.get_cached_by_ap_id(id) do
{:ok, object}
with {:ok, %{body: body, status_code: code}} when code in 200..299 <- @httpoison.get(id, [Accept: "application/activity+json"], follow_redirect: true, timeout: 10000, recv_timeout: 20000),
{:ok, data} <- Poison.decode(body),
data <- Transmogrifier.fix_object(data),
- %User{} <- User.get_or_fetch_by_ap_id(data["attributedTo"]) do
- Object.create(data)
+ nil <- Object.get_by_ap_id(data["id"]),
+ %User{} = user <- User.get_or_fetch_by_ap_id(data["attributedTo"]),
+ {:ok, activity} = create(%{to: data["to"], actor: user, context: data["context"], object: data, local: false, additional: %{"cc" => data["cc"]}}) do
+ {:ok, Object.get_by_ap_id(activity.data["object"]["id"])}
+ else
+ object = %Object{} -> {:ok, object}
+ e -> e
end
end
end
"""
alias Pleroma.User
alias Pleroma.Object
+ alias Pleroma.Activity
alias Pleroma.Web.ActivityPub.ActivityPub
@doc """
def handle_incoming(%{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = data) do
with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
- {:ok, object} <- ActivityPub.fetch_object_from_id(object_id),
+ {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
{:ok, activity, object} <- ActivityPub.announce(actor, object, id, false) do
{:ok, activity}
else
def handle_incoming(_), do: :error
+ def get_obj_helper(id) do
+ if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
+ end
+
@doc
"""
internal -> Mastodon
end
describe "fetching an object" do
- test "it fetches an existing object" do
+ test "it fetches an object" do
{:ok, object} = ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
+ assert Activity.get_create_activity_by_object_ap_id(object.data["id"])
+ {:ok, object_again} = ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367")
+
+ assert object == object_again
end
end
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"
+
+ 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
end
end