diff options
author | lain <lain@soykaf.club> | 2018-02-17 21:56:33 +0100 |
---|---|---|
committer | lain <lain@soykaf.club> | 2018-02-17 21:56:33 +0100 |
commit | 5e36b750c1f98c440f4edcb9bb5bac5e6f93278f (patch) | |
tree | bd4792d1dcb5e0f6c1cd7a2ca86e6915b41836e0 | |
parent | 0f2ad25a7b1125c1e945d474a1873773fdfee26a (diff) | |
download | pleroma-5e36b750c1f98c440f4edcb9bb5bac5e6f93278f.tar.gz |
ActivityPub: Fetch an object from an id.
-rw-r--r-- | lib/pleroma/web/activity_pub/activity_pub.ex | 13 | ||||
-rw-r--r-- | test/web/activity_pub/activity_pub_test.exs | 6 |
2 files changed, 19 insertions, 0 deletions
diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 3d476d729..7c9ddcfe7 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -272,4 +272,17 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do @httpoison.post(inbox, Poison.encode!(data), [{"Content-Type", "application/activity+json"}, {"signature", signature}]) end end + + def fetch_object_from_id(id) do + if object = Object.get_cached_by_ap_id(id) do + {:ok, object} + else + 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) + end + end + end end diff --git a/test/web/activity_pub/activity_pub_test.exs b/test/web/activity_pub/activity_pub_test.exs index 4817a9f38..be81e75aa 100644 --- a/test/web/activity_pub/activity_pub_test.exs +++ b/test/web/activity_pub/activity_pub_test.exs @@ -265,6 +265,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do end end + describe "fetching an object" do + test "it fetches an existing object" do + {:ok, object} = ActivityPub.fetch_object_from_id("http://mastodon.example.org/@admin/99541947525187367") + end + end + describe "following / unfollowing" do test "creates a follow activity" do follower = insert(:user) |