From 091a275b22bb7d600757d7f857e3cd751fd21ce0 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 2 Oct 2019 10:28:06 -0400 Subject: [PATCH] Add fallback inbox handler --- lib/clacks/inbox.ex | 22 +++++++++---- .../controllers/inbox_controller.ex | 32 +++---------------- 2 files changed, 21 insertions(+), 33 deletions(-) diff --git a/lib/clacks/inbox.ex b/lib/clacks/inbox.ex index 9037f65..107c6ff 100644 --- a/lib/clacks/inbox.ex +++ b/lib/clacks/inbox.ex @@ -13,8 +13,9 @@ 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 + @spec handle(activity :: map()) :: :ok | {:error, reason :: any()} + + def handle(%{"type" => "Create", "object" => object} = activity) do changeset = Object.changeset(%Object{}, %{data: object}) case Repo.insert(changeset) do @@ -34,10 +35,7 @@ defmodule Clacks.Inbox do end end - @spec handle_follow(activity :: map()) :: :ok | {:error, reason :: any()} - def handle_follow( - %{"type" => "Follow", "object" => followed_id, "actor" => follower_id} = activity - ) + def handle(%{"type" => "Follow", "object" => followed_id, "actor" => follower_id} = activity) when is_binary(followed_id) do followed = Actor.get_by_ap_id(followed_id) follower = Actor.get_by_ap_id(follower_id) @@ -64,4 +62,16 @@ defmodule Clacks.Inbox do 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 diff --git a/lib/clacks_web/controllers/inbox_controller.ex b/lib/clacks_web/controllers/inbox_controller.ex index 9ad0f72..8d97d6e 100644 --- a/lib/clacks_web/controllers/inbox_controller.ex +++ b/lib/clacks_web/controllers/inbox_controller.ex @@ -14,37 +14,15 @@ defmodule ClacksWeb.InboxController do handle(conn, conn.body_params) end - def handle(conn, %{"type" => "Create"} = activity) do - case Inbox.handle_create(activity) do + def handle(conn, %{"type" => type} = activity) do + case Inbox.handle(activity) do :ok -> - conn - |> put_status(200) + put_status(conn, 200) {:error, reason} -> - Logger.error("Could not handle incoming Create: #{inspect(reason)}") - - conn - |> put_status(500) + Logger.error("Could not handle incoming #{type} activity: #{inspect(reason)}") + put_status(conn, 500) end |> json(%{}) 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