diff --git a/lib/vonbraun/activity_pub/handler.ex b/lib/vonbraun/activity_pub/handler.ex index ba1196b..c9df419 100644 --- a/lib/vonbraun/activity_pub/handler.ex +++ b/lib/vonbraun/activity_pub/handler.ex @@ -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 diff --git a/lib/vonbraun/activity_pub/handler/accept.ex b/lib/vonbraun/activity_pub/handler/accept.ex index 9105430..53b0371 100644 --- a/lib/vonbraun/activity_pub/handler/accept.ex +++ b/lib/vonbraun/activity_pub/handler/accept.ex @@ -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(%{ - "type" => "Accept", - "actor" => actor_id, - "object" => object = %{"type" => "Follow"} - }) do + def handle( + %{ + "type" => "Accept", + "actor" => actor_id, + "object" => object = %{"type" => "Follow"} + }, + %{} + ) 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 diff --git a/lib/vonbraun/activity_pub/handler/follow.ex b/lib/vonbraun/activity_pub/handler/follow.ex index f607194..81463c1 100644 --- a/lib/vonbraun/activity_pub/handler/follow.ex +++ b/lib/vonbraun/activity_pub/handler/follow.ex @@ -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 diff --git a/lib/vonbraun/activity_pub/handler/reject.ex b/lib/vonbraun/activity_pub/handler/reject.ex index a6269fb..e969776 100644 --- a/lib/vonbraun/activity_pub/handler/reject.ex +++ b/lib/vonbraun/activity_pub/handler/reject.ex @@ -42,5 +42,5 @@ defmodule Vonbraun.ActivityPub.Handler.Reject do end end - def handle(%{}), do: {:error, :match} + def handle(%{}, %{}), do: {:error, :match} end diff --git a/lib/vonbraun/activity_pub/handler/undo.ex b/lib/vonbraun/activity_pub/handler/undo.ex index b276539..979bbc4 100644 --- a/lib/vonbraun/activity_pub/handler/undo.ex +++ b/lib/vonbraun/activity_pub/handler/undo.ex @@ -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 diff --git a/lib/vonbraun/activity_pub/handler_behaviour.ex b/lib/vonbraun/activity_pub/handler_behaviour.ex index d3a1ed6..541d29b 100644 --- a/lib/vonbraun/activity_pub/handler_behaviour.ex +++ b/lib/vonbraun/activity_pub/handler_behaviour.ex @@ -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