devel #210

Merged
inhji merged 4 commits from devel into main 2023-07-17 19:01:23 +02:00
11 changed files with 113 additions and 28 deletions

View file

@ -5,7 +5,7 @@ defmodule Chiya.Notes do
import Ecto.Query, warn: false
alias Chiya.Repo
alias Chiya.Notes.{Note, NoteImage, NoteNote, NoteTag, NoteComment}
alias Chiya.Notes.{Note, NoteImage, NoteImageTemp, NoteNote, NoteTag, NoteComment}
@preloads [
:channels,
@ -262,6 +262,13 @@ defmodule Chiya.Notes do
Note.changeset(note, attrs)
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 """
Gets a single note image.
"""
@ -296,6 +303,12 @@ defmodule Chiya.Notes do
:ok
end
def create_note_image_temp(attrs \\ %{}) do
%NoteImageTemp{}
|> NoteImageTemp.changeset(attrs)
|> Repo.insert()
end
def get_note_note!(attrs \\ %{}) do
Repo.get_by!(NoteNote, attrs)
end
@ -314,13 +327,6 @@ defmodule Chiya.Notes do
Repo.delete(note_note)
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
Repo.get_by!(NoteTag, attrs)
end

View file

@ -3,6 +3,11 @@ defmodule Chiya.Notes.NoteImage do
use Waffle.Ecto.Schema
import Ecto.Changeset
@attachment_options [
allow_paths: true,
allow_urls: true
]
schema "note_images" do
field :content, :string, default: ""
field :path, ChiyaWeb.Uploaders.NoteImage.Type
@ -24,7 +29,7 @@ defmodule Chiya.Notes.NoteImage do
def update_changeset(note_image, attrs) do
note_image
|> cast(attrs, [:content, :note_id, :featured])
|> cast_attachments(attrs, [:path], allow_paths: true)
|> cast_attachments(attrs, [:path], @attachment_options)
|> validate_required([:path, :note_id])
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>
<head>
<title>Not found</title>
<title>Not Found</title>
<link rel="preconnect" href="https://rsms.me/" />
<link rel="stylesheet" href="https://rsms.me/inter/inter.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
slug = Slugger.slugify_downcase(text)
content_tag(:ul, [class: "m-0"],
do: [
content_tag(:li, do:
content_tag(:a, text, href: "##{slug}")),
content_tag(:li, do: content_tag(:a, text, href: "##{slug}")),
Enum.map(children, &do_render_outline/1)
]
)

View file

@ -3,22 +3,23 @@
<%= @note.name %>
</.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">
<%= Markdown.render(@note.content) |> raw %>
</section>
<%= if not Enum.empty?(@note.links_to) do %>
<section class="mt-8 prose prose-gruvbox max-w-2xl mx-auto">
<.divider text="" />
Notes linking here:
<ul class="">
<%= for link <- @note.links_to do %>
<li><a href={~p"/note/#{link.slug}"}><%= link.name %></a></li>
<% end %>
</ul>
</section>
<section class="mt-8 prose prose-gruvbox max-w-2xl mx-auto">
<.divider text="" /> Notes linking here:
<ul class="">
<%= for link <- @note.links_to do %>
<li><a href={~p"/note/#{link.slug}"}><%= link.name %></a></li>
<% end %>
</ul>
</section>
<% end %>
<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} <- Chiya.Notes.create_note(note_attrs) do
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)}
else
error ->
@ -54,8 +66,15 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
end
@impl true
def handle_media(_files, _access_token) do
{:error, :insufficient_scope}
def handle_media(file, access_token) do
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
@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?(_props), do: true
def get_photo(%{"photo" => [photo]} = _props), do: photo
def get_photo(_props), do: nil
def get_photos(%{"photo" => photos} = _props), do: photos
def get_photos(_props), do: []
def get_syndication_targets(%{"mp-syndicate-to" => targets} = _props), do: targets
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
tag = List.first(note.tags)
assert tag.name == "Foo"
assert tag.name == "foo"
end
end
end

View file

@ -9,6 +9,6 @@ defmodule ChiyaWeb.ErrorHTMLTest do
end
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