diff --git a/lib/chiya/notes/note.ex b/lib/chiya/notes/note.ex index faf5f43..05f22d0 100644 --- a/lib/chiya/notes/note.ex +++ b/lib/chiya/notes/note.ex @@ -45,6 +45,10 @@ defmodule Chiya.Notes.Note do timestamps() end + def note_url(note) do + URI.merge(ChiyaWeb.Endpoint.url, note.slug) |> to_string() + end + @doc false def changeset(note, attrs) do note diff --git a/lib/chiya_web/indie/micropub_handler.ex b/lib/chiya_web/indie/micropub_handler.ex index 99a748f..0797235 100644 --- a/lib/chiya_web/indie/micropub_handler.ex +++ b/lib/chiya_web/indie/micropub_handler.ex @@ -1,15 +1,33 @@ defmodule ChiyaWeb.Indie.MicropubHandler do + require Logger + + @impl true def handle_create(type, properties, access_token) do - properties = handle_uploads(properties) - params = %{type: type, properties: properties} - # Chiya.Notes.create_note - id = 0 - {:ok, :created, ""} + with :ok <- ChiyaWeb.Indie.Token.verify(access_token, "create", get_hostname()), + {:ok, note_attrs} <- note_attrs(type, properties), + {:ok, note} <- Chiya.Notes.create_note(note_attrs) do + # After token has been verified, + # note attrs have been created, + # we can create the note and return its url! + {:ok, :created, Chiya.Notes.Note.note_url(note)} + 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} + defp note_attrs(type, properties) do + end + defp handle_uploads(props) do end + + defp get_hostname(), + do: URI.parse(ChiyaWeb.Endpoint.url()).host end diff --git a/lib/chiya_web/indie/properties.ex b/lib/chiya_web/indie/properties.ex new file mode 100644 index 0000000..47ba5eb --- /dev/null +++ b/lib/chiya_web/indie/properties.ex @@ -0,0 +1,66 @@ +defmodule ChiyaWeb.Indie.Properties do + def get_post_type(properties) do + cond do + Map.has_key?(properties, "like-of") -> + {:ok, :like} + + Map.has_key?(properties, "bookmark-of") -> + {:ok, :bookmark} + + Map.has_key?(properties, "content") -> + {:ok, :note} + + true -> + {:error, :unsupported_posttype} + end + end + + def get_tags(%{"category" => [""]} = _props), do: [] + def get_tags(%{"category" => tags} = _props), do: tags + def get_tags(_props), do: [] + + def get_title(%{"name" => [title]} = _props), do: title + def get_title(_props), do: nil + + def get_content(%{"content" => [%{"html" => content_html}]} = _props), do: content_html + def get_content(%{"content" => [content]} = _props), do: content + def get_content(_props), do: nil + + def get_bookmarked_url(%{"bookmark-of" => [url]} = _props), do: url + def get_bookmarked_url(_props), do: nil + + def get_reposted_url(%{"repost-of" => [url]} = _props), do: url + def get_reposted_url(_props), do: nil + + def get_liked_url(%{"like-of" => [url]} = _props), do: url + def get_liked_url(_props), do: nil + + def get_read_url(%{"read-of" => [url]} = _props), do: url + def get_read_url(_props), do: nil + + def get_watched_url(%{"watch-of" => [url]} = _props), do: url + def get_watched_url(_props), do: nil + + def get_listened_url(%{"listen-of" => [url]} = _props), do: url + def get_listened_url(_props), do: nil + + def get_reply_to(%{"in-reply-to" => [reply_to]} = _props), do: reply_to + def get_reply_to(_props), do: nil + + def is_published?(%{"post-status" => ["draft"]} = _props), do: false + def is_published?(_props), do: true + + def get_photo(%{"photo" => [photo]} = _props), do: photo + def get_photo(_props), do: nil + + def get_syndication_targets(%{"mp-syndicate-to" => targets} = _props), do: targets + def get_syndication_targets(_props), do: [] + + def get_channel(%{"mp-channel" => [channel]} = _props), do: channel + def get_channel(_props), do: nil + + def has_target?(%{"mp-syndicate-to" => targets} = _props, name), + do: Enum.any?(targets, fn t -> t == name end) + + def has_target?(_props, _name), do: false +end