Add unliking to TwAPI controller.
authorRoger Braun <roger@rogerbraun.net>
Fri, 14 Apr 2017 16:27:17 +0000 (18:27 +0200)
committerRoger Braun <roger@rogerbraun.net>
Fri, 14 Apr 2017 16:27:17 +0000 (18:27 +0200)
lib/pleroma/web/router.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 e11086538390379a457d2c3a1de04937edb6e8ae..7e4866c2f2ab3e728215614d8c5e8a6aa836e6be 100644 (file)
@@ -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
index f66238e0a9d1d3c115fb677ba978f92e3a68c0ef..c71c123b21c276cc5f4c62f704e302d7d44fd4e6 100644 (file)
@@ -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")
index a4128f4420ad4483af214126c71f7098f7dcdbe2..814f457b98037a95c661e0e3b4bccb0e21a42715 100644 (file)
@@ -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]
index 88e6dd6841bde419abc97f016ce687aceed3e6f5..36d4f40122ebeaff3fc4288f09acef6fa12405d0 100644 (file)
@@ -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