From c83f279a7d1d9061dff6fb2f3f36740abad2e85c Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Fri, 14 Apr 2017 18:27:17 +0200 Subject: [PATCH] Add unliking to TwAPI controller. --- lib/pleroma/web/router.ex | 1 + .../web/twitter_api/twitter_api_controller.ex | 9 +++++++ .../twitter_api_controller_test.exs | 24 ++++++++++++++++++- test/web/twitter_api/twitter_api_test.exs | 3 +-- 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index e11086538..7e4866c2f 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -42,5 +42,6 @@ defmodule Pleroma.Web.Router do post "/media/upload", TwitterAPI.Controller, :upload_json post "/favorites/create/:id", TwitterAPI.Controller, :favorite post "/favorites/create", TwitterAPI.Controller, :favorite + post "/favorites/destroy/:id", TwitterAPI.Controller, :unfavorite 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 f66238e0a..c71c123b2 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -113,6 +113,15 @@ defmodule Pleroma.Web.TwitterAPI.Controller do |> json_reply(200, response) end + def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do + activity = Repo.get(Activity, id) + {:ok, status} = TwitterAPI.unfavorite(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 a4128f442..814f457b9 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -2,7 +2,8 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do use Pleroma.Web.ConnCase alias Pleroma.Web.TwitterAPI.Representers.{UserRepresenter, ActivityRepresenter} alias Pleroma.Builders.{ActivityBuilder, UserBuilder} - alias Pleroma.{Repo, Activity, User} + alias Pleroma.{Repo, Activity, User, Object} + alias Pleroma.Web.ActivityPub.ActivityPub import Pleroma.Factory @@ -175,6 +176,27 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do end end + describe "POST /api/favorites/destroy/:id" do + setup [:valid_user] + test "without valid credentials", %{conn: conn} do + note_activity = insert(:note_activity) + conn = post conn, "/api/favorites/destroy/#{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) + object = Object.get_by_ap_id(note_activity.data["object"]["id"]) + ActivityPub.like(current_user, object) + + conn = conn + |> with_credentials(current_user.nickname, "test") + |> post("/api/favorites/destroy/#{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] diff --git a/test/web/twitter_api/twitter_api_test.exs b/test/web/twitter_api/twitter_api_test.exs index 88e6dd684..36d4f4012 100644 --- a/test/web/twitter_api/twitter_api_test.exs +++ b/test/web/twitter_api/twitter_api_test.exs @@ -198,12 +198,11 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"]) object = Object.get_by_ap_id(note_activity.data["object"]["id"]) - {:ok, like_activity, object } = ActivityPub.like(user, object) + {:ok, _like_activity, _object } = ActivityPub.like(user, object) updated_activity = Activity.get_by_ap_id(note_activity.data["id"]) assert ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})["fave_num"] == 1 {:ok, status} = TwitterAPI.unfavorite(user, note_activity) - updated_activity = Activity.get_by_ap_id(note_activity.data["id"]) assert status["fave_num"] == 0 end