Add an ostatus representer for like activities.
authorRoger Braun <roger@rogerbraun.net>
Tue, 2 May 2017 14:35:53 +0000 (16:35 +0200)
committerRoger Braun <roger@rogerbraun.net>
Tue, 2 May 2017 14:35:53 +0000 (16:35 +0200)
lib/pleroma/web/ostatus/activity_representer.ex
test/web/ostatus/activity_representer_test.exs

index c64bb3a3bbcdf65852da48ab0b25a1e4afebfd2e..cf6aae727c54a25059b318557b24d462f2c9859b 100644 (file)
@@ -10,11 +10,17 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
   defp get_in_reply_to(_), do: []
 
   defp get_mentions(to) do
-    Enum.map(to, fn
-      ("https://www.w3.org/ns/activitystreams#Public") ->
-        {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection", href: "http://activityschema.org/collection/public"], []}
-      (id) ->
-        {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
+    Enum.map(to, fn (id) ->
+      cond do
+        # Special handling for the AP/Ostatus public collections
+        "https://www.w3.org/ns/activitystreams#Public" == id ->
+          {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection", href: "http://activityschema.org/collection/public"], []}
+        # Ostatus doesn't handle follower collections, ignore these.
+        Regex.match?(~r/^#{Pleroma.Web.base_url}.+followers$/, id) ->
+          []
+        true ->
+          {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
+      end
     end)
   end
 
@@ -49,6 +55,35 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
     ] ++ attachments ++ in_reply_to ++ author ++ mentions
   end
 
+  def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
+    h = fn(str) -> [to_charlist(str)] end
+
+    updated_at = activity.updated_at
+    |> NaiveDateTime.to_iso8601
+    inserted_at = activity.inserted_at
+    |> NaiveDateTime.to_iso8601
+
+    in_reply_to = get_in_reply_to(activity.data)
+    author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
+    mentions = activity.data["to"] |> get_mentions
+
+    [
+      {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
+      {:id, h.(activity.data["id"])},
+      {:title, ['New favorite by #{user.nickname}']},
+      {:content, [type: 'html'], ['#{user.nickname} favorited something']},
+      {:published, h.(inserted_at)},
+      {:updated, h.(updated_at)},
+      {:"activity:object", [
+        {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
+        {:id, h.(activity.data["object"])}, # For notes, federate the object id.
+      ]},
+      {:"ostatus:conversation", [], h.(activity.data["context"])},
+      {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
+      {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
+    ] ++ author ++ mentions
+  end
+
   def wrap_with_entry(simple_form) do
     [{
       :entry, [
index 439c733d7e33e17a0f0978bfa9001cbf08b4ca58..4cf73427b6de55ac8d295c2d1b9c2a3f6ff96e95 100644 (file)
@@ -3,6 +3,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
 
   alias Pleroma.Web.OStatus.ActivityRepresenter
   alias Pleroma.{User, Activity}
+  alias Pleroma.Web.ActivityPub.ActivityPub
 
   import Pleroma.Factory
 
@@ -72,6 +73,41 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
     assert clean(res) == clean(expected)
   end
 
+  test "a like activity" do
+    note = insert(:note)
+    user = insert(:user)
+    {:ok, like, _note} = ActivityPub.like(user, note)
+
+    updated_at = like.updated_at
+    |> NaiveDateTime.to_iso8601
+    inserted_at = like.inserted_at
+    |> NaiveDateTime.to_iso8601
+
+    tuple = ActivityRepresenter.to_simple_form(like, user)
+    refute is_nil(tuple)
+
+    res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
+
+    expected = """
+    <activity:verb>http://activitystrea.ms/schema/1.0/favorite</activity:verb>
+    <id>#{like.data["id"]}</id>
+    <title>New favorite by #{user.nickname}</title>
+    <content type="html">#{user.nickname} favorited something</content>
+    <published>#{inserted_at}</published>
+    <updated>#{updated_at}</updated>
+    <activity:object>
+      <activity:object-type>http://activitystrea.ms/schema/1.0/note</activity:object-type>
+      <id>#{note.data["id"]}</id>
+    </activity:object>
+    <ostatus:conversation>#{like.data["context"]}</ostatus:conversation>
+    <link href="#{like.data["context"]}" rel="ostatus:conversation" />
+    <thr:in-reply-to ref="#{note.data["id"]}" />
+    <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{note.data["actor"]}"/>
+    """
+
+    assert clean(res) == clean(expected)
+  end
+
   test "an unknown activity" do
     tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
     assert is_nil(tuple)