Accept bites for objects

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
marcin mikołajczak 2024-10-18 17:59:46 +02:00
parent e79dcd2bfe
commit 61410ad886
5 changed files with 36 additions and 8 deletions

View file

@ -38,7 +38,7 @@ defp validate_data(cng) do
|> validate_required([:id, :type, :actor, :to, :target])
|> validate_inclusion(:type, ["Bite"])
|> validate_actor_presence()
|> validate_actor_presence(field_name: :target)
|> validate_object_or_user_presence(field_name: :target)
end
def cast_and_validate(data) do

View file

@ -487,8 +487,35 @@ def handle_incoming(
end
end
def handle_incoming(
%{"type" => "Bite", "target" => target_id} = data,
options
) do
target_id =
cond do
%User{ap_id: actor_id} = User.get_by_ap_id(target_id) ->
actor_id
%Object{data: data} = Object.get_by_ap_id(target_id)
when is_binary(data["actor"]) or is_binary(data["attributedTo"]) ->
data["actor"] || data["attributedTo"]
_ ->
target_id
end
with data = Map.put(data, "target", target_id),
:ok <- ObjectValidator.fetch_actor_and_object(data),
{:ok, activity, _meta} <-
Pipeline.common_pipeline(data, local: false) do
{:ok, activity}
else
e -> {:error, e}
end
end
def handle_incoming(%{"type" => type} = data, _options)
when type in ~w{Like EmojiReact Announce Add Remove Bite} do
when type in ~w{Like EmojiReact Announce Add Remove} do
with :ok <- ObjectValidator.fetch_actor_and_object(data),
{:ok, activity, _meta} <-
Pipeline.common_pipeline(data, local: false) do

View file

@ -984,9 +984,10 @@ defp maybe_exclude_activity_id(query, %Activity{id: activity_id}) do
end
defp exclude_rejected(query) do
rejected_activities = "Reject"
|> Activity.Queries.by_type()
|> select([a], fragment("?->>'object'", a.data))
rejected_activities =
"Reject"
|> Activity.Queries.by_type()
|> select([a], fragment("?->>'object'", a.data))
query
|> where([a], fragment("?->>'id'", a.data) not in subquery(rejected_activities))

View file

@ -15,7 +15,7 @@ defmodule Pleroma.Web.MastodonAPI.BiteController do
plug(OAuthScopesPlug, %{scopes: ["write:bites"]} when action == :bite)
plug(RateLimiter, [name: :bites])
plug(RateLimiter, name: :bites)
plug(:assign_account_by_id)

View file

@ -16,7 +16,7 @@ test "bites a user", %{conn: conn} do
|> post("/api/v1/bite?id=#{bitten_id}")
|> json_response_and_validate_schema(200)
assert response == %{}
assert response == %{}
end
test "self harm is not supported", %{conn: conn, user: %{id: self_id}} do
@ -25,6 +25,6 @@ test "self harm is not supported", %{conn: conn, user: %{id: self_id}} do
|> post("/api/v1/bite?id=#{self_id}")
|> json_response_and_validate_schema(400)
assert %{"error" => "Can not bite yourself"} = response
assert %{"error" => "Can not bite yourself"} = response
end
end