CommonAPI: Add unreactions.

This commit is contained in:
lain 2019-10-02 15:38:57 +02:00
parent dfe5c958eb
commit 9cfe9a57c5
3 changed files with 26 additions and 1 deletions

View File

@ -452,6 +452,19 @@ defmodule Pleroma.Web.ActivityPub.Utils do
|> Repo.one() |> Repo.one()
end 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 #### Announce-related helpers
@doc """ @doc """

View File

@ -125,6 +125,15 @@ defmodule Pleroma.Web.CommonAPI do
end end
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 def vote(user, %{data: %{"type" => "Question"}} = object, choices) do
with :ok <- validate_not_author(object, user), with :ok <- validate_not_author(object, user),
:ok <- validate_existing_votes(user, object), :ok <- validate_existing_votes(user, object),

View File

@ -243,7 +243,10 @@ defmodule Pleroma.Web.CommonAPITest do
{:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"}) {:ok, activity} = CommonAPI.post(other_user, %{"status" => "cofe"})
{:ok, reaction, _} = CommonAPI.react_with_emoji(activity.id, user, "👍") {: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 end
test "repeating a status" do test "repeating a status" do