clacks/lib/clacks_web/controllers/inbox_controller.ex

51 lines
1.1 KiB
Elixir
Raw Normal View History

2019-10-01 22:39:17 +00:00
defmodule ClacksWeb.InboxController do
2019-10-02 13:47:04 +00:00
require Logger
2019-10-01 22:39:17 +00:00
use ClacksWeb, :controller
2019-10-02 01:59:43 +00:00
alias Clacks.Inbox
2019-10-01 22:39:17 +00:00
plug Plug.Parsers, parsers: [:urlencoded, :json], json_decoder: Jason
plug ClacksWeb.Plug.HTTPSignature
2019-10-02 01:59:43 +00:00
def shared(conn, _params) do
handle(conn, conn.body_params)
2019-10-01 22:39:17 +00:00
end
2019-10-02 01:59:43 +00:00
def user_specific(conn, _params) do
handle(conn, conn.body_params)
2019-10-01 22:39:17 +00:00
end
2019-10-02 01:59:43 +00:00
def handle(conn, %{"type" => "Create"} = activity) do
2019-10-02 13:49:59 +00:00
case Inbox.handle_create(activity) do
:ok ->
conn
|> put_status(200)
{:error, reason} ->
Logger.error("Could not handle incoming Create: #{inspect(reason)}")
2019-10-02 01:59:43 +00:00
2019-10-02 13:49:59 +00:00
conn
|> put_status(500)
end
2019-10-02 13:47:04 +00:00
|> json(%{})
2019-10-02 01:59:43 +00:00
end
def handle(conn, %{"type" => "Follow", "object" => object} = activity) when is_binary(object) do
2019-10-02 13:47:04 +00:00
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(%{})
2019-10-02 01:59:43 +00:00
end
# def handle(conn, _) do
# # todo: figure out how handle unhandled activity types
# end
2019-10-01 22:39:17 +00:00
end