From 4799dc6991682489e8f1701946685a7725ad0a6a Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 15 Apr 2017 14:09:54 +0200 Subject: [PATCH] Add retweeting to TwAPI controller. --- lib/pleroma/web/router.ex | 1 + .../web/twitter_api/twitter_api_controller.ex | 9 +++++++++ .../twitter_api_controller_test.exs | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 7e4866c2f..7bb3c9763 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -43,5 +43,6 @@ def user_fetcher(username) do post "/favorites/create/:id", TwitterAPI.Controller, :favorite post "/favorites/create", TwitterAPI.Controller, :favorite post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite + post "/statuses/retweet/:id", TwitterAPI.Controller, :retweet 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 c71c123b2..a4d8a1d14 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -122,6 +122,15 @@ def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do |> json_reply(200, response) end + def retweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do + activity = Repo.get(Activity, id) + {:ok, status} = TwitterAPI.retweet(user, activity) + response = Poison.encode!(status) + + 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 814f457b9..0eb1b60c5 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -197,6 +197,25 @@ test "with credentials", %{conn: conn, user: current_user} do end end + describe "POST /api/statuses/retweet/:id" do + setup [:valid_user] + test "without valid credentials", %{conn: conn} do + note_activity = insert(:note_activity) + conn = post conn, "/api/statuses/retweet/#{note_activity.id}.json" + assert json_response(conn, 403) == %{"error" => "Invalid credentials."} + end + + test "with credentials", %{conn: conn, user: current_user} do + note_activity = insert(:note_activity) + + conn = conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/statuses/retweet/#{note_activity.id}.json") + + assert json_response(conn, 200) + end + end + defp valid_user(_context) do { :ok, user } = UserBuilder.insert(%{nickname: "lambda", ap_id: "lambda"}) [user: user]