Handle unrepeats via the TwitterAPI
authorFrancis Dinh <normandy@firemail.cc>
Wed, 18 Apr 2018 10:00:40 +0000 (06:00 -0400)
committerFrancis Dinh <normandy@firemail.cc>
Wed, 18 Apr 2018 10:00:40 +0000 (06:00 -0400)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.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

index 3b918e28f236249123b2a1128ca6a1a3fae44ba3..dccd2e7577c2c8c97497182bc03915ba2f800a1f 100644 (file)
@@ -146,7 +146,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
          {:ok, unannounce_activity} <- insert(unannounce_data, local),
          {:ok, _activity} <- Repo.delete(activity),
          {:ok, object} <- remove_announce_from_object(activity, object) do
-      {:ok, unannounce_activity, object}
+      {:ok, unannounce_activity, activity, object}
     else
       _e -> {:ok, object}
     end
index c01552410f446b738dbd11e896cf45743adbddf5..ebd587d26ed4f669edd4a123422379ad30277191 100644 (file)
@@ -297,7 +297,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, _, _, %{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 c12cd7f8aa14a8912e89b69e04609a9e31897b38..b6ae7c7f70f285ee01c2e1d8168eeb4b51e02ab6 100644 (file)
@@ -12,6 +12,18 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
     CommonAPI.post(user, data)
   end
 
+  def delete(%User{} = user, id) do
+    # TwitterAPI does not have an "unretweet" endpoint; instead this is done
+    # via the "destroy" endpoint.  Therefore, there is a 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
+    end
+  end
+
   def follow(%User{} = follower, params) do
     with {:ok, %User{} = followed} <- get_user(params),
          {:ok, follower} <- User.follow(follower, followed),
@@ -64,6 +76,12 @@ 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
+      {:ok, activity}
+    end
+  end
+
   def fav(%User{} = user, ap_id_or_id) do
     with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
          %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
index 986436326ce8c096795a39f3c263b2e9b686c16c..e1c1cb5d49deb6332506de23a753a422b213b3ce 100644 (file)
@@ -150,8 +150,8 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
   end
 
   def delete_post(%{assigns: %{user: user}} = conn, %{"id" => id}) do
-    with {:ok, delete} <- CommonAPI.delete(id, user) do
-      render(conn, ActivityView, "activity.json", %{activity: delete, for: user})
+    with {:ok, activity} <- TwitterAPI.delete(id, user) do
+      render(conn, ActivityView, "activity.json", %{activity: activity, for: user})
     end
   end
 
@@ -229,12 +229,6 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     end
   end
 
-  def unretweet(%{assigns: %{user: user}} = conn, %{"id" => id}) do
-    with {: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 85a6aecf08ad307829342dc8b3c4d9cc31c5483c..6a07da775242c545488b58134233da9456e9c48b 100644 (file)
@@ -284,9 +284,11 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubTest do
       {:ok, announce_activity, object} = ActivityPub.announce(user, object)
       assert object.data["announcement_count"] == 1
 
-      {:ok, unannounce_activity, object} = ActivityPub.unannounce(user, object)
+      {:ok, unannounce_activity, 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),
                note_activity.data["actor"]