From d9fd1bb08dc5a8a30642e069cbb12ec51edcffda Mon Sep 17 00:00:00 2001 From: Inhji Date: Mon, 17 Jul 2023 19:00:41 +0200 Subject: [PATCH] prepare mediaendpoint 2 --- lib/chiya/notes/note_image.ex | 7 ++++++- lib/chiya_web/indie/micropub_handler.ex | 19 +++++++++++++++---- lib/chiya_web/indie/properties.ex | 4 ++-- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/chiya/notes/note_image.ex b/lib/chiya/notes/note_image.ex index 0c30629..0f1a9ac 100644 --- a/lib/chiya/notes/note_image.ex +++ b/lib/chiya/notes/note_image.ex @@ -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 diff --git a/lib/chiya_web/indie/micropub_handler.ex b/lib/chiya_web/indie/micropub_handler.ex index 7e30f78..31cdc45 100644 --- a/lib/chiya_web/indie/micropub_handler.ex +++ b/lib/chiya_web/indie/micropub_handler.ex @@ -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 -> @@ -60,8 +72,8 @@ defmodule ChiyaWeb.Indie.MicropubHandler do url = ChiyaWeb.Uploaders.UserImageTemp.url({image.path, image}, :original) {:ok, url} else - _ -> - {:error, :insufficient_scope} + _ -> + {:error, :insufficient_scope} end end @@ -182,8 +194,7 @@ defmodule ChiyaWeb.Indie.MicropubHandler do content = Props.get_content(p) name = Props.get_title(p) || Chiya.Notes.Note.note_title(content) tags = Props.get_tags(p) |> Enum.join(",") - photo = Props.get_photo(p) - + published_at = if Props.is_published?(p), do: NaiveDateTime.local_now(), diff --git a/lib/chiya_web/indie/properties.ex b/lib/chiya_web/indie/properties.ex index 47ba5eb..a93df30 100644 --- a/lib/chiya_web/indie/properties.ex +++ b/lib/chiya_web/indie/properties.ex @@ -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: []