118 lines
3.9 KiB
Elixir
118 lines
3.9 KiB
Elixir
defmodule PlugMicropub.Handler do
|
|
import Plug.Conn
|
|
alias PlugMicropub.{Response, Parser, Properties}
|
|
|
|
def handle_action(:create, access_token, conn) do
|
|
content_type = conn |> get_req_header("content-type") |> List.first()
|
|
handler = conn.private[:plug_micropub][:handler]
|
|
|
|
with {:ok, type, raw_properties} <- Parser.parse_create_body(content_type, conn.body_params),
|
|
{:ok, properties} = Properties.parse(raw_properties),
|
|
{:ok, code, url} <- handler.handle_create(type, properties, access_token) do
|
|
conn
|
|
|> put_resp_header("location", url)
|
|
|> send_resp(code, "")
|
|
else
|
|
error -> Response.send_error(conn, error)
|
|
end
|
|
end
|
|
|
|
def handle_action(:update, access_token, conn) do
|
|
content_type = conn |> get_req_header("content-type") |> List.first()
|
|
|
|
with "application/json" <- content_type,
|
|
{url, properties} when is_binary(url) <- Map.pop(conn.body_params, "url"),
|
|
{:ok, replace, add, delete} <- Parser.parse_update_properties(properties),
|
|
do: do_update(conn, access_token, url, replace, add, delete),
|
|
else: (_ -> Response.send_error(conn, {:error, :invalid_request}))
|
|
end
|
|
|
|
def handle_action(:delete, access_token, conn) do
|
|
with {:ok, url} <- Map.fetch(conn.body_params, "url"),
|
|
do: do_delete(conn, access_token, url),
|
|
else: (_ -> Response.send_error(conn, {:error, :invalid_request}))
|
|
end
|
|
|
|
def handle_action(:undelete, access_token, conn) do
|
|
with {:ok, url} <- Map.fetch(conn.body_params, "url"),
|
|
do: do_undelete(conn, access_token, url),
|
|
else: (_ -> Response.send_error(conn, {:error, :invalid_request}))
|
|
end
|
|
|
|
def handle_query(:config, access_token, conn) do
|
|
handler = conn.private[:plug_micropub][:handler]
|
|
|
|
case handler.handle_config_query(access_token) do
|
|
{:ok, content} -> Response.send_content(conn, content)
|
|
error -> Response.send_error(conn, error)
|
|
end
|
|
end
|
|
|
|
def handle_query(:source, access_token, conn) do
|
|
with {:ok, url} <- Map.fetch(conn.query_params, "url"),
|
|
do: do_source_query(conn, access_token, url),
|
|
else: (_ -> Response.send_error(conn, {:error, :invalid_request}))
|
|
end
|
|
|
|
def handle_query(:"syndicate-to", access_token, conn) do
|
|
handler = conn.private[:plug_micropub][:handler]
|
|
|
|
case handler.handle_syndicate_to_query(access_token) do
|
|
{:ok, content} -> Response.send_content(conn, content)
|
|
error -> Response.send_error(conn, error)
|
|
end
|
|
end
|
|
|
|
defp do_update(conn, access_token, url, replace, add, delete) do
|
|
handler = conn.private[:plug_micropub][:handler]
|
|
|
|
case handler.handle_update(url, replace, add, delete, access_token) do
|
|
:ok ->
|
|
send_resp(conn, :no_content, "")
|
|
|
|
{:ok, url} ->
|
|
conn
|
|
|> put_resp_header("location", url)
|
|
|> send_resp(:created, "")
|
|
|
|
error ->
|
|
Response.send_error(conn, error)
|
|
end
|
|
end
|
|
|
|
defp do_delete(conn, access_token, url) do
|
|
handler = conn.private[:plug_micropub][:handler]
|
|
|
|
case handler.handle_delete(url, access_token) do
|
|
:ok -> send_resp(conn, :no_content, "")
|
|
error -> Response.send_error(conn, error)
|
|
end
|
|
end
|
|
|
|
defp do_undelete(conn, access_token, url) do
|
|
handler = conn.private[:plug_micropub][:handler]
|
|
|
|
case handler.handle_undelete(url, access_token) do
|
|
:ok ->
|
|
send_resp(conn, :no_content, "")
|
|
|
|
{:ok, url} ->
|
|
conn
|
|
|> put_resp_header("location", url)
|
|
|> send_resp(:created, "")
|
|
|
|
error ->
|
|
Response.send_error(conn, error)
|
|
end
|
|
end
|
|
|
|
defp do_source_query(conn, access_token, url) do
|
|
handler = conn.private[:plug_micropub][:handler]
|
|
properties = Map.get(conn.query_params, "properties", [])
|
|
|
|
case handler.handle_source_query(url, properties, access_token) do
|
|
{:ok, content} -> Response.send_content(conn, content)
|
|
error -> Response.send_error(conn, error)
|
|
end
|
|
end
|
|
end
|