Add conversation fetching to TwAPI controller.
authorRoger Braun <roger@rogerbraun.net>
Tue, 28 Mar 2017 15:22:44 +0000 (17:22 +0200)
committerRoger Braun <roger@rogerbraun.net>
Tue, 28 Mar 2017 15:22:44 +0000 (17:22 +0200)
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/twitter_api/twitter_api_controller_test.exs
test/web/twitter_api/twitter_api_test.exs

index d9a3968a7c85cd42335a6b822f59b26b3bd46344..c60107072a2326b633b07f0c1147eb3e60785343 100644 (file)
@@ -24,6 +24,7 @@ defmodule Pleroma.Web.Router do
     get "/statuses/public_timeline", TwitterAPI.Controller, :public_timeline
     get "/statuses/public_and_external_timeline", TwitterAPI.Controller, :public_timeline
     get "/statuses/show/:id", TwitterAPI.Controller, :fetch_status
+    get "/statusnet/conversation/:id", TwitterAPI.Controller, :fetch_conversation
   end
 
   scope "/api", Pleroma.Web do
index 07893e5650335cf253679fbefb1d3fa0d1e8c7c1..e240dc2ea696933699c756b9d47ba6efa399a94d 100644 (file)
@@ -3,6 +3,8 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
 
+  import Ecto.Query
+
   def create_status(user = %User{}, data = %{}) do
     date = DateTime.utc_now() |> DateTime.to_iso8601
 
@@ -55,6 +57,29 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     |> activities_to_statuses(%{for: user})
   end
 
+  def fetch_conversation(user, id) do
+    query = from activity in Activity,
+      where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
+      limit: 1
+
+    with %Activity{} = activity <- Repo.one(query),
+         context <- activity.data["context"],
+         activities <- ActivityPub.fetch_activities_for_context(context),
+         statuses <- activities |> activities_to_statuses(%{for: user})
+    do
+      statuses
+    else e ->
+        IO.inspect(e)
+      []
+    end
+  end
+
+  def fetch_status(user, id) do
+    with %Activity{} = activity <- Repo.get(Activity, id) do
+      activity_to_status(activity, %{for: user})
+    end
+  end
+
   def follow(%User{} = follower, followed_id) do
     with %User{} = followed <- Repo.get(User, followed_id),
          { :ok, follower } <- User.follow(follower, followed)
@@ -95,10 +120,4 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     user = Repo.get_by!(User, ap_id: actor)
     ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
   end
-
-  def fetch_status(user, id) do
-    with %Activity{} = activity <- Repo.get(Activity, id) do
-      activity_to_status(activity, %{for: user})
-    end
-  end
 end
index 2a49b6270b2c9e6544a1a3e6e55205c480625c15..f42cee0b09555335cb60a94e00eac892f19b88b3 100644 (file)
@@ -57,6 +57,15 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     |> json_reply(200, response)
   end
 
+  def fetch_conversation(%{assigns: %{user: user}} = conn, %{ "id" => id }) do
+    id = String.to_integer(id)
+    response = TwitterAPI.fetch_conversation(user, id) |> Poison.encode!
+
+    conn
+    |> json_reply(200, response)
+  end
+
+
   defp json_reply(conn, status, json) do
     conn
     |> put_resp_content_type("application/json")
index 0e732861352687c52545274bc1ac4cecf514d330..5aad12593e3e0ac4d87a90edad6410aca04d4593 100644 (file)
@@ -67,6 +67,22 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
   end
 
+  describe "GET /statusnet/conversation/:id.json" do
+    test "returns the statuses in the conversation", %{conn: conn} do
+      {:ok, _user} = UserBuilder.insert
+      {:ok, _activity} = ActivityBuilder.insert(%{"statusnetConversationId" => 1, "context" => "2hu"})
+      {:ok, _activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"})
+      {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
+
+      conn = conn
+      |> get("/api/statusnet/conversation/1.json")
+
+      response = json_response(conn, 200)
+
+      assert length(response) == 2
+    end
+  end
+
   describe "GET /statuses/friends_timeline.json" do
     setup [:valid_user]
     test "without valid credentials", %{conn: conn} do
index 381a9fa5b4c927df52087d6d83f612c7eb63b746..5325110e142ff346e4631d10afe8d1d15196675e 100644 (file)
@@ -103,4 +103,17 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
 
     assert user.following == []
   end
+
+  test "fetch statuses in a context using the conversation id" do
+    {:ok, user} = UserBuilder.insert()
+    {:ok, activity} = ActivityBuilder.insert(%{"statusnetConversationId" => 1, "context" => "2hu"})
+    {:ok, activity_two} = ActivityBuilder.insert(%{"statusnetConversationId" => 1,"context" => "2hu"})
+    {:ok, _activity_three} = ActivityBuilder.insert(%{"context" => "3hu"})
+
+    statuses = TwitterAPI.fetch_conversation(user, 1)
+
+    assert length(statuses) == 2
+    assert Enum.at(statuses, 0)["id"] == activity.id
+    assert Enum.at(statuses, 1)["id"] == activity_two.id
+  end
 end