Federate follow salmons.
authorRoger Braun <roger@rogerbraun.net>
Sun, 7 May 2017 12:45:37 +0000 (14:45 +0200)
committerRoger Braun <roger@rogerbraun.net>
Sun, 7 May 2017 12:45:37 +0000 (14:45 +0200)
lib/pleroma/web/ostatus/activity_representer.ex
lib/pleroma/web/twitter_api/twitter_api.ex
test/web/ostatus/activity_representer_test.exs

index 717670852fc37e566dd0013478b64a228616e977..076ead41adff3cd0244312bd1cfb7eb8e469dfbc 100644 (file)
@@ -118,6 +118,34 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
     ] ++ mentions ++ author
   end
 
+  def to_simple_form(%{data: %{"type" => "Follow"}} = 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
+
+    author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
+
+    mentions = activity.data["to"] |> get_mentions
+    [
+      {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
+      {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
+      {:id, h.(activity.data["id"])},
+      {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
+      {:content, [type: 'html'], ['#{user.nickname} started following #{activity.data["object"]}']},
+      {:published, h.(inserted_at)},
+      {:updated, h.(updated_at)},
+      {:"activity:object", [
+        {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
+        {:id, h.(activity.data["object"])},
+        {:uri, h.(activity.data["object"])},
+      ]},
+      {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
+    ] ++ mentions ++ author
+  end
+
   def wrap_with_entry(simple_form) do
     [{
       :entry, [
index 71f0c366e0f602c82123b6db09632fcc306c576b..3921c0d74e75adbb589695f2d8c81887b47896ea 100644 (file)
@@ -139,10 +139,13 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
          {:ok, activity} <- ActivityPub.insert(%{
            "type" => "Follow",
            "actor" => follower.ap_id,
+           "to" => [followed.ap_id],
            "object" => followed.ap_id,
            "published" => make_date()
          })
     do
+      # TODO move all this to ActivityPub
+      Pleroma.Web.Federator.enqueue(:publish, activity)
       {:ok, follower, followed, activity}
     else
       err -> err
index 12c9bbaa25d3b40718fe599968c464d8bdb7a174..af936b57c59f8e5a66406b45976672dc684e4beb 100644 (file)
@@ -124,6 +124,7 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
     user = insert(:user)
     {:ok, like, _note} = ActivityPub.like(user, note)
 
+    # TODO: Are these the correct dates?
     updated_at = like.updated_at
     |> NaiveDateTime.to_iso8601
     inserted_at = like.inserted_at
@@ -155,6 +156,49 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenterTest do
     assert clean(res) == clean(expected)
   end
 
+  test "a follow activity" do
+    follower = insert(:user)
+    followed = insert(:user)
+    {:ok, activity} = ActivityPub.insert(%{
+          "type" => "Follow",
+          "actor" => follower.ap_id,
+          "object" => followed.ap_id,
+          "to" => [followed.ap_id]
+    })
+
+
+    # TODO: Are these the correct dates?
+    updated_at = activity.updated_at
+    |> NaiveDateTime.to_iso8601
+    inserted_at = activity.inserted_at
+    |> NaiveDateTime.to_iso8601
+
+    tuple = ActivityRepresenter.to_simple_form(activity, follower)
+
+    refute is_nil(tuple)
+
+    res = :xmerl.export_simple_content(tuple, :xmerl_xml) |> IO.iodata_to_binary
+
+    expected = """
+    <activity:object-type>http://activitystrea.ms/schema/1.0/activity</activity:object-type>
+    <activity:verb>http://activitystrea.ms/schema/1.0/follow</activity:verb>
+    <id>#{activity.data["id"]}</id>
+    <title>#{follower.nickname} started following #{activity.data["object"]}</title>
+    <content type="html"> #{follower.nickname} started following #{activity.data["object"]}</content>
+    <published>#{inserted_at}</published>
+    <updated>#{updated_at}</updated>
+    <activity:object>
+      <activity:object-type>http://activitystrea.ms/schema/1.0/person</activity:object-type>
+      <id>#{activity.data["object"]}</id>
+      <uri>#{activity.data["object"]}</uri>
+    </activity:object>
+    <link rel="self" type="application/atom+xml" href="#{activity.data["id"]}"/>
+    <link rel="mentioned" ostatus:object-type="http://activitystrea.ms/schema/1.0/person" href="#{activity.data["object"]}"/>
+    """
+
+    assert clean(res) == clean(expected)
+  end
+
   test "an unknown activity" do
     tuple = ActivityRepresenter.to_simple_form(%Activity{}, nil)
     assert is_nil(tuple)