From d2cdcc097b171461687df1613371c4b04da60186 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Tue, 28 Mar 2017 17:22:44 +0200 Subject: [PATCH] Add conversation fetching to TwAPI controller. --- lib/pleroma/web/router.ex | 1 + lib/pleroma/web/twitter_api/twitter_api.ex | 31 +++++++++++++++---- .../web/twitter_api/twitter_api_controller.ex | 9 ++++++ .../twitter_api_controller_test.exs | 16 ++++++++++ test/web/twitter_api/twitter_api_test.exs | 13 ++++++++ 5 files changed, 64 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index d9a3968a7..c60107072 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -24,6 +24,7 @@ def user_fetcher(username) 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 diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 07893e565..e240dc2ea 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -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 @@ def fetch_public_statuses(user, opts \\ %{}) 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 @@ defp activity_to_status(activity, opts) 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 diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 2a49b6270..f42cee0b0 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -57,6 +57,15 @@ def fetch_status(%{assigns: %{user: user}} = conn, %{ "id" => id }) 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") diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 0e7328613..5aad12593 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -67,6 +67,22 @@ test "returns one status", %{conn: conn} 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 diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 381a9fa5b..5325110e1 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -103,4 +103,17 @@ test "Unfollow another user" 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