Browse Source

Handle incoming Creates better

master
Shadowfacts 2 years ago
parent
commit
9e39d56325
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
2 changed files with 26 additions and 5 deletions
  1. +16
    -2
      lib/clacks/inbox.ex
  2. +10
    -3
      lib/clacks_web/controllers/inbox_controller.ex

+ 16
- 2
lib/clacks/inbox.ex 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()}


+ 10
- 3
lib/clacks_web/controllers/inbox_controller.ex 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(200)
conn
|> put_status(500)
end
|> json(%{})
end


Loading…
Cancel
Save