CommonAPI: Don't error out on double favs/repeats
authorlain <lain@soykaf.club>
Mon, 20 Jan 2020 13:27:59 +0000 (14:27 +0100)
committerlain <lain@soykaf.club>
Mon, 20 Jan 2020 13:27:59 +0000 (14:27 +0100)
lib/pleroma/web/common_api/common_api.ex
test/web/common_api/common_api_test.exs
test/web/mastodon_api/controllers/status_controller_test.exs

index 2f3bcfc3c979cd24af1070a7138230c009e0d3b5..c05a6c544f3f3eb2ff7578ceda844224c8289c9e 100644 (file)
@@ -85,9 +85,13 @@ defmodule Pleroma.Web.CommonAPI do
   def repeat(id_or_ap_id, user, params \\ %{}) do
     with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
          object <- Object.normalize(activity),
-         nil <- Utils.get_existing_announce(user.ap_id, object),
+         announce_activity <- Utils.get_existing_announce(user.ap_id, object),
          public <- public_announce?(object, params) do
-      ActivityPub.announce(user, object, nil, true, public)
+      if announce_activity do
+        {:ok, announce_activity, object}
+      else
+        ActivityPub.announce(user, object, nil, true, public)
+      end
     else
       _ -> {:error, dgettext("errors", "Could not repeat")}
     end
@@ -105,8 +109,12 @@ defmodule Pleroma.Web.CommonAPI do
   def favorite(id_or_ap_id, user) do
     with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
          object <- Object.normalize(activity),
-         nil <- Utils.get_existing_like(user.ap_id, object) do
-      ActivityPub.like(user, object)
+         like_activity <- Utils.get_existing_like(user.ap_id, object) do
+      if like_activity do
+        {:ok, like_activity, object}
+      else
+        ActivityPub.like(user, object)
+      end
     else
       _ -> {:error, dgettext("errors", "Could not favorite")}
     end
index b5d6d40558441c250331f81b3bcfed0c131fb43d..f8963e42e90a9c41666fc78f4f0905b27e736310 100644 (file)
@@ -284,22 +284,22 @@ defmodule Pleroma.Web.CommonAPITest do
       {:ok, %Activity{}, _} = CommonAPI.favorite(activity.id, user)
     end
 
-    test "retweeting a status twice returns an error" do
+    test "retweeting a status twice returns the status" do
       user = insert(:user)
       other_user = insert(:user)
 
       {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
-      {:ok, %Activity{}, _object} = CommonAPI.repeat(activity.id, user)
-      {:error, _} = CommonAPI.repeat(activity.id, user)
+      {:ok, %Activity{} = activity, object} = CommonAPI.repeat(activity.id, user)
+      {:ok, ^activity, ^object} = CommonAPI.repeat(activity.id, user)
     end
 
-    test "favoriting a status twice returns an error" do
+    test "favoriting a status twice returns the status" do
       user = insert(:user)
       other_user = insert(:user)
 
       {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
-      {:ok, %Activity{}, _object} = CommonAPI.favorite(activity.id, user)
-      {:error, _} = CommonAPI.favorite(activity.id, user)
+      {:ok, %Activity{} = activity, object} = CommonAPI.favorite(activity.id, user)
+      {:ok, ^activity, ^object} = CommonAPI.favorite(activity.id, user)
     end
   end
 
index 307221c5d015469d6840b7d0fdd891f1fda403e9..0b75fcc9193637c599d61c36110624ff639af048 100644 (file)
@@ -638,6 +638,13 @@ defmodule Pleroma.Web.MastodonAPI.StatusControllerTest do
       assert to_string(activity.id) == id
     end
 
+    test "favoriting twice will just return 200", %{conn: conn} do
+      activity = insert(:note_activity)
+
+      post(conn, "/api/v1/statuses/#{activity.id}/favourite")
+      assert post(conn, "/api/v2/statuses/#{activity.id}/favourite") |> json_response(200)
+    end
+
     test "returns 400 error for a wrong id", %{conn: conn} do
       conn = post(conn, "/api/v1/statuses/1/favourite")