WIP micropub update
This commit is contained in:
parent
dcdbebbdfc
commit
f2a9e422e4
2 changed files with 71 additions and 16 deletions
|
@ -8,7 +8,7 @@ defmodule ChiyaWeb.Indie.Micropub do
|
||||||
settings = Chiya.Site.get_settings()
|
settings = Chiya.Site.get_settings()
|
||||||
channel_id = settings.micropub_channel_id
|
channel_id = settings.micropub_channel_id
|
||||||
|
|
||||||
with {:ok, note_attrs} <- get_attrs(type, properties, channel_id),
|
with {:ok, note_attrs} <- get_create_attrs(type, properties, channel_id),
|
||||||
{:ok, note} <- Chiya.Notes.create_note(note_attrs) do
|
{:ok, note} <- Chiya.Notes.create_note(note_attrs) do
|
||||||
create_photos(note, properties)
|
create_photos(note, properties)
|
||||||
|
|
||||||
|
@ -25,19 +25,17 @@ defmodule ChiyaWeb.Indie.Micropub do
|
||||||
|
|
||||||
def find_note(note_url) do
|
def find_note(note_url) do
|
||||||
slug = Chiya.Notes.Note.note_slug(note_url)
|
slug = Chiya.Notes.Note.note_slug(note_url)
|
||||||
Chiya.Notes.get_note_preloaded_by_slug(slug)
|
note = Chiya.Notes.get_note_by_slug_preloaded(slug)
|
||||||
|
|
||||||
|
if is_nil(note) do
|
||||||
|
{:error, :invalid_request}
|
||||||
|
else
|
||||||
|
{:ok, note}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_note(note, replace, add, _delete) do
|
def update_note(note, replace, add, delete) do
|
||||||
settings = Chiya.Site.get_settings()
|
with {:ok, note_attrs} <- get_update_attrs(replace, add, delete),
|
||||||
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
|
{:ok, note} <- Chiya.Notes.update_note(note, note_attrs) do
|
||||||
Logger.info("Note updated!")
|
Logger.info("Note updated!")
|
||||||
{:ok, note}
|
{:ok, note}
|
||||||
|
@ -81,7 +79,7 @@ defmodule ChiyaWeb.Indie.Micropub do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp get_attrs(type, properties, channel_id) do
|
defp get_create_attrs(type, properties, channel_id) do
|
||||||
{:ok, post_type} = Props.get_post_type(properties)
|
{:ok, post_type} = Props.get_post_type(properties)
|
||||||
Logger.info("Creating a #{type}/#{post_type}..")
|
Logger.info("Creating a #{type}/#{post_type}..")
|
||||||
|
|
||||||
|
@ -97,6 +95,51 @@ defmodule ChiyaWeb.Indie.Micropub do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp get_update_attrs(replace, add, _delete) do
|
||||||
|
replace_attrs = put_update_attrs(replace)
|
||||||
|
add_attrs = put_update_attrs(add)
|
||||||
|
|
||||||
|
attrs =
|
||||||
|
%{}
|
||||||
|
|> Enum.into(replace_attrs)
|
||||||
|
|> Enum.into(add_attrs)
|
||||||
|
|
||||||
|
{:ok, attrs}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp put_update_attrs(source) do
|
||||||
|
name = Props.get_title(source)
|
||||||
|
attrs = %{}
|
||||||
|
|
||||||
|
attrs =
|
||||||
|
if name do
|
||||||
|
Map.put(attrs, :name, name)
|
||||||
|
else
|
||||||
|
attrs
|
||||||
|
end
|
||||||
|
|
||||||
|
content = Props.get_content(source)
|
||||||
|
|
||||||
|
attrs =
|
||||||
|
if content do
|
||||||
|
Map.put(attrs, :content, content)
|
||||||
|
else
|
||||||
|
attrs
|
||||||
|
end
|
||||||
|
|
||||||
|
tags = Props.get_tags(source)
|
||||||
|
|
||||||
|
attrs =
|
||||||
|
if !Enum.empty?(tags) do
|
||||||
|
tags_string = Enum.join(tags, ",")
|
||||||
|
Map.put(attrs, :tags_string, tags_string)
|
||||||
|
else
|
||||||
|
attrs
|
||||||
|
end
|
||||||
|
|
||||||
|
attrs
|
||||||
|
end
|
||||||
|
|
||||||
defp verify_micropub_token(access_token) do
|
defp verify_micropub_token(access_token) do
|
||||||
Token.verify(access_token, "create", get_hostname())
|
Token.verify(access_token, "create", get_hostname())
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,7 @@ defmodule ChiyaWeb.MicropubTest do
|
||||||
alias ChiyaWeb.Indie.Micropub
|
alias ChiyaWeb.Indie.Micropub
|
||||||
alias Chiya.Notes.Note
|
alias Chiya.Notes.Note
|
||||||
alias Chiya.Channels.Channel
|
alias Chiya.Channels.Channel
|
||||||
import Chiya.NoteFixtures
|
import Chiya.NotesFixtures
|
||||||
|
|
||||||
@valid_props %{
|
@valid_props %{
|
||||||
"content" => ["this is a test"]
|
"content" => ["this is a test"]
|
||||||
|
@ -24,10 +24,22 @@ defmodule ChiyaWeb.MicropubTest do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "update_note" do
|
describe "update_note" do
|
||||||
test "updates a note" do
|
test "updates a note by replacing content" do
|
||||||
note = note_fixture()
|
note = note_fixture()
|
||||||
|
|
||||||
assert :ok = Micropub.update_note(note, %{"content" => ["replaced content"]})
|
assert {:ok, %Note{} = note} =
|
||||||
|
Micropub.update_note(note, %{"content" => ["replaced content"]}, %{}, %{})
|
||||||
|
|
||||||
|
assert note.content == "replaced content"
|
||||||
|
end
|
||||||
|
|
||||||
|
test "updates a note by adding categories" do
|
||||||
|
note = note_fixture()
|
||||||
|
|
||||||
|
assert {:ok, %Note{} = note} =
|
||||||
|
Micropub.update_note(note, %{}, %{"category" => ["foo", "bar"]}, %{})
|
||||||
|
|
||||||
|
assert Enum.empty?(note.tags) == false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue