chiya/lib/chiya_web/indie/micropub_handler.ex

176 lines
4 KiB
Elixir
Raw Normal View History

defmodule ChiyaWeb.Indie.MicropubHandler do
2023-08-21 19:42:16 +02:00
@behaviour ChiyaWeb.Plugs.PlugMicropub.HandlerBehaviour
2023-05-28 22:59:11 +02:00
require Logger
2023-07-17 19:39:03 +02:00
use Phoenix.VerifiedRoutes,
endpoint: ChiyaWeb.Endpoint,
router: ChiyaWeb.Router
2023-07-17 20:22:58 +02:00
alias ChiyaWeb.Indie.Micropub
2023-05-28 22:59:11 +02:00
2023-06-11 22:22:17 +02:00
@default_properties [
"name",
"content",
"published_at",
"slug",
"channels",
"tags"
]
@post_types [
%{
"type" => "note",
"name" => "Note"
},
%{
"type" => "bookmark",
"name" => "Bookmark"
2023-08-16 22:54:57 +02:00
},
%{
"type" => "like",
"name" => "Like"
},
%{
"type" => "repost",
"name" => "Repost"
}
]
@impl true
def handle_create(type, properties, access_token) do
Logger.info("Handle create")
Logger.info("Properties: #{inspect(properties)}")
Logger.info("Type: #{type}")
2023-05-31 21:32:34 +02:00
2023-07-17 20:22:58 +02:00
case Micropub.verify_token(access_token) do
2023-07-17 22:23:58 +02:00
:ok -> Micropub.create_note(type, properties)
2023-07-17 20:22:58 +02:00
_ -> {:error, :invalid_request}
2023-05-28 22:59:11 +02:00
end
end
@impl true
2023-07-24 18:39:39 +02:00
def handle_update(url, replace, add, delete, access_token) do
with :ok <- Micropub.verify_token(access_token),
{:ok, note} <- Micropub.find_note(url),
2023-07-24 22:31:00 +02:00
{:ok, _note} <- Micropub.update_note(note, replace, add, delete) do
2023-07-24 18:39:39 +02:00
:ok
else
error -> error
end
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
2023-07-16 17:16:44 +02:00
def handle_media(file, access_token) do
2023-07-17 20:22:58 +02:00
with :ok <- Micropub.verify_token(access_token),
2023-07-16 17:16:44 +02:00
{:ok, image} <- Chiya.Notes.create_note_image_temp(%{path: file.path}) do
2023-07-17 20:22:58 +02:00
url = ChiyaWeb.Uploaders.NoteImageTemp.url({image.path, image}, :original)
2023-07-16 17:16:44 +02:00
{:ok, url}
else
2023-07-17 19:00:41 +02:00
_ ->
{:error, :insufficient_scope}
2023-07-16 17:16:44 +02:00
end
end
@impl true
2023-06-11 22:22:17 +02:00
def handle_source_query(url, filter_properties, access_token) do
filter_properties =
if Enum.empty?(filter_properties),
do: @default_properties,
else: filter_properties
2023-07-17 20:22:58 +02:00
with :ok <- Micropub.verify_token(access_token),
2023-06-11 22:22:17 +02:00
{:ok, slug} <- Chiya.Notes.Note.note_slug(url),
note <- Chiya.Notes.get_public_note_by_slug_preloaded!(slug) do
2023-08-14 18:53:34 +02:00
properties = %{
"name" => [note.name],
"content" => [note.content],
"category" => Enum.map(note.tags, fn tag -> tag.name end),
"published" => note.published_at
}
2023-06-11 22:22:17 +02:00
filtered_note =
2023-08-16 22:34:06 +02:00
Map.filter(properties, fn {key, _val} ->
2023-06-11 22:22:17 +02:00
Enum.member?(filter_properties, to_string(key))
end)
{:ok, filtered_note}
else
_ -> {:error, :insufficient_scope}
end
end
@impl true
2023-06-11 22:22:17 +02:00
def handle_config_query(access_token) do
2023-07-17 20:22:58 +02:00
case Micropub.verify_token(access_token) do
2023-06-11 22:22:17 +02:00
:ok ->
2023-07-17 19:39:03 +02:00
config = %{
"media-endpoint" => url(~p"/indie/micropub/media"),
"destination" => [],
"post-types" => @post_types,
2023-07-24 22:31:00 +02:00
"channels" => get_channels()
2023-07-17 19:39:03 +02:00
}
{:ok, config}
2023-06-11 22:22:17 +02:00
_ ->
{:error, :insufficient_scope}
end
end
@impl true
def handle_syndicate_to_query(access_token) do
2023-07-17 20:22:58 +02:00
case Micropub.verify_token(access_token) do
:ok -> {:ok, %{"syndicate-to" => []}}
_ -> {:error, :insufficient_scope}
end
end
@impl true
def handle_category_query(access_token) do
2023-07-17 20:22:58 +02:00
case Micropub.verify_token(access_token) do
2023-06-11 22:22:17 +02:00
:ok ->
2023-07-24 22:31:00 +02:00
{:ok, %{"categories" => get_categories()}}
2023-06-11 22:22:17 +02:00
_ ->
{:error, :insufficient_scope}
end
2023-07-17 22:23:58 +02:00
end
2023-07-24 22:31:00 +02:00
@impl true
def handle_channel_query(access_token) do
case Micropub.verify_token(access_token) do
:ok ->
{:ok, %{"channels" => get_channels()}}
_ ->
{:error, :insufficient_scope}
end
end
defp get_channels() do
channels = Chiya.Channels.list_channels()
Enum.map(channels, fn c ->
%{
"uid" => c.slug,
"name" => c.name
}
end)
end
defp get_categories() do
tags = Chiya.Tags.list_tags()
Enum.map(tags, fn t -> t.name end)
end
end