activitypub transmogrifier: rewrite non-http URLs using the object's external URL
authorWilliam Pitcock <nenolod@dereferenced.org>
Tue, 13 Mar 2018 17:46:37 +0000 (17:46 +0000)
committerlain <lain@soykaf.club>
Wed, 14 Mar 2018 07:53:29 +0000 (08:53 +0100)
Signed-off-by: lain <lain@soykaf.club>
lib/pleroma/web/activity_pub/transmogrifier.ex
test/web/activity_pub/transmogrifier_test.exs

index 8b1a26ae5110e67a404d55d4e72654ab0d762b16..d759ca2b2faf21a65074a0a09d7fac1cc21dc6b4 100644 (file)
@@ -210,11 +210,31 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
 
   def prepare_outgoing(%{"type" => type} = data) do
     data = data
+    |> maybe_fix_object_url
     |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
 
     {:ok, data}
   end
 
+  def maybe_fix_object_url(data) do
+    if is_binary(data["object"]) and not String.starts_with?(data["object"], "http") do
+      case ActivityPub.fetch_object_from_id(data["object"]) do
+        {:ok, relative_object} ->
+          if relative_object.data["external_url"] do
+            data = data
+            |> Map.put("object", relative_object.data["external_url"])
+          else
+            data
+          end
+        e ->
+          Logger.error("Couldn't fetch #{data["object"]} #{inspect(e)}")
+          data
+      end
+    else
+      data
+    end
+  end
+
   def add_hashtags(object) do
     tags = (object["tag"] || [])
     |> Enum.map fn (tag) -> %{"href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}", "name" => "##{tag}", "type" => "Hashtag"} end
index c0ee209f1c84979557cbf018825a3c7859a3fb85..51b09b166eb7f1f3cc309434478fb7fc59a8a1ae 100644 (file)
@@ -1,6 +1,7 @@
 defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
   use Pleroma.DataCase
   alias Pleroma.Web.ActivityPub.Transmogrifier
+  alias Pleroma.Web.OStatus
   alias Pleroma.Activity
   alias Pleroma.User
   alias Pleroma.Repo
@@ -220,6 +221,18 @@ defmodule Pleroma.Web.ActivityPub.TransmogrifierTest do
 
       assert modified["object"]["actor"] == modified["object"]["attributedTo"]
     end
+
+    test "it translates ostatus IDs to external URLs" do
+      incoming = File.read!("test/fixtures/incoming_note_activity.xml")
+      {:ok, [referent_activity]} = OStatus.handle_incoming(incoming)
+
+      user = insert(:user)
+
+      {:ok, activity, _} = CommonAPI.favorite(referent_activity.id, user)
+      {:ok, modified} = Transmogrifier.prepare_outgoing(activity.data)
+
+      assert modified["object"] == "http://gs.example.org:4040/index.php/notice/29"
+    end
   end
 
   describe "user upgrade" do