prepare default channel for micropub

This commit is contained in:
Inhji 2023-06-12 18:50:00 +02:00
parent 8ef8ad14b2
commit 9c6ec988f1
2 changed files with 12 additions and 6 deletions

View file

@ -31,7 +31,8 @@ defmodule Chiya.Site.Setting do
:custom_css, :custom_css,
:custom_html, :custom_html,
:home_channel_id, :home_channel_id,
:default_channel_id :default_channel_id,
:micropub_channel_id
]) ])
|> validate_required([:title, :subtitle, :theme, :user_agent]) |> validate_required([:title, :subtitle, :theme, :user_agent])
end end

View file

@ -20,9 +20,12 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
Logger.info("Properties: #{inspect(properties)}") Logger.info("Properties: #{inspect(properties)}")
Logger.info("Type: #{type}") Logger.info("Type: #{type}")
settings = Chiya.Site.get_settings()
micropub_channel = settings.micropub_channel_id
with :ok <- verify_token(access_token), with :ok <- verify_token(access_token),
{:ok, post_type} <- Props.get_post_type(properties), {:ok, post_type} <- Props.get_post_type(properties),
{:ok, note_attrs} <- get_attrs(type, post_type, properties), {:ok, note_attrs} <- get_attrs(type, post_type, properties, micropub_channel),
{:ok, note} <- Chiya.Notes.create_note(note_attrs) do {:ok, note} <- Chiya.Notes.create_note(note_attrs) do
Logger.info("Note created!") Logger.info("Note created!")
{:ok, :created, Chiya.Notes.Note.note_url(note)} {:ok, :created, Chiya.Notes.Note.note_url(note)}
@ -157,16 +160,16 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
end end
end end
defp get_attrs(type, post_type, properties) do defp get_attrs(type, post_type, properties, default_channel) do
Logger.info("Creating a #{type}/#{post_type}..") Logger.info("Creating a #{type}/#{post_type}..")
case post_type do case post_type do
:note -> get_note_attrs(properties) :note -> get_note_attrs(properties, default_channel)
_ -> {:error, :insufficient_scope} _ -> {:error, :insufficient_scope}
end end
end end
defp get_note_attrs(p) do defp get_note_attrs(p, default_channel) do
content = Props.get_content(p) content = Props.get_content(p)
name = Props.get_title(p) || String.slice(content, 0..15) name = Props.get_title(p) || String.slice(content, 0..15)
tags = Props.get_tags(p) |> Enum.join(",") tags = Props.get_tags(p) |> Enum.join(",")
@ -176,12 +179,14 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
do: NaiveDateTime.local_now(), do: NaiveDateTime.local_now(),
else: nil else: nil
# TODO: Add default channel
{:ok, {:ok,
%{ %{
content: content, content: content,
name: name, name: name,
tags_string: tags, tags_string: tags,
published_at: published_at published_at: published_at,
}} }}
end end