Merge pull request 'devel' (#229) from devel into main

Reviewed-on: #229
This commit is contained in:
inhji 2023-07-24 22:36:35 +02:00
commit 8f7a347f2f
7 changed files with 106 additions and 33 deletions

View file

@ -8,7 +8,7 @@
src={ChiyaWeb.Uploaders.NoteImage.url({@image.path, @image}, :full_dithered)}
/>
<pre>![<%= @image.content %>](<%= ChiyaWeb.Uploaders.NoteImage.url({@image.path, @image}, :full_dithered) %>)</pre>
<pre class="mt-4 p-1 bg-gray-100 text-black rounded select-all">![<%= @image.content %>](<%= ChiyaWeb.Uploaders.NoteImage.url({@image.path, @image}, :full_dithered) %>)</pre>
<.simple_form
:let={f}

View file

@ -14,14 +14,14 @@
<section class="flex flex-row flex-wrap mt-6 -mb-6 gap-3">
<a
href={~p"/admin/notes"}
class="dark:text-gray-300 rounded-full bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700 px-4 py-2 border border-gray-300 dark:border-gray-600 shadow-sm transition"
class="text-sm dark:text-gray-300 rounded-full bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700 px-4 py-2 border border-gray-300 dark:border-gray-600 shadow-sm transition"
>
All
</a>
<%= for channel <- @channels do %>
<a
href={~p"/admin/notes?channel=#{channel.slug}"}
class="dark:text-gray-300 rounded-full bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700 px-4 py-2 border border-gray-300 dark:border-gray-600 shadow-sm transition"
class="text-sm dark:text-gray-300 rounded-full bg-gray-100 hover:bg-gray-200 dark:bg-gray-800 dark:hover:bg-gray-700 px-4 py-2 border border-gray-300 dark:border-gray-600 shadow-sm transition"
>
<%= channel.name %>
<span class="text-gray-600 dark:text-gray-500">(<%= Enum.count(channel.notes) %>)</span>

View file

@ -8,7 +8,7 @@ defmodule ChiyaWeb.Indie.Micropub do
settings = Chiya.Site.get_settings()
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
create_photos(note, properties)
@ -25,19 +25,17 @@ defmodule ChiyaWeb.Indie.Micropub do
def find_note(note_url) do
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
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),
def update_note(note, replace, add, delete) do
with {:ok, note_attrs} <- get_update_attrs(replace, add, delete),
{:ok, note} <- Chiya.Notes.update_note(note, note_attrs) do
Logger.info("Note updated!")
{:ok, note}
@ -81,7 +79,7 @@ defmodule ChiyaWeb.Indie.Micropub do
)
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)
Logger.info("Creating a #{type}/#{post_type}..")
@ -97,6 +95,51 @@ defmodule ChiyaWeb.Indie.Micropub do
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
Token.verify(access_token, "create", get_hostname())
end

View file

@ -44,7 +44,7 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
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, _note} <- Micropub.update_note(note, replace, add, delete) do
:ok
else
error -> error
@ -98,19 +98,11 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
def handle_config_query(access_token) do
case Micropub.verify_token(access_token) do
:ok ->
channels = Chiya.Channels.list_channels()
config = %{
"media-endpoint" => url(~p"/indie/micropub/media"),
"destination" => [],
"post-types" => @post_types,
"channels" =>
Enum.map(channels, fn c ->
%{
"uid" => c.slug,
"name" => c.name
}
end)
"channels" => get_channels()
}
{:ok, config}
@ -132,11 +124,37 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
def handle_category_query(access_token) do
case Micropub.verify_token(access_token) do
:ok ->
tags = Enum.map(Chiya.Tags.list_tags(), fn t -> t.name end)
{:ok, %{"categories" => tags}}
{:ok, %{"categories" => get_categories()}}
_ ->
{:error, :insufficient_scope}
end
end
@impl true
def handle_channel_query(access_token) do
case Micropub.verify_token(access_token) do
:ok ->
{:ok, %{"channels" => get_channels()}}
_ ->
{:error, :insufficient_scope}
end
end
defp get_channels() do
channels = Chiya.Channels.list_channels()
Enum.map(channels, fn c ->
%{
"uid" => c.slug,
"name" => c.name
}
end)
end
defp get_categories() do
tags = Chiya.Tags.list_tags()
Enum.map(tags, fn t -> t.name end)
end
end

View file

@ -50,7 +50,7 @@ defmodule Chiya.MixProject do
{:phoenix_live_reload, "~> 1.2", only: :dev},
{:phoenix_live_view, "~> 0.19"},
{:plug_cowboy, "~> 2.5"},
{:plug_micropub, git: "https://git.inhji.de/inhji/plug_micropub.git", branch: "main"},
{:plug_micropub, git: "https://git.inhji.de/inhji/plug_micropub.git", branch: "chiya"},
{:postgrex, ">= 0.0.0"},
{:swoosh, "~> 1.11"},
{:tailwind, "~> 0.2.0", runtime: Mix.env() == :dev},

View file

@ -44,7 +44,7 @@
"plug": {:hex, :plug, "1.14.2", "cff7d4ec45b4ae176a227acd94a7ab536d9b37b942c8e8fa6dfc0fff98ff4d80", [:mix], [{:mime, "~> 1.0 or ~> 2.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.1.1 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4.3 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "842fc50187e13cf4ac3b253d47d9474ed6c296a8732752835ce4a86acdf68d13"},
"plug_cowboy": {:hex, :plug_cowboy, "2.6.1", "9a3bbfceeb65eff5f39dab529e5cd79137ac36e913c02067dba3963a26efe9b2", [:mix], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:cowboy_telemetry, "~> 0.3", [hex: :cowboy_telemetry, repo: "hexpm", optional: false]}, {:plug, "~> 1.14", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "de36e1a21f451a18b790f37765db198075c25875c64834bcc82d90b309eb6613"},
"plug_crypto": {:hex, :plug_crypto, "1.2.5", "918772575e48e81e455818229bf719d4ab4181fcbf7f85b68a35620f78d89ced", [:mix], [], "hexpm", "26549a1d6345e2172eb1c233866756ae44a9609bd33ee6f99147ab3fd87fd842"},
"plug_micropub": {:git, "https://git.inhji.de/inhji/plug_micropub.git", "c06eacc1252442626cfb3da7333668a1e68460c3", [branch: "main"]},
"plug_micropub": {:git, "https://git.inhji.de/inhji/plug_micropub.git", "1845a4067703fc656fec34b8732b9544967893c0", [branch: "chiya"]},
"poison": {:hex, :poison, "4.0.1", "bcb755a16fac91cad79bfe9fc3585bb07b9331e50cfe3420a24bcc2d735709ae", [:mix], [], "hexpm", "ba8836feea4b394bb718a161fc59a288fe0109b5006d6bdf97b6badfcf6f0f25"},
"postgrex": {:hex, :postgrex, "0.17.1", "01c29fd1205940ee55f7addb8f1dc25618ca63a8817e56fac4f6846fc2cddcbe", [:mix], [{:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "14b057b488e73be2beee508fb1955d8db90d6485c6466428fe9ccf1d6692a555"},
"ranch": {:hex, :ranch, "1.8.0", "8c7a100a139fd57f17327b6413e4167ac559fbc04ca7448e9be9057311597a1d", [:make, :rebar3], [], "hexpm", "49fbcfd3682fab1f5d109351b61257676da1a2fdbe295904176d5e521a2ddfe5"},

View file

@ -4,7 +4,7 @@ defmodule ChiyaWeb.MicropubTest do
alias ChiyaWeb.Indie.Micropub
alias Chiya.Notes.Note
alias Chiya.Channels.Channel
import Chiya.NoteFixtures
import Chiya.NotesFixtures
@valid_props %{
"content" => ["this is a test"]
@ -24,10 +24,22 @@ defmodule ChiyaWeb.MicropubTest do
end
describe "update_note" do
test "updates a note" do
test "updates a note by replacing content" do
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