From 2e4d61d587114fe2b34b4fb16d8b0de09ec87b3b Mon Sep 17 00:00:00 2001 From: Inhji Date: Mon, 24 Jul 2023 18:39:39 +0200 Subject: [PATCH] WIP: micropub update note --- lib/chiya_web/indie/micropub.ex | 29 ++++++++++++++++++++++++- lib/chiya_web/indie/micropub_handler.ex | 10 +++++++-- test/chiya_web/indie/micropub_test.exs | 9 ++++++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/lib/chiya_web/indie/micropub.ex b/lib/chiya_web/indie/micropub.ex index d5e1b31..35a7834 100644 --- a/lib/chiya_web/indie/micropub.ex +++ b/lib/chiya_web/indie/micropub.ex @@ -11,8 +11,8 @@ defmodule ChiyaWeb.Indie.Micropub do with {:ok, note_attrs} <- get_attrs(type, properties, channel_id), {:ok, note} <- Chiya.Notes.create_note(note_attrs) do create_photos(note, properties) - Logger.info("Note created!") + Logger.info("Note created!") {:ok, :created, Chiya.Notes.Note.note_url(note)} else error -> @@ -23,6 +23,33 @@ defmodule ChiyaWeb.Indie.Micropub do end end + def find_note(note_url) do + slug = Chiya.Notes.Note.note_slug(note_url) + Chiya.Notes.get_note_preloaded_by_slug(slug) + end + + def update_note(note, replace, add, _delete) do + settings = Chiya.Site.get_settings() + channel_id = settings.micropub_channel_id + + properties = + %{} + |> Enum.into(replace) + |> Enum.into(add) + + with {:ok, note_attrs} <- get_attrs("entry", properties, channel_id), + {:ok, note} <- Chiya.Notes.update_note(note, note_attrs) do + Logger.info("Note updated!") + {:ok, note} + else + error -> + Logger.error("Error occurred while creating note from micropub:") + Logger.error(inspect(error)) + + {:error, :invalid_request} + end + end + defp create_photos(note, properties) do properties |> Props.get_photos() diff --git a/lib/chiya_web/indie/micropub_handler.ex b/lib/chiya_web/indie/micropub_handler.ex index 477f402..548cf46 100644 --- a/lib/chiya_web/indie/micropub_handler.ex +++ b/lib/chiya_web/indie/micropub_handler.ex @@ -41,8 +41,14 @@ defmodule ChiyaWeb.Indie.MicropubHandler do end @impl true - def handle_update(_, _, _, _, _) do - {:error, :insufficient_scope} + def handle_update(url, replace, add, delete, access_token) do + with :ok <- Micropub.verify_token(access_token), + {:ok, note} <- Micropub.find_note(url), + {:ok, note} <- Micropub.update_note(note, replace, add, delete) do + :ok + else + error -> error + end end @impl true diff --git a/test/chiya_web/indie/micropub_test.exs b/test/chiya_web/indie/micropub_test.exs index bba5199..93c1d9b 100644 --- a/test/chiya_web/indie/micropub_test.exs +++ b/test/chiya_web/indie/micropub_test.exs @@ -4,6 +4,7 @@ defmodule ChiyaWeb.MicropubTest do alias ChiyaWeb.Indie.Micropub alias Chiya.Notes.Note alias Chiya.Channels.Channel + import Chiya.NoteFixtures @valid_props %{ "content" => ["this is a test"] @@ -22,6 +23,14 @@ defmodule ChiyaWeb.MicropubTest do end end + describe "update_note" do + test "updates a note" do + note = note_fixture() + + assert :ok = Micropub.update_note(note, %{"content" => ["replaced content"]}) + end + end + setup do {:ok, channel} = Chiya.Channels.create_channel(%{