take in actor object instead of querying it twice

This commit is contained in:
Moon Man 2024-08-29 11:59:48 +00:00
parent 9bbe024652
commit 90283a4ffd
6 changed files with 22 additions and 19 deletions

View File

@ -15,15 +15,15 @@ defmodule Vonbraun.ActivityPub.Handler do
]
|> Enum.reduce(Map.new(), fn mod, map ->
type = apply(mod, :type, [])
Map.put(map, type, &mod.handle/1)
Map.put(map, type, &mod.handle/2)
end)
end
@spec handle(%{type: String.t()}) :: :ok | {:ok, atom()} | {:error, any()}
def handle(activity = %{"type" => type}) when is_binary(type) do
@spec handle(%{type: String.t()}, map()) :: :ok | {:ok, atom()} | {:error, any()}
def handle(activity = %{"type" => type}, actor = %{}) when is_binary(type) do
Agent.get(__MODULE__, fn map ->
func = Map.get(map, type, fn _ -> {:error, :type} end)
apply(func, [activity])
apply(func, [activity, actor])
end)
end
end

View File

@ -30,11 +30,14 @@ defmodule Vonbraun.ActivityPub.Handler.Accept do
end
# Lots of kinds of things can be accepted but for right now only follows.
def handle(%{
def handle(
%{
"type" => "Accept",
"actor" => actor_id,
"object" => object = %{"type" => "Follow"}
}) do
},
%{}
) do
with {:actor, {:ok, follow_actor_id}} <- {:actor, extract_follow_object_actor(object)},
{:match, true} <- {:match, follow_actor_id == Object.my_id()},
{:asked, {:ok, %Actor{:blocked => nil, :following_state => "accepted"}}} <-
@ -63,5 +66,5 @@ defmodule Vonbraun.ActivityPub.Handler.Accept do
end
end
def handle(%{}), do: {:error, :match}
def handle(%{}, %{}), do: {:error, :match}
end

View File

@ -8,10 +8,12 @@ defmodule Vonbraun.ActivityPub.Handler.Follow do
def type, do: "Follow"
def handle(%{"type" => "Follow", "actor" => follow_requester_id, "object" => follow_target})
def handle(
%{"type" => "Follow", "actor" => follow_requester_id, "object" => follow_target},
actor = %{}
)
when is_binary(follow_requester_id) and is_binary(follow_target) do
with {:valid_target, true} <- {:valid_target, Object.my_id() == follow_target},
{:actor, {:ok, actor}} <- {:actor, ActivityPubReq.get_actor(follow_requester_id)},
{:add, {:ok, %Actor{:blocked => nil, :follows_me_state => follows_me_state}}}
when not is_nil(follows_me_state) <-
{:add, Actor.maybe_add_follower(follow_requester_id)} do
@ -58,9 +60,6 @@ defmodule Vonbraun.ActivityPub.Handler.Follow do
{:valid_target, false} ->
{:ok, :unauthorized}
{:actor, {:error, error}} ->
{:error, {:actor, error}}
{:add, {:ok, %Actor{:blocked => blocked_ts}}} when not is_nil(blocked_ts) ->
{:ok, :unauthorized}
@ -73,5 +72,5 @@ defmodule Vonbraun.ActivityPub.Handler.Follow do
end
end
def handle(%{}), do: {:error, :match}
def handle(%{}, %{}), do: {:error, :match}
end

View File

@ -42,5 +42,5 @@ defmodule Vonbraun.ActivityPub.Handler.Reject do
end
end
def handle(%{}), do: {:error, :match}
def handle(%{}, %{}), do: {:error, :match}
end

View File

@ -4,7 +4,7 @@ defmodule Vonbraun.ActivityPub.Handler.Undo do
def type, do: "Undo"
# Lots of different kinds of things can be undone.
def handle(_activity = %{"type" => "Undo"}) do
def handle(%{"type" => "Undo"}, %{}) do
{:ok, :unknown}
end
end

View File

@ -1,4 +1,5 @@
defmodule Vonbraun.ActivityPub.HandlerBehaviour do
@callback type() :: String.t()
@callback handle(activity :: map()) :: :ok | {:ok, atom()} | {:error, any()}
@callback handle(activity :: map(), actor_object :: map()) ::
:ok | {:ok, atom()} | {:error, any()}
end