Handle incoming Creates better

This commit is contained in:
Shadowfacts 2019-10-02 09:49:59 -04:00
parent 2316c5d41d
commit 9e39d56325
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 26 additions and 5 deletions

View File

@ -13,11 +13,25 @@ defmodule Clacks.Inbox do
Repo.insert(changeset)
end
@spec handle_create(activity :: map()) :: :ok | {:error, reason :: any()}
def handle_create(%{"type" => "Create", "object" => object} = activity) do
changeset = Object.changeset(%Object{}, %{data: object})
{:ok, _object} = Repo.insert(changeset)
{:ok, _activity} = store_activity(activity)
case Repo.insert(changeset) do
{:error, changeset} ->
Logger.error("Couldn't store object: #{inspect(changeset)}")
{:error, "Couldn't store changeset"}
{:ok, _object} ->
case store_activity(activity) do
{:error, changeset} ->
Logger.error("Couldn't store Create activity: #{inspect(changeset)}")
{:error, "Couldn't store Create activity"}
{:ok, _activity} ->
:ok
end
end
end
@spec handle_follow(activity :: map()) :: :ok | {:error, reason :: any()}

View File

@ -15,10 +15,17 @@ defmodule ClacksWeb.InboxController do
end
def handle(conn, %{"type" => "Create"} = activity) do
Inbox.handle_create(activity)
case Inbox.handle_create(activity) do
:ok ->
conn
|> put_status(200)
{:error, reason} ->
Logger.error("Could not handle incoming Create: #{inspect(reason)}")
conn
|> put_status(500)
end
|> json(%{})
end