Add fallback inbox handler

This commit is contained in:
Shadowfacts 2019-10-02 10:28:06 -04:00
parent 9e39d56325
commit 091a275b22
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 21 additions and 33 deletions

View File

@ -13,8 +13,9 @@ defmodule Clacks.Inbox do
Repo.insert(changeset) Repo.insert(changeset)
end end
@spec handle_create(activity :: map()) :: :ok | {:error, reason :: any()} @spec handle(activity :: map()) :: :ok | {:error, reason :: any()}
def handle_create(%{"type" => "Create", "object" => object} = activity) do
def handle(%{"type" => "Create", "object" => object} = activity) do
changeset = Object.changeset(%Object{}, %{data: object}) changeset = Object.changeset(%Object{}, %{data: object})
case Repo.insert(changeset) do case Repo.insert(changeset) do
@ -34,10 +35,7 @@ defmodule Clacks.Inbox do
end end
end end
@spec handle_follow(activity :: map()) :: :ok | {:error, reason :: any()} def handle(%{"type" => "Follow", "object" => followed_id, "actor" => follower_id} = activity)
def handle_follow(
%{"type" => "Follow", "object" => followed_id, "actor" => follower_id} = activity
)
when is_binary(followed_id) do when is_binary(followed_id) do
followed = Actor.get_by_ap_id(followed_id) followed = Actor.get_by_ap_id(followed_id)
follower = Actor.get_by_ap_id(follower_id) follower = Actor.get_by_ap_id(follower_id)
@ -64,4 +62,16 @@ defmodule Clacks.Inbox do
end end
end end
end end
# as a fallback, just store the activity
def handle(activity) do
case store_activity(activity) do
{:error, changeset} ->
Logger.error("Could not store activity: #{inspect(changeset)}")
{:error, "Could not store activity"}
{:ok, _activity} ->
:ok
end
end
end end

View File

@ -14,37 +14,15 @@ defmodule ClacksWeb.InboxController do
handle(conn, conn.body_params) handle(conn, conn.body_params)
end end
def handle(conn, %{"type" => "Create"} = activity) do def handle(conn, %{"type" => type} = activity) do
case Inbox.handle_create(activity) do case Inbox.handle(activity) do
:ok -> :ok ->
conn put_status(conn, 200)
|> put_status(200)
{:error, reason} -> {:error, reason} ->
Logger.error("Could not handle incoming Create: #{inspect(reason)}") Logger.error("Could not handle incoming #{type} activity: #{inspect(reason)}")
put_status(conn, 500)
conn
|> put_status(500)
end end
|> json(%{}) |> json(%{})
end end
def handle(conn, %{"type" => "Follow", "object" => object} = activity) when is_binary(object) do
case Inbox.handle_follow(activity) do
:ok ->
conn
|> put_status(200)
{:error, reason} ->
Logger.error("Could not handle incoming follow: #{inspect(reason)}")
conn
|> put_status(500)
end
|> json(%{})
end
# def handle(conn, _) do
# # todo: figure out how handle unhandled activity types
# end
end end