take in actor object instead of querying it twice
This commit is contained in:
parent
9bbe024652
commit
90283a4ffd
|
@ -15,15 +15,15 @@ defmodule Vonbraun.ActivityPub.Handler do
|
||||||
]
|
]
|
||||||
|> Enum.reduce(Map.new(), fn mod, map ->
|
|> Enum.reduce(Map.new(), fn mod, map ->
|
||||||
type = apply(mod, :type, [])
|
type = apply(mod, :type, [])
|
||||||
Map.put(map, type, &mod.handle/1)
|
Map.put(map, type, &mod.handle/2)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec handle(%{type: String.t()}) :: :ok | {:ok, atom()} | {:error, any()}
|
@spec handle(%{type: String.t()}, map()) :: :ok | {:ok, atom()} | {:error, any()}
|
||||||
def handle(activity = %{"type" => type}) when is_binary(type) do
|
def handle(activity = %{"type" => type}, actor = %{}) when is_binary(type) do
|
||||||
Agent.get(__MODULE__, fn map ->
|
Agent.get(__MODULE__, fn map ->
|
||||||
func = Map.get(map, type, fn _ -> {:error, :type} end)
|
func = Map.get(map, type, fn _ -> {:error, :type} end)
|
||||||
apply(func, [activity])
|
apply(func, [activity, actor])
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -30,11 +30,14 @@ defmodule Vonbraun.ActivityPub.Handler.Accept do
|
||||||
end
|
end
|
||||||
|
|
||||||
# Lots of kinds of things can be accepted but for right now only follows.
|
# Lots of kinds of things can be accepted but for right now only follows.
|
||||||
def handle(%{
|
def handle(
|
||||||
"type" => "Accept",
|
%{
|
||||||
"actor" => actor_id,
|
"type" => "Accept",
|
||||||
"object" => object = %{"type" => "Follow"}
|
"actor" => actor_id,
|
||||||
}) do
|
"object" => object = %{"type" => "Follow"}
|
||||||
|
},
|
||||||
|
%{}
|
||||||
|
) do
|
||||||
with {:actor, {:ok, follow_actor_id}} <- {:actor, extract_follow_object_actor(object)},
|
with {:actor, {:ok, follow_actor_id}} <- {:actor, extract_follow_object_actor(object)},
|
||||||
{:match, true} <- {:match, follow_actor_id == Object.my_id()},
|
{:match, true} <- {:match, follow_actor_id == Object.my_id()},
|
||||||
{:asked, {:ok, %Actor{:blocked => nil, :following_state => "accepted"}}} <-
|
{:asked, {:ok, %Actor{:blocked => nil, :following_state => "accepted"}}} <-
|
||||||
|
@ -63,5 +66,5 @@ defmodule Vonbraun.ActivityPub.Handler.Accept do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(%{}), do: {:error, :match}
|
def handle(%{}, %{}), do: {:error, :match}
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,10 +8,12 @@ defmodule Vonbraun.ActivityPub.Handler.Follow do
|
||||||
|
|
||||||
def type, do: "Follow"
|
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
|
when is_binary(follow_requester_id) and is_binary(follow_target) do
|
||||||
with {:valid_target, true} <- {:valid_target, Object.my_id() == follow_target},
|
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}}}
|
{:add, {:ok, %Actor{:blocked => nil, :follows_me_state => follows_me_state}}}
|
||||||
when not is_nil(follows_me_state) <-
|
when not is_nil(follows_me_state) <-
|
||||||
{:add, Actor.maybe_add_follower(follow_requester_id)} do
|
{:add, Actor.maybe_add_follower(follow_requester_id)} do
|
||||||
|
@ -58,9 +60,6 @@ defmodule Vonbraun.ActivityPub.Handler.Follow do
|
||||||
{:valid_target, false} ->
|
{:valid_target, false} ->
|
||||||
{:ok, :unauthorized}
|
{:ok, :unauthorized}
|
||||||
|
|
||||||
{:actor, {:error, error}} ->
|
|
||||||
{:error, {:actor, error}}
|
|
||||||
|
|
||||||
{:add, {:ok, %Actor{:blocked => blocked_ts}}} when not is_nil(blocked_ts) ->
|
{:add, {:ok, %Actor{:blocked => blocked_ts}}} when not is_nil(blocked_ts) ->
|
||||||
{:ok, :unauthorized}
|
{:ok, :unauthorized}
|
||||||
|
|
||||||
|
@ -73,5 +72,5 @@ defmodule Vonbraun.ActivityPub.Handler.Follow do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(%{}), do: {:error, :match}
|
def handle(%{}, %{}), do: {:error, :match}
|
||||||
end
|
end
|
||||||
|
|
|
@ -42,5 +42,5 @@ defmodule Vonbraun.ActivityPub.Handler.Reject do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def handle(%{}), do: {:error, :match}
|
def handle(%{}, %{}), do: {:error, :match}
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,7 @@ defmodule Vonbraun.ActivityPub.Handler.Undo do
|
||||||
def type, do: "Undo"
|
def type, do: "Undo"
|
||||||
|
|
||||||
# Lots of different kinds of things can be undone.
|
# Lots of different kinds of things can be undone.
|
||||||
def handle(_activity = %{"type" => "Undo"}) do
|
def handle(%{"type" => "Undo"}, %{}) do
|
||||||
{:ok, :unknown}
|
{:ok, :unknown}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
defmodule Vonbraun.ActivityPub.HandlerBehaviour do
|
defmodule Vonbraun.ActivityPub.HandlerBehaviour do
|
||||||
@callback type() :: String.t()
|
@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
|
end
|
||||||
|
|
Loading…
Reference in New Issue