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

Reviewed-on: #210
This commit is contained in:
inhji 2023-07-17 19:01:22 +02:00
commit 66de20f936
11 changed files with 113 additions and 28 deletions

View file

@ -5,7 +5,7 @@ defmodule Chiya.Notes do
import Ecto.Query, warn: false import Ecto.Query, warn: false
alias Chiya.Repo alias Chiya.Repo
alias Chiya.Notes.{Note, NoteImage, NoteNote, NoteTag, NoteComment} alias Chiya.Notes.{Note, NoteImage, NoteImageTemp, NoteNote, NoteTag, NoteComment}
@preloads [ @preloads [
:channels, :channels,
@ -262,6 +262,13 @@ defmodule Chiya.Notes do
Note.changeset(note, attrs) Note.changeset(note, attrs)
end end
@doc """
Returns an `%Ecto.Changeset{}` for tracking note_image changes.
"""
def change_note_image(%NoteImage{} = note_image, attrs \\ %{}) do
NoteImage.update_changeset(note_image, attrs)
end
@doc """ @doc """
Gets a single note image. Gets a single note image.
""" """
@ -296,6 +303,12 @@ defmodule Chiya.Notes do
:ok :ok
end end
def create_note_image_temp(attrs \\ %{}) do
%NoteImageTemp{}
|> NoteImageTemp.changeset(attrs)
|> Repo.insert()
end
def get_note_note!(attrs \\ %{}) do def get_note_note!(attrs \\ %{}) do
Repo.get_by!(NoteNote, attrs) Repo.get_by!(NoteNote, attrs)
end end
@ -314,13 +327,6 @@ defmodule Chiya.Notes do
Repo.delete(note_note) Repo.delete(note_note)
end end
@doc """
Returns an `%Ecto.Changeset{}` for tracking note_image changes.
"""
def change_note_image(%NoteImage{} = note_image, attrs \\ %{}) do
NoteImage.update_changeset(note_image, attrs)
end
def get_note_tag!(attrs \\ %{}) do def get_note_tag!(attrs \\ %{}) do
Repo.get_by!(NoteTag, attrs) Repo.get_by!(NoteTag, attrs)
end end

View file

@ -3,6 +3,11 @@ defmodule Chiya.Notes.NoteImage do
use Waffle.Ecto.Schema use Waffle.Ecto.Schema
import Ecto.Changeset import Ecto.Changeset
@attachment_options [
allow_paths: true,
allow_urls: true
]
schema "note_images" do schema "note_images" do
field :content, :string, default: "" field :content, :string, default: ""
field :path, ChiyaWeb.Uploaders.NoteImage.Type field :path, ChiyaWeb.Uploaders.NoteImage.Type
@ -24,7 +29,7 @@ defmodule Chiya.Notes.NoteImage do
def update_changeset(note_image, attrs) do def update_changeset(note_image, attrs) do
note_image note_image
|> cast(attrs, [:content, :note_id, :featured]) |> cast(attrs, [:content, :note_id, :featured])
|> cast_attachments(attrs, [:path], allow_paths: true) |> cast_attachments(attrs, [:path], @attachment_options)
|> validate_required([:path, :note_id]) |> validate_required([:path, :note_id])
end end
end end

View file

@ -0,0 +1,20 @@
defmodule Chiya.Notes.NoteImageTemp do
use Ecto.Schema
use Waffle.Ecto.Schema
import Ecto.Changeset
schema "note_images_temp" do
field :content, :string, default: ""
field :path, ChiyaWeb.Uploaders.NoteImage.Type
timestamps()
end
@doc false
def changeset(note_image, attrs) do
note_image
|> cast(attrs, [:content])
|> cast_attachments(attrs, [:path], allow_paths: true)
|> validate_required([:path])
end
end

View file

@ -1,6 +1,6 @@
<html> <html>
<head> <head>
<title>Not found</title> <title>Not Found</title>
<link rel="preconnect" href="https://rsms.me/" /> <link rel="preconnect" href="https://rsms.me/" />
<link rel="stylesheet" href="https://rsms.me/inter/inter.css" /> <link rel="stylesheet" href="https://rsms.me/inter/inter.css" />
<link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} /> <link phx-track-static rel="stylesheet" href={~p"/assets/app.css"} />

View file

@ -16,10 +16,10 @@ defmodule ChiyaWeb.PageHTML do
def do_render_outline(%{text: text, children: children, level: _level}) do def do_render_outline(%{text: text, children: children, level: _level}) do
slug = Slugger.slugify_downcase(text) slug = Slugger.slugify_downcase(text)
content_tag(:ul, [class: "m-0"], content_tag(:ul, [class: "m-0"],
do: [ do: [
content_tag(:li, do: content_tag(:li, do: content_tag(:a, text, href: "##{slug}")),
content_tag(:a, text, href: "##{slug}")),
Enum.map(children, &do_render_outline/1) Enum.map(children, &do_render_outline/1)
] ]
) )

View file

@ -3,22 +3,23 @@
<%= @note.name %> <%= @note.name %>
</.header> </.header>
<aside class="max-w-2xl mx-auto mt-8 prose prose-gruvbox bg-theme-background1 rounded p-2 empty:hidden"><%= raw render_outline(@note) %></aside> <aside class="max-w-2xl mx-auto mt-8 prose prose-gruvbox bg-theme-background1 rounded p-2 empty:hidden">
<%= raw(render_outline(@note)) %>
</aside>
<section class="mt-8 mx-auto prose prose-gruvbox md:prose-lg lg:prose-xl | p-summary e-content"> <section class="mt-8 mx-auto prose prose-gruvbox md:prose-lg lg:prose-xl | p-summary e-content">
<%= Markdown.render(@note.content) |> raw %> <%= Markdown.render(@note.content) |> raw %>
</section> </section>
<%= if not Enum.empty?(@note.links_to) do %> <%= if not Enum.empty?(@note.links_to) do %>
<section class="mt-8 prose prose-gruvbox max-w-2xl mx-auto"> <section class="mt-8 prose prose-gruvbox max-w-2xl mx-auto">
<.divider text="" /> <.divider text="" /> Notes linking here:
Notes linking here: <ul class="">
<ul class=""> <%= for link <- @note.links_to do %>
<%= for link <- @note.links_to do %> <li><a href={~p"/note/#{link.slug}"}><%= link.name %></a></li>
<li><a href={~p"/note/#{link.slug}"}><%= link.name %></a></li> <% end %>
<% end %> </ul>
</ul> </section>
</section>
<% end %> <% end %>
<footer class="max-w-2xl mx-auto mt-8 text-theme-base"> <footer class="max-w-2xl mx-auto mt-8 text-theme-base">

View file

@ -28,6 +28,18 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
{:ok, note_attrs} <- get_attrs(type, post_type, properties, micropub_channel_id), {:ok, note_attrs} <- get_attrs(type, post_type, properties, micropub_channel_id),
{:ok, note} <- Chiya.Notes.create_note(note_attrs) do {:ok, note} <- Chiya.Notes.create_note(note_attrs) do
Logger.info("Note created!") Logger.info("Note created!")
# TODO: Make separate function for this
note_attrs
|> Props.get_photos()
|> Enum.map(fn photo_url ->
Chiya.Notes.create_note_image(%{
note_id: note.id,
path: photo_url
})
end)
|> Enum.each(&IO.inspect/1)
{:ok, :created, Chiya.Notes.Note.note_url(note)} {:ok, :created, Chiya.Notes.Note.note_url(note)}
else else
error -> error ->
@ -54,8 +66,15 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
end end
@impl true @impl true
def handle_media(_files, _access_token) do def handle_media(file, access_token) do
{:error, :insufficient_scope} with :ok <- verify_token(access_token),
{:ok, image} <- Chiya.Notes.create_note_image_temp(%{path: file.path}) do
url = ChiyaWeb.Uploaders.UserImageTemp.url({image.path, image}, :original)
{:ok, url}
else
_ ->
{:error, :insufficient_scope}
end
end end
@impl true @impl true

View file

@ -50,8 +50,8 @@ defmodule ChiyaWeb.Indie.Properties do
def is_published?(%{"post-status" => ["draft"]} = _props), do: false def is_published?(%{"post-status" => ["draft"]} = _props), do: false
def is_published?(_props), do: true def is_published?(_props), do: true
def get_photo(%{"photo" => [photo]} = _props), do: photo def get_photos(%{"photo" => photos} = _props), do: photos
def get_photo(_props), do: nil def get_photos(_props), do: []
def get_syndication_targets(%{"mp-syndicate-to" => targets} = _props), do: targets def get_syndication_targets(%{"mp-syndicate-to" => targets} = _props), do: targets
def get_syndication_targets(_props), do: [] def get_syndication_targets(_props), do: []

View file

@ -0,0 +1,34 @@
defmodule ChiyaWeb.Uploaders.NoteImageTemp do
use Waffle.Definition
use Waffle.Ecto.Definition
# Include ecto support (requires package waffle_ecto installed):
# use Waffle.Ecto.Definition
@versions [:original]
# Whitelist file extensions:
def validate({file, _}) do
file_extension = file.file_name |> Path.extname() |> String.downcase()
case Enum.member?(~w(.jpg .jpeg .gif .png), file_extension) do
true -> :ok
false -> {:error, "invalid file type"}
end
end
# Override the persisted filenames:
def filename(_version, {_file, %{id: image_id}}) do
image_id
end
# Override the storage directory:
def storage_dir(_version, _scope) do
"uploads/temp"
end
# Provide a default URL if there hasn't been a file uploaded
# def default_url(version, scope) do
# "/images/avatars/default_#{version}.png"
# end
end

View file

@ -51,7 +51,7 @@ defmodule Chiya.TagUpdaterTest do
assert Enum.count(note.tags) == 1 assert Enum.count(note.tags) == 1
tag = List.first(note.tags) tag = List.first(note.tags)
assert tag.name == "Foo" assert tag.name == "foo"
end end
end end
end end

View file

@ -9,6 +9,6 @@ defmodule ChiyaWeb.ErrorHTMLTest do
end end
test "renders 500.html" do test "renders 500.html" do
assert render_to_string(ChiyaWeb.ErrorHTML, "500", "html", []) =~ "Internal Server Error" assert render_to_string(ChiyaWeb.ErrorHTML, "500", "html", []) =~ "Infernal Server Error"
end end
end end