Merge pull request 'devel' (#215) from devel into main
Reviewed-on: #215
This commit is contained in:
commit
089481b8d7
4 changed files with 47 additions and 16 deletions
|
@ -4,9 +4,12 @@ defmodule ChiyaWeb.Indie.Micropub do
|
|||
alias ChiyaWeb.Indie.Properties, as: Props
|
||||
alias ChiyaWeb.Indie.Token
|
||||
|
||||
def create_note(type, properties, channel_id) do
|
||||
def create_note(type, properties) do
|
||||
settings = Chiya.Site.get_settings()
|
||||
|
||||
with {:ok, post_type} <- Props.get_post_type(properties),
|
||||
{:ok, note_attrs} <- get_attrs(type, post_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
|
||||
Logger.info("Note created!")
|
||||
|
||||
|
@ -50,12 +53,12 @@ defmodule ChiyaWeb.Indie.Micropub do
|
|||
)
|
||||
end
|
||||
|
||||
defp get_attrs(type, post_type, properties, default_channel_id) do
|
||||
defp get_attrs(type, post_type, properties, channel_id) do
|
||||
Logger.info("Creating a #{type}/#{post_type}..")
|
||||
|
||||
channel =
|
||||
if default_channel_id,
|
||||
do: Chiya.Channels.get_channel(default_channel_id),
|
||||
if channel_id,
|
||||
do: Chiya.Channels.get_channel(channel_id),
|
||||
else: nil
|
||||
|
||||
case post_type do
|
||||
|
@ -87,7 +90,7 @@ defmodule ChiyaWeb.Indie.Micropub do
|
|||
end
|
||||
end
|
||||
|
||||
defp get_note_attrs(p, default_channel) do
|
||||
defp get_note_attrs(p, channel) do
|
||||
content = Props.get_content(p)
|
||||
name = Props.get_title(p) || Chiya.Notes.Note.note_title(content)
|
||||
tags = Props.get_tags(p) |> Enum.join(",")
|
||||
|
@ -105,8 +108,8 @@ defmodule ChiyaWeb.Indie.Micropub do
|
|||
}
|
||||
|
||||
attrs =
|
||||
if default_channel,
|
||||
do: Map.put(attrs, :channel, default_channel),
|
||||
if channel,
|
||||
do: Map.put(attrs, :channels, [channel]),
|
||||
else: attrs
|
||||
|
||||
{:ok, attrs}
|
||||
|
|
|
@ -23,11 +23,8 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
|
|||
Logger.info("Properties: #{inspect(properties)}")
|
||||
Logger.info("Type: #{type}")
|
||||
|
||||
settings = Chiya.Site.get_settings()
|
||||
channel_id = settings.micropub_channel_id
|
||||
|
||||
case Micropub.verify_token(access_token) do
|
||||
:ok -> Micropub.create_note(type, properties, channel_id)
|
||||
:ok -> Micropub.create_note(type, properties)
|
||||
_ -> {:error, :invalid_request}
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,9 +2,11 @@ defmodule Chiya.NotesTest do
|
|||
use Chiya.DataCase
|
||||
|
||||
import Chiya.NotesFixtures
|
||||
import Chiya.ChannelsFixtures
|
||||
|
||||
alias Chiya.Notes
|
||||
alias Chiya.Notes.Note
|
||||
alias Chiya.Channels.Channel
|
||||
|
||||
describe "notes" do
|
||||
@invalid_attrs %{content: nil, kind: nil, name: nil, published_at: nil, slug: nil, url: nil}
|
||||
|
@ -20,12 +22,15 @@ defmodule Chiya.NotesTest do
|
|||
end
|
||||
|
||||
test "create_note/1 with valid data creates a note" do
|
||||
channel = channel_fixture()
|
||||
|
||||
valid_attrs = %{
|
||||
content: "some content",
|
||||
kind: "post",
|
||||
name: "some name",
|
||||
published_at: ~N[2023-03-04 16:22:00],
|
||||
url: "some url"
|
||||
url: "some url",
|
||||
channels: [channel]
|
||||
}
|
||||
|
||||
assert {:ok, %Note{} = note} = Notes.create_note(valid_attrs)
|
||||
|
@ -35,6 +40,7 @@ defmodule Chiya.NotesTest do
|
|||
assert note.published_at == ~N[2023-03-04 16:22:00]
|
||||
assert note.slug == "some-name"
|
||||
assert note.url == "some url"
|
||||
assert [%Channel{}] = note.channels
|
||||
end
|
||||
|
||||
test "create_note/1 with invalid data returns error changeset" do
|
||||
|
|
|
@ -2,6 +2,8 @@ defmodule ChiyaWeb.MicropubTest do
|
|||
use Chiya.DataCase
|
||||
|
||||
alias ChiyaWeb.Indie.Micropub
|
||||
alias Chiya.Notes.Note
|
||||
alias Chiya.Channels.Channel
|
||||
|
||||
@valid_props %{
|
||||
"content" => ["this is a test"]
|
||||
|
@ -10,9 +12,32 @@ defmodule ChiyaWeb.MicropubTest do
|
|||
describe "create_note/3" do
|
||||
test "creates a note with valid attributes" do
|
||||
assert {:ok, :created, url} =
|
||||
Micropub.create_note("entry", @valid_props, nil)
|
||||
Micropub.create_note("entry", @valid_props)
|
||||
|
||||
assert url =~ "this-is-a-test"
|
||||
note = Chiya.Notes.get_note_by_slug_preloaded("this-is-a-test")
|
||||
|
||||
assert url =~ note.slug
|
||||
assert %Note{} = note
|
||||
assert [%Channel{}] = note.channels
|
||||
end
|
||||
end
|
||||
|
||||
setup do
|
||||
{:ok, channel} =
|
||||
Chiya.Channels.create_channel(%{
|
||||
name: "Home",
|
||||
content: "Home channel"
|
||||
})
|
||||
|
||||
settings = Chiya.Site.get_settings()
|
||||
|
||||
Chiya.Site.update_setting(settings, %{
|
||||
home_channel_id: channel.id,
|
||||
default_channel_id: channel.id,
|
||||
micropub_channel_id: channel.id,
|
||||
wiki_channel_id: channel.id
|
||||
})
|
||||
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue