chiya/lib/chiya_web/indie/micropub_handler.ex

53 lines
1.4 KiB
Elixir
Raw Normal View History

defmodule ChiyaWeb.Indie.MicropubHandler do
2023-05-28 22:59:11 +02:00
require Logger
2023-05-31 21:32:34 +02:00
alias ChiyaWeb.Indie.Properties, as: Props
alias ChiyaWeb.Indie.Token
2023-05-28 22:59:11 +02:00
@impl true
def handle_create(type, properties, access_token) do
2023-05-31 21:32:34 +02:00
dbg(properties)
with :ok <- Token.verify(access_token, "create", get_hostname()),
{:ok, post_type} <- Props.get_post_type(properties),
{:ok, note_attrs} <- note_attrs(type, post_type, properties),
2023-05-28 22:59:11 +02:00
{:ok, note} <- Chiya.Notes.create_note(note_attrs) do
2023-05-31 21:32:34 +02:00
{:ok, :created, Chiya.Notes.Note.note_url(note)} |> dbg()
2023-05-28 22:59:11 +02:00
else
error ->
Logger.error("Error occurred while creating note from micropub:")
dbg(error)
{:error, :unhandled_error}
end
end
def handle_create(_, _, _), do: {:error, :insufficient_scope}
2023-05-31 21:32:34 +02:00
defp note_attrs("h-entry", post_type, properties) do
case post_type do
:note -> do_note_attrs(properties)
_ -> {:error, :insufficient_scope}
end
2023-05-28 22:59:11 +02:00
end
2023-05-31 21:32:34 +02:00
defp note_attrs(_, _, _), do: {:error, :insufficient_scope}
defp do_note_attrs(p) do
content = Props.get_content(p)
name = Props.get_title(p) || String.slice(content, 0..15)
tags = Props.get_tags(p) |> Enum.join(",")
{:ok,
%{
content: content,
name: name,
tags_string: tags
}}
|> dbg()
end
2023-05-28 22:59:11 +02:00
defp get_hostname(),
do: URI.parse(ChiyaWeb.Endpoint.url()).host
end