CommonAPI: Add unreactions.
authorlain <lain@soykaf.club>
Wed, 2 Oct 2019 13:38:57 +0000 (15:38 +0200)
committerlain <lain@soykaf.club>
Wed, 2 Oct 2019 13:38:57 +0000 (15:38 +0200)
lib/pleroma/web/activity_pub/utils.ex
lib/pleroma/web/common_api/common_api.ex
test/web/common_api/common_api_test.exs

index 7807fa9cdf08dafc85da0457aa250e94e8fbd7e7..82e2d8f76b9c2c94a3df64af27378898d5a5f856 100644 (file)
@@ -452,6 +452,19 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     |> Repo.one()
   end
 
+  def get_latest_reaction(internal_activity_id, %{ap_id: ap_id}, emoji) do
+    %{data: %{"object" => object_ap_id}} = Activity.get_by_id(internal_activity_id)
+
+    "EmojiReaction"
+    |> Activity.Queries.by_type()
+    |> where(actor: ^ap_id)
+    |> where([activity], fragment("?->>'content' = ?", activity.data, ^emoji))
+    |> Activity.Queries.by_object_id(object_ap_id)
+    |> order_by([activity], fragment("? desc nulls last", activity.id))
+    |> limit(1)
+    |> Repo.one()
+  end
+
   #### Announce-related helpers
 
   @doc """
index 53ada8fabb8c9109bee2223d459e662047a3308d..995d4b1af96724ddeff1b36d31a28df374e48f26 100644 (file)
@@ -125,6 +125,15 @@ defmodule Pleroma.Web.CommonAPI do
     end
   end
 
+  def unreact_with_emoji(id, user, emoji) do
+    with %Activity{} = reaction_activity <- Utils.get_latest_reaction(id, user, emoji) do
+      ActivityPub.unreact_with_emoji(user, reaction_activity.data["id"])
+    else
+      _ ->
+        {:error, dgettext("errors", "Could not remove reaction emoji")}
+    end
+  end
+
   def vote(user, %{data: %{"type" => "Question"}} = object, choices) do
     with :ok <- validate_not_author(object, user),
          :ok <- validate_existing_votes(user, object),
index e048ed217c98cc5fa96eb7f9f648bdc319fd1729..b9e785885053363e3b2fd4e46899c149fe449f70 100644 (file)
@@ -243,7 +243,10 @@ defmodule Pleroma.Web.CommonAPITest do
       {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
       {:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍")
 
-      assert false
+      {:ok, unreaction, _} = CommonAPI.unreact_with_emoji(activity.id, user, "👍")
+
+      assert unreaction.data["type"] == "Undo"
+      assert unreaction.data["object"] == reaction.data["id"]
     end
 
     test "repeating a status" do