Add friends status fetching.
authorRoger Braun <roger@rogerbraun.net>
Wed, 22 Mar 2017 15:51:20 +0000 (16:51 +0100)
committerRoger Braun <roger@rogerbraun.net>
Wed, 22 Mar 2017 15:51:20 +0000 (16:51 +0100)
lib/pleroma/web/twitter_api/representers/activity_representer.ex
lib/pleroma/web/twitter_api/twitter_api.ex
test/support/builders/user_builder.ex
test/web/activity_pub/activity_pub_test.exs
test/web/twitter_api/representers/activity_representer_test.exs
test/web/twitter_api/twitter_api_test.exs

index 32871c0deb3303961549c97b99a7030dd7029431..1718781625f7c55b92f21f92e21cb13e7473e4c3 100644 (file)
@@ -1,9 +1,11 @@
 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
   use Pleroma.Web.TwitterAPI.Representers.BaseRepresenter
   alias Pleroma.Web.TwitterAPI.Representers.UserRepresenter
+  alias Pleroma.Activity
 
-  def to_map(activity, %{user: user}) do
+  def to_map(%Activity{} = activity, %{user: user}) do
     content = get_in(activity.data, ["object", "content"])
+    published = get_in(activity.data, ["object", "published"])
     %{
       "id" => activity.id,
       "user" => UserRepresenter.to_map(user),
@@ -11,7 +13,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter do
       "statusnet_html" => content,
       "text" => content,
       "is_local" => true,
-      "is_post_verb" => true
+      "is_post_verb" => true,
+      "created_at" => published
     }
   end
 end
index da707dd6ebd05348234af6c3e1a29b510776977f..7e0ca42338a896f3442c86fd9453d1da2fac8725 100644 (file)
@@ -5,6 +5,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
 
   def create_status(user = %User{}, data = %{}) do
+    date = DateTime.utc_now() |> DateTime.to_iso8601
     activity = %{
       "type" => "Create",
       "to" => [
@@ -14,16 +15,26 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
       "actor" => User.ap_id(user),
       "object" => %{
         "type" => "Note",
-        "content" => data["status"]
-      }
+        "content" => data["status"],
+        "published" => date
+      },
+      "published" => date
     }
 
     ActivityPub.insert(activity)
   end
 
+  def fetch_friend_statuses(user, opts \\ %{}) do
+    ActivityPub.fetch_activities(user.following, opts)
+    |> activities_to_statuses
+  end
+
   def fetch_public_statuses(opts \\ %{}) do
-    activities = ActivityPub.fetch_public_activities(opts)
+    ActivityPub.fetch_public_activities(opts)
+    |> activities_to_statuses
+  end
 
+  defp activities_to_statuses(activities) do
     Enum.map(activities, fn(activity) ->
       actor = get_in(activity.data, ["actor"])
       user = Repo.get_by!(User, ap_id: actor)
index 0028d42e0cadff4477dd7a449be3d742941216da..710a1b87cbdd5735681b44894e4184718bfa9882 100644 (file)
@@ -1,8 +1,8 @@
 defmodule Pleroma.Builders.UserBuilder do
   alias Pleroma.{User, Repo}
 
-  def build do
-    %User{
+  def build(data \\ %{}) do
+    user = %User{
       email: "test@example.org",
       name: "Test Name",
       nickname: "testname",
@@ -10,9 +10,10 @@ defmodule Pleroma.Builders.UserBuilder do
       bio: "A tester.",
       ap_id: "some id"
     }
+    Map.merge(user, data)
   end
 
-  def insert do
-    Repo.insert(build())
+  def insert(data \\ %{}) do
+    Repo.insert(build(data))
   end
 end
index 754bd72f3151e08292a0195e35e967f8b5e2e26c..d220f91c38c7efbd40efd23d2cb367fc55293578 100644 (file)
@@ -20,7 +20,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
     test "retrieve the activities for certain recipients" do
       {:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
       {:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
-      {:ok, activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
+      {:ok, _activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
 
       activities = ActivityPub.fetch_activities(["someone", "someone_else"])
       assert length(activities) == 2
index b34d3b7875f46a3bfdf859670999cc40aa7171a0..ecfd5a73ffb913a6724dfe1b65757910b1fb1d18 100644 (file)
@@ -7,6 +7,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
   test "an activity" do
     {:ok, user} = UserBuilder.insert
     content = "Some content"
+    date = DateTime.utc_now() |> DateTime.to_iso8601
+
     activity = %Activity{
       id: 1,
       data: %{
@@ -17,9 +19,11 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
         ],
         "actor" => User.ap_id(user),
         "object" => %{
+          "published" => date,
           "type" => "Note",
           "content" => content
-        }
+        },
+        "published" => date
       }
     }
 
@@ -31,7 +35,8 @@ defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
       "attentions" => [],
       "statusnet_html" => content,
       "text" => content,
-      "is_post_verb" => true
+      "is_post_verb" => true,
+      "created_at" => date
     }
 
     assert ActivityRepresenter.to_map(activity, %{user: user}) == expected_status
index 8016f45379235dc2788052bda43f4716006cd471..5c78790af92372079e7708b88f1c3bb12313a6f9 100644 (file)
@@ -20,11 +20,24 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
     assert Enum.member?(get_in(activity.data, ["to"]), "https://www.w3.org/ns/activitystreams#Public")
   end
 
-  test "fetch public activities" do
+  test "fetch public statuses" do
     %{ public: activity, user: user } = ActivityBuilder.public_and_non_public
     statuses = TwitterAPI.fetch_public_statuses()
 
     assert length(statuses) == 1
     assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: user})
   end
+
+  test "fetch friends' statuses" do
+    ActivityBuilder.public_and_non_public
+    {:ok, activity} = ActivityBuilder.insert(%{"to" => ["someguy/followers"]})
+    {:ok, user} = UserBuilder.insert(%{ap_id: "some other id", following: ["someguy/followers"]})
+
+    statuses = TwitterAPI.fetch_friend_statuses(user)
+
+    activity_user = Repo.get_by(User, ap_id: activity.data["actor"])
+
+    assert length(statuses) == 1
+    assert Enum.at(statuses, 0) == ActivityRepresenter.to_map(activity, %{user: activity_user})
+  end
 end