support bookmarks through micropub

This commit is contained in:
Inhji 2023-07-19 23:10:44 +02:00
parent ca140a8821
commit 95b4dd8303
2 changed files with 58 additions and 32 deletions

View file

@ -50,6 +50,10 @@
<span>Tags</span> <span>Tags</span>
<.tags note={@note} /> <.tags note={@note} />
<% end %> <% end %>
<%= if @note.kind != :post do %>
<.dot />
<span><%= @note.kind %></span>
<% end %>
<%= if @current_user do %> <%= if @current_user do %>
<.dot /> <.dot />
<a href={~p"/admin/notes/#{@note}"} class="underline-link font-semibold">Show in Admin</a> <a href={~p"/admin/notes/#{@note}"} class="underline-link font-semibold">Show in Admin</a>

View file

@ -6,31 +6,13 @@ defmodule ChiyaWeb.Indie.Micropub do
def create_note(type, properties) do def create_note(type, properties) do
settings = Chiya.Site.get_settings() settings = Chiya.Site.get_settings()
channel_id = settings.micropub_channel_id
with {:ok, post_type} <- Props.get_post_type(properties), with {:ok, note_attrs} <- get_attrs(type, properties, channel_id),
{:ok, note_attrs} <-
get_attrs(type, post_type, properties, settings.micropub_channel_id),
{:ok, note} <- Chiya.Notes.create_note(note_attrs) do {:ok, note} <- Chiya.Notes.create_note(note_attrs) do
create_photos(note, properties)
Logger.info("Note created!") Logger.info("Note created!")
# TODO: Make separate function for this
properties
|> Props.get_photos()
|> Enum.with_index()
|> Enum.map(fn {photo, index} ->
featured = index == 0
Chiya.Notes.create_note_image(%{
note_id: note.id,
path: photo.path,
featured: featured
})
end)
|> Enum.each(fn result ->
Logger.info("Photo created!")
Logger.info(inspect(result))
end)
{:ok, :created, Chiya.Notes.Note.note_url(note)} {:ok, :created, Chiya.Notes.Note.note_url(note)}
else else
error -> error ->
@ -41,6 +23,21 @@ defmodule ChiyaWeb.Indie.Micropub do
end end
end end
defp create_photos(note, properties) do
properties
|> Props.get_photos()
|> Enum.with_index()
|> Enum.each(fn {photo, index} ->
featured = index == 0
Chiya.Notes.create_note_image(%{
note_id: note.id,
path: photo.path,
featured: featured
})
end)
end
def verify_token(access_token) do def verify_token(access_token) do
Enum.reduce_while( Enum.reduce_while(
[ [
@ -57,7 +54,8 @@ defmodule ChiyaWeb.Indie.Micropub do
) )
end end
defp get_attrs(type, post_type, properties, channel_id) do defp get_attrs(type, properties, channel_id) do
{:ok, post_type} = Props.get_post_type(properties)
Logger.info("Creating a #{type}/#{post_type}..") Logger.info("Creating a #{type}/#{post_type}..")
channel = channel =
@ -67,6 +65,7 @@ defmodule ChiyaWeb.Indie.Micropub do
case post_type do case post_type do
:note -> get_note_attrs(properties, channel) :note -> get_note_attrs(properties, channel)
:bookmark -> get_bookmark_attrs(properties, channel)
_ -> {:error, :insufficient_scope} _ -> {:error, :insufficient_scope}
end end
end end
@ -94,13 +93,35 @@ defmodule ChiyaWeb.Indie.Micropub do
end end
end end
defp get_note_attrs(p, channel) do defp get_note_attrs(properties, channel) do
content = Props.get_content(p) attrs =
name = Props.get_title(p) || Chiya.Notes.Note.note_title(content) properties
tags = Props.get_tags(p) |> Enum.join(",") |> get_base_attrs()
|> get_channel(channel)
{:ok, attrs}
end
defp get_bookmark_attrs(properties, channel) do
url = Props.get_bookmarked_url(properties)
attrs =
properties
|> get_base_attrs()
|> get_channel(channel)
|> Map.put_new(:url, url)
|> Map.put_new(:kind, :bookmark)
{:ok, attrs}
end
defp get_base_attrs(properties) do
content = Props.get_content(properties)
name = Props.get_title(properties) || Chiya.Notes.Note.note_title(content)
tags = Props.get_tags(properties) |> Enum.join(",")
published_at = published_at =
if Props.is_published?(p), if Props.is_published?(properties),
do: NaiveDateTime.local_now(), do: NaiveDateTime.local_now(),
else: nil else: nil
@ -111,12 +132,13 @@ defmodule ChiyaWeb.Indie.Micropub do
published_at: published_at published_at: published_at
} }
attrs = attrs
if channel, end
do: Map.put(attrs, :channels, [channel]),
else: attrs
{:ok, attrs} def get_channel(attrs, channel) do
if channel,
do: Map.put(attrs, :channels, [channel]),
else: attrs
end end
defp get_hostname(), defp get_hostname(),