Display html links correctly for remote activies.
authorRoger Braun <roger@rogerbraun.net>
Tue, 1 Aug 2017 12:05:18 +0000 (14:05 +0200)
committerRoger Braun <roger@rogerbraun.net>
Tue, 1 Aug 2017 12:05:18 +0000 (14:05 +0200)
lib/pleroma/web/ostatus/activity_representer.ex
test/web/ostatus/activity_representer_test.exs

index 03e3d26bdbc804a65cf31e8c22a0da46ea0befc8..a129fac23c5a1b28e86f3bf00cbe766eca34293f 100644 (file)
@@ -24,6 +24,29 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
     end)
   end
 
+  defp get_links(%{local: true, data: data}) do
+    h = fn(str) -> [to_charlist(str)] end
+    [
+      {:link, [type: ['application/atom+xml'], href: h.(data["object"]["id"]), rel: 'self'], []},
+      {:link, [type: ['text/html'], href: h.(data["object"]["id"]), rel: 'alternate'], []}
+    ]
+  end
+
+  defp get_links(%{local: false,
+                   data: %{
+                     "object" => %{
+                       "external_url" => external_url
+                     }
+                   }}) do
+
+    h = fn(str) -> [to_charlist(str)] end
+    [
+      {:link, [type: ['text/html'], href: h.(external_url), rel: 'alternate'], []}
+    ]
+  end
+
+  defp get_links(_activity), do: []
+
   def to_simple_form(activity, user, with_author \\ false)
   def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
     h = fn(str) -> [to_charlist(str)] end
@@ -53,9 +76,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
       {:updated, h.(updated_at)},
       {:"ostatus:conversation", [], h.(activity.data["context"])},
       {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
-      {:link, [type: ['application/atom+xml'], href: h.(activity.data["object"]["id"]), rel: 'self'], []},
-      {:link, [type: ['text/html'], href: h.(activity.data["object"]["id"]), rel: 'alternate'], []}
-    ] ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions
+    ] ++ get_links(activity) ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions
   end
 
   def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
index c706c1e07c2f2e2f1843a7d83cac4fd3eab0b285..171a8bae75e6fe20cc3d5df42592df50de9e26e8 100644 (file)
@@ -4,9 +4,23 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
   alias Pleroma.Web.OStatus.ActivityRepresenter
   alias Pleroma.{User, Activity, Object}
   alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Web.OStatus
 
   import Pleroma.Factory
 
+  test "an external note activity" do
+    incoming = File.read!("test/fixtures/mastodon-note-cw.xml")
+    {:ok, [activity]} = OStatus.handle_incoming(incoming)
+
+    user = User.get_cached_by_ap_id(activity.data["actor"])
+
+    tuple = ActivityRepresenter.to_simple_form(activity, user)
+
+    res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
+
+    assert String.contains?(res, ~s{<link type="text/html" href="https://mastodon.social/users/lambadalambda/updates/2314748" rel="alternate"/>})
+  end
+
   test "a note activity" do
     note_activity = insert(:note_activity)