From 42565be50c35541059ecaafc340c8f8ec0ece8c7 Mon Sep 17 00:00:00 2001 From: Inhji Date: Mon, 17 Jul 2023 22:23:58 +0200 Subject: [PATCH 1/2] fix missing micropub_channel --- lib/chiya_web/indie/micropub.ex | 19 +++++++++++-------- lib/chiya_web/indie/micropub_handler.ex | 7 ++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/lib/chiya_web/indie/micropub.ex b/lib/chiya_web/indie/micropub.ex index 5c87ddc..e943fda 100644 --- a/lib/chiya_web/indie/micropub.ex +++ b/lib/chiya_web/indie/micropub.ex @@ -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} diff --git a/lib/chiya_web/indie/micropub_handler.ex b/lib/chiya_web/indie/micropub_handler.ex index 3dc09e7..1739831 100644 --- a/lib/chiya_web/indie/micropub_handler.ex +++ b/lib/chiya_web/indie/micropub_handler.ex @@ -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 @@ -129,5 +126,5 @@ defmodule ChiyaWeb.Indie.MicropubHandler do _ -> {:error, :insufficient_scope} end - end + end end From ecfa7e236b36ce10354295a9a848c2e22a52643f Mon Sep 17 00:00:00 2001 From: Inhji Date: Mon, 17 Jul 2023 22:24:17 +0200 Subject: [PATCH 2/2] add micropub and note test --- test/chiya/notes_test.exs | 8 ++++++- test/chiya_web/indie/micropub_test.exs | 29 ++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/test/chiya/notes_test.exs b/test/chiya/notes_test.exs index 6e9bd0b..e261b31 100644 --- a/test/chiya/notes_test.exs +++ b/test/chiya/notes_test.exs @@ -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 diff --git a/test/chiya_web/indie/micropub_test.exs b/test/chiya_web/indie/micropub_test.exs index 1d41a64..bba5199 100644 --- a/test/chiya_web/indie/micropub_test.exs +++ b/test/chiya_web/indie/micropub_test.exs @@ -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