2023-05-25 21:23:04 +02:00
|
|
|
defmodule ChiyaWeb.Indie.MicropubHandler do
|
2023-05-31 21:42:55 +02:00
|
|
|
@behaviour PlugMicropub.HandlerBehaviour
|
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
|
|
|
|
2023-05-25 21:23:04 +02:00
|
|
|
@impl true
|
|
|
|
def handle_create(type, properties, access_token) do
|
2023-05-31 21:32:34 +02:00
|
|
|
dbg(properties)
|
2023-05-31 22:15:39 +02:00
|
|
|
dbg(type)
|
2023-05-31 21:32:34 +02:00
|
|
|
|
2023-06-08 09:06:16 +02:00
|
|
|
with :ok <- verify_token(access_token),
|
2023-05-31 21:32:34 +02:00
|
|
|
{:ok, post_type} <- Props.get_post_type(properties),
|
2023-05-31 22:15:39 +02:00
|
|
|
{:ok, note_attrs} <- get_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
|
2023-05-25 21:23:04 +02:00
|
|
|
end
|
|
|
|
|
2023-05-31 21:42:55 +02:00
|
|
|
@impl true
|
|
|
|
def handle_update(_, _, _, _, _) do
|
|
|
|
{:error, :insufficient_scope}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_delete(_url, _access_token) do
|
|
|
|
{:error, :insufficient_scope}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_undelete(_url, _access_token) do
|
|
|
|
{:error, :insufficient_scope}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_source_query(_url, _filter_properties, _access_token) do
|
|
|
|
{:error, :insufficient_scope}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_media(_files, _access_token) do
|
|
|
|
{:error, :insufficient_scope}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_config_query(_access_token) do
|
2023-06-08 09:06:16 +02:00
|
|
|
channels = Chiya.Channels.list_channels()
|
|
|
|
|
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
"destination" => [],
|
2023-06-08 09:21:33 +02:00
|
|
|
"post-types" => [
|
|
|
|
%{
|
|
|
|
"type" => "note",
|
|
|
|
"name" => "Note"
|
|
|
|
}
|
|
|
|
],
|
2023-06-08 09:06:16 +02:00
|
|
|
"channels" =>
|
|
|
|
Enum.map(channels, fn c ->
|
|
|
|
%{
|
|
|
|
uid: c.slug,
|
|
|
|
name: c.name
|
|
|
|
}
|
|
|
|
end)
|
|
|
|
}}
|
2023-05-31 21:42:55 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def handle_syndicate_to_query(_access_token) do
|
2023-06-08 09:06:16 +02:00
|
|
|
{:ok, %{"syndicate-to" => []}}
|
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_token(access_token) do
|
|
|
|
Enum.reduce_while([&verify_app_token/1, &verify_micropub_token/1], nil, fn fun, result ->
|
|
|
|
case fun.(access_token) do
|
|
|
|
:ok -> {:halt, :ok}
|
|
|
|
error -> {:cont, error}
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_micropub_token(access_token) do
|
|
|
|
Token.verify(access_token, "create", get_hostname())
|
|
|
|
end
|
|
|
|
|
|
|
|
defp verify_app_token(access_token) do
|
|
|
|
token = Chiya.Accounts.get_app_token("obsidian", "app")
|
|
|
|
|
|
|
|
if not is_nil(token) do
|
|
|
|
token_string =
|
|
|
|
token.token
|
|
|
|
|> :crypto.bytes_to_integer()
|
|
|
|
|> to_string()
|
|
|
|
|
|
|
|
if token_string == access_token do
|
|
|
|
:ok
|
|
|
|
else
|
|
|
|
{:error, :insufficient_scope, "Could not verify app token"}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
{:error, :insufficient_scope, "Could not verify app token"}
|
|
|
|
end
|
2023-05-31 21:42:55 +02:00
|
|
|
end
|
2023-05-25 21:23:04 +02:00
|
|
|
|
2023-05-31 22:15:39 +02:00
|
|
|
defp get_attrs(type, post_type, properties) do
|
|
|
|
Logger.info("Creating a #{type}/#{post_type}..")
|
|
|
|
|
2023-05-31 21:32:34 +02:00
|
|
|
case post_type do
|
2023-05-31 22:15:39 +02:00
|
|
|
:note -> get_note_attrs(properties)
|
2023-05-31 21:32:34 +02:00
|
|
|
_ -> {:error, :insufficient_scope}
|
|
|
|
end
|
2023-05-28 22:59:11 +02:00
|
|
|
end
|
|
|
|
|
2023-05-31 22:15:39 +02:00
|
|
|
defp get_note_attrs(p) do
|
2023-05-31 21:32:34 +02:00
|
|
|
content = Props.get_content(p)
|
|
|
|
name = Props.get_title(p) || String.slice(content, 0..15)
|
|
|
|
tags = Props.get_tags(p) |> Enum.join(",")
|
|
|
|
|
2023-06-01 23:22:16 +02:00
|
|
|
published_at =
|
|
|
|
if Props.is_published?(p),
|
|
|
|
do: NaiveDateTime.local_now(),
|
|
|
|
else: nil
|
|
|
|
|
2023-05-31 21:32:34 +02:00
|
|
|
{:ok,
|
|
|
|
%{
|
|
|
|
content: content,
|
|
|
|
name: name,
|
2023-06-01 23:22:16 +02:00
|
|
|
tags_string: tags,
|
|
|
|
published_at: published_at
|
2023-05-31 21:32:34 +02:00
|
|
|
}}
|
2023-05-25 21:23:04 +02:00
|
|
|
end
|
2023-05-28 22:59:11 +02:00
|
|
|
|
|
|
|
defp get_hostname(),
|
|
|
|
do: URI.parse(ChiyaWeb.Endpoint.url()).host
|
2023-05-25 21:23:04 +02:00
|
|
|
end
|