devel #123
5 changed files with 103 additions and 32 deletions
|
@ -3,7 +3,7 @@ defmodule Chiya.Channels.Channel do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
alias Chiya.Channels.ChannelSlug
|
alias Chiya.Channels.ChannelSlug
|
||||||
|
|
||||||
@derive {Jason.Encoder, only: [:id, :name, :content, :slug, :visibility]}
|
@derive {Jason.Encoder, only: [:name]}
|
||||||
schema "channels" do
|
schema "channels" do
|
||||||
field :content, :string
|
field :content, :string
|
||||||
field :name, :string
|
field :name, :string
|
||||||
|
|
|
@ -104,7 +104,11 @@ defmodule Chiya.Notes do
|
||||||
** (Ecto.NoResultsError)
|
** (Ecto.NoResultsError)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def get_note_preloaded!(id), do: Repo.get!(Note, id) |> preload_note()
|
def get_note_preloaded!(id),
|
||||||
|
do:
|
||||||
|
Note
|
||||||
|
|> Repo.get!(id)
|
||||||
|
|> preload_note()
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets a single note by its slug and preloads it.
|
Gets a single note by its slug and preloads it.
|
||||||
|
@ -120,7 +124,11 @@ defmodule Chiya.Notes do
|
||||||
** (Ecto.NoResultsError)
|
** (Ecto.NoResultsError)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def get_note_by_slug_preloaded!(slug), do: Repo.get_by!(Note, slug: slug) |> preload_note()
|
def get_note_by_slug_preloaded!(slug),
|
||||||
|
do:
|
||||||
|
Note
|
||||||
|
|> Repo.get_by!(slug: slug)
|
||||||
|
|> preload_note()
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets a single note by its slug and preloads it.
|
Gets a single note by its slug and preloads it.
|
||||||
|
@ -136,7 +144,25 @@ defmodule Chiya.Notes do
|
||||||
nil
|
nil
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def get_note_by_slug_preloaded(slug), do: Repo.get_by(Note, slug: slug) |> preload_note()
|
def get_note_by_slug_preloaded(slug),
|
||||||
|
do:
|
||||||
|
Note
|
||||||
|
|> Repo.get_by(slug: slug)
|
||||||
|
|> preload_note()
|
||||||
|
|
||||||
|
def get_public_note_by_slug_preloaded(slug),
|
||||||
|
do:
|
||||||
|
Note
|
||||||
|
|> where([n], not is_nil(n.published_at))
|
||||||
|
|> Repo.get_by(slug: slug)
|
||||||
|
|> preload_note()
|
||||||
|
|
||||||
|
def get_public_note_by_slug_preloaded!(slug),
|
||||||
|
do:
|
||||||
|
Note
|
||||||
|
|> where([n], not is_nil(n.published_at))
|
||||||
|
|> Repo.get_by!(slug: slug)
|
||||||
|
|> preload_note()
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Creates a note.
|
Creates a note.
|
||||||
|
|
|
@ -9,8 +9,9 @@ defmodule Chiya.Notes.Note do
|
||||||
statics: ChiyaWeb.static_paths()
|
statics: ChiyaWeb.static_paths()
|
||||||
|
|
||||||
@reserved_slugs ~w(user admin dev api)
|
@reserved_slugs ~w(user admin dev api)
|
||||||
|
@note_url_regex ~r/\/note\/([a-z0-9-]+)/
|
||||||
|
|
||||||
@derive {Jason.Encoder, only: [:id, :name, :content, :slug, :channels]}
|
@derive {Jason.Encoder, only: [:id, :name, :content, :slug, :channels, :tags]}
|
||||||
schema "notes" do
|
schema "notes" do
|
||||||
field :content, :string
|
field :content, :string
|
||||||
|
|
||||||
|
@ -50,9 +51,19 @@ defmodule Chiya.Notes.Note do
|
||||||
timestamps()
|
timestamps()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def note_path(note) do
|
||||||
|
~p"/note/#{note.slug}"
|
||||||
|
end
|
||||||
|
|
||||||
def note_url(note) do
|
def note_url(note) do
|
||||||
URI.merge(ChiyaWeb.Endpoint.url(), ~p"/note/#{note.slug}")
|
Phoenix.VerifiedRoutes.url(~p"/note/#{note.slug}")
|
||||||
|> to_string()
|
end
|
||||||
|
|
||||||
|
def note_slug(note_url) do
|
||||||
|
case Regex.run(@note_url_regex, note_url) do
|
||||||
|
nil -> {:error, nil}
|
||||||
|
[_full, slug] -> {:ok, slug}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc false
|
@doc false
|
||||||
|
|
|
@ -6,6 +6,7 @@ defmodule Chiya.Tags.Tag do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
alias Chiya.Tags.TagSlug
|
alias Chiya.Tags.TagSlug
|
||||||
|
|
||||||
|
@derive {Jason.Encoder, only: [:name]}
|
||||||
schema "tags" do
|
schema "tags" do
|
||||||
field :name, :string
|
field :name, :string
|
||||||
field :slug, TagSlug.Type
|
field :slug, TagSlug.Type
|
||||||
|
|
|
@ -5,6 +5,15 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
|
||||||
alias ChiyaWeb.Indie.Properties, as: Props
|
alias ChiyaWeb.Indie.Properties, as: Props
|
||||||
alias ChiyaWeb.Indie.Token
|
alias ChiyaWeb.Indie.Token
|
||||||
|
|
||||||
|
@default_properties [
|
||||||
|
"name",
|
||||||
|
"content",
|
||||||
|
"published_at",
|
||||||
|
"slug",
|
||||||
|
"channels",
|
||||||
|
"tags"
|
||||||
|
]
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_create(type, properties, access_token) do
|
def handle_create(type, properties, access_token) do
|
||||||
Logger.info("Handle create")
|
Logger.info("Handle create")
|
||||||
|
@ -41,37 +50,59 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
|
||||||
{:error, :insufficient_scope}
|
{:error, :insufficient_scope}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
|
||||||
def handle_source_query(_url, _filter_properties, _access_token) do
|
|
||||||
{:error, :insufficient_scope}
|
|
||||||
end
|
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_media(_files, _access_token) do
|
def handle_media(_files, _access_token) do
|
||||||
{:error, :insufficient_scope}
|
{:error, :insufficient_scope}
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
def handle_config_query(_access_token) do
|
def handle_source_query(url, filter_properties, access_token) do
|
||||||
channels = Chiya.Channels.list_channels()
|
filter_properties =
|
||||||
|
if Enum.empty?(filter_properties),
|
||||||
|
do: @default_properties,
|
||||||
|
else: filter_properties
|
||||||
|
|
||||||
{:ok,
|
with :ok <- verify_token(access_token),
|
||||||
%{
|
{:ok, slug} <- Chiya.Notes.Note.note_slug(url),
|
||||||
"destination" => [],
|
note <- Chiya.Notes.get_public_note_by_slug_preloaded!(slug) do
|
||||||
"post-types" => [
|
filtered_note =
|
||||||
|
Map.filter(note, fn {key, _val} ->
|
||||||
|
Enum.member?(filter_properties, to_string(key))
|
||||||
|
end)
|
||||||
|
|
||||||
|
{:ok, filtered_note}
|
||||||
|
else
|
||||||
|
_ -> {:error, :insufficient_scope}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
@impl true
|
||||||
|
def handle_config_query(access_token) do
|
||||||
|
case verify_token(access_token) do
|
||||||
|
:ok ->
|
||||||
|
channels = Chiya.Channels.list_channels()
|
||||||
|
|
||||||
|
{:ok,
|
||||||
%{
|
%{
|
||||||
"type" => "note",
|
"destination" => [],
|
||||||
"name" => "Note"
|
"post-types" => [
|
||||||
}
|
%{
|
||||||
],
|
"type" => "note",
|
||||||
"channels" =>
|
"name" => "Note"
|
||||||
Enum.map(channels, fn c ->
|
}
|
||||||
%{
|
],
|
||||||
"uid" => c.slug,
|
"channels" =>
|
||||||
"name" => c.name
|
Enum.map(channels, fn c ->
|
||||||
}
|
%{
|
||||||
end)
|
"uid" => c.slug,
|
||||||
}}
|
"name" => c.name
|
||||||
|
}
|
||||||
|
end)
|
||||||
|
}}
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
{:error, :insufficient_scope}
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@impl true
|
@impl true
|
||||||
|
@ -88,7 +119,9 @@ defmodule ChiyaWeb.Indie.MicropubHandler do
|
||||||
:ok ->
|
:ok ->
|
||||||
tags = Enum.map(Chiya.Tags.list_tags(), fn t -> t.name end)
|
tags = Enum.map(Chiya.Tags.list_tags(), fn t -> t.name end)
|
||||||
{:ok, %{"categories" => tags}}
|
{:ok, %{"categories" => tags}}
|
||||||
_ -> {:error, :insufficient_scope}
|
|
||||||
|
_ ->
|
||||||
|
{:error, :insufficient_scope}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue