Add unretweet TwAPI endpoint and cleanup AP.unannounce
authorFrancis Dinh <normandy@firemail.cc>
Thu, 14 Jun 2018 01:29:55 +0000 (21:29 -0400)
committerFrancis Dinh <normandy@firemail.cc>
Thu, 14 Jun 2018 01:33:36 +0000 (21:33 -0400)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/activity_pub/transmogrifier.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/activity_pub/activity_pub_test.exs
test/web/twitter_api/twitter_api_controller_test.exs
test/web/twitter_api/twitter_api_test.exs

index 4e0be5ba2802143daa32350b2d971ccc6111e650..70e8dfd92f4f88511cb78797c344ff4fd73d9d47 100644 (file)
@@ -198,7 +198,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
          :ok <- maybe_federate(unannounce_activity),
          {:ok, _activity} <- Repo.delete(announce_activity),
          {:ok, object} <- remove_announce_from_object(announce_activity, object) do
-      {:ok, unannounce_activity, announce_activity, object}
+      {:ok, unannounce_activity, object}
     else
       _e -> {:ok, object}
     end
index 3c9377be96873e1a0088c2812d163e7cfa2e6fba..053484f6fc3aecb22431626359c81405a89248c0 100644 (file)
@@ -303,7 +303,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
          {:ok, object} <-
            get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
-         {:ok, activity, _, _} <- ActivityPub.unannounce(actor, object, id, false) do
+         {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
       {:ok, activity}
     else
       _e -> :error
index 4252ac2fe2f0bce288bfb181901c2b1086277c6e..60f67a8c1878d89017343bdd62931c0658fd5564 100644 (file)
@@ -336,7 +336,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
   end
 
   def unreblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
-    with {:ok, _, _, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
+    with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
          %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
       render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
     end
index 924254895030a87bd512c8c53d18dd778a4ab19f..95627f8fdc408d21812ca006eecb9c16f8bbf8da 100644 (file)
@@ -238,6 +238,7 @@ defmodule Pleroma.Web.Router do
 
     post("/statuses/update", TwitterAPI.Controller, :status_update)
     post("/statuses/retweet/:id", TwitterAPI.Controller, :retweet)
+    post("/statuses/unretweet/:id", TwitterAPI.Controller, :unretweet)
     post("/statuses/destroy/:id", TwitterAPI.Controller, :delete_post)
 
     post("/friendships/create", TwitterAPI.Controller, :follow)
index ccc6fe8e7be07fa7ccdb902cd20979e8c1ce6e10..c23b3c2c4fee8f3ab17c1b93633823c2099bf63e 100644 (file)
@@ -12,14 +12,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
   end
 
   def delete(%User{} = user, id) do
-    # TwitterAPI does not have an "unretweet" endpoint; instead this is done
-    # via the "destroy" endpoint.  Therefore, we need to handle
-    # when the status to "delete" is actually an Announce (repeat) object.
-    with %Activity{data: %{"type" => type}} <- Repo.get(Activity, id) do
-      case type do
-        "Announce" -> unrepeat(user, id)
-        _ -> CommonAPI.delete(id, user)
-      end
+    with %Activity{data: %{"type" => type}} <- Repo.get(Activity, id),
+         {:ok, activity} <- CommonAPI.delete(id, user) do
+      {:ok, activity}
     end
   end
 
@@ -70,8 +65,9 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     end
   end
 
-  defp unrepeat(%User{} = user, ap_id_or_id) do
-    with {:ok, _unannounce, activity, _object} <- CommonAPI.unrepeat(ap_id_or_id, user) do
+  def unrepeat(%User{} = user, ap_id_or_id) do
+    with {:ok, _unannounce, %{data: %{"id" => id}}} <- CommonAPI.unrepeat(ap_id_or_id, user),
+         %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
       {:ok, activity}
     end
   end
index d53dd0c44897f6adb5c794dbe61be7a4e5386d19..56690cd6bb33a461b7a0debf244ccd2b0973411d 100644 (file)
@@ -240,6 +240,13 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     end
   end
 
+  def unretweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
+    with {_, {:ok, id}} <- {:param_cast, Ecto.Type.cast(:integer, id)},
+         {:ok, activity} <- TwitterAPI.unrepeat(user, id) do
+      render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
+    end
+  end
+
   def register(conn, params) do
     with {:ok, user} <- TwitterAPI.register_user(params) do
       render(conn, UserView, "show.json", %{user: user})
index 081c202b14d5616f8102f9fc3ea4d995e7e38585..bc33b4dfca49c1fd0362ac8b8603b99372c2b2a5 100644 (file)
@@ -318,11 +318,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       {:ok, announce_activity, object} = ActivityPub.announce(user, object)
       assert object.data["announcement_count"] == 1
 
-      {:ok, unannounce_activity, activity, object} = ActivityPub.unannounce(user, object)
+      {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
       assert object.data["announcement_count"] == 0
 
-      assert activity == announce_activity
-
       assert unannounce_activity.data["to"] == [
                User.ap_followers(user),
                announce_activity.data["actor"]
index 68f4331dfbda7fc6ec64ca4584bf0e58638d03f9..6866a362f3bd0f4e85fad1c5499cdd4e5683b4b1 100644 (file)
@@ -580,6 +580,40 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
     end
   end
 
+  describe "POST /api/statuses/unretweet/: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)
+
+      request_path = "/api/statuses/retweet/#{note_activity.id}.json"
+
+      _response =
+        conn
+        |> with_credentials(current_user.nickname, "test")
+        |> post(request_path)
+
+      request_path = String.replace(request_path, "retweet", "unretweet")
+
+      response =
+        conn
+        |> with_credentials(current_user.nickname, "test")
+        |> post(request_path)
+
+      activity = Repo.get(Activity, note_activity.id)
+      activity_user = Repo.get_by(User, ap_id: note_activity.data["actor"])
+
+      assert json_response(response, 200) ==
+               ActivityRepresenter.to_map(activity, %{user: activity_user, for: current_user})
+    end
+  end
+
   describe "POST /api/account/register" do
     test "it creates a new user", %{conn: conn} do
       data = %{
index edacb312d142a3a1ace59bfee9356ee9dd7e46bd..06c1ba6ec8ca71ced6a63163dbe64aa21eaa93e2 100644 (file)
@@ -228,6 +228,17 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPITest do
     assert status == updated_activity
   end
 
+  test "it unretweets an already retweeted status" do
+    user = insert(:user)
+    note_activity = insert(:note_activity)
+
+    {:ok, _status} = TwitterAPI.repeat(user, note_activity.id)
+    {:ok, status} = TwitterAPI.unrepeat(user, note_activity.id)
+    updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
+
+    assert status == updated_activity
+  end
+
   test "it registers a new user and returns the user." do
     data = %{
       "nickname" => "lain",