Browse Source

Add fallback inbox handler

master
Shadowfacts 2 years ago
parent
commit
091a275b22
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
2 changed files with 21 additions and 33 deletions
  1. +16
    -6
      lib/clacks/inbox.ex
  2. +5
    -27
      lib/clacks_web/controllers/inbox_controller.ex

+ 16
- 6
lib/clacks/inbox.ex View File

@ -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

+ 5
- 27
lib/clacks_web/controllers/inbox_controller.ex View File

@ -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)
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)
Logger.error("Could not handle incoming #{type} activity: #{inspect(reason)}")
put_status(conn, 500)
end
|> json(%{})
end
# def handle(conn, _) do
# # todo: figure out how handle unhandled activity types
# end
end

Loading…
Cancel
Save