2023-03-05 17:23:16 +01:00
|
|
|
defmodule ChiyaWeb.NoteController do
|
|
|
|
use ChiyaWeb, :controller
|
2023-04-08 15:27:59 +02:00
|
|
|
import Plug.Conn, only: [assign: 3]
|
2023-03-05 17:23:16 +01:00
|
|
|
|
|
|
|
alias Chiya.Notes
|
2023-04-07 11:37:55 +02:00
|
|
|
alias Chiya.Notes.{Note, NoteImport}
|
2023-03-05 17:23:16 +01:00
|
|
|
|
2023-04-08 11:40:41 +02:00
|
|
|
def index(conn, %{"channel" => channel_slug}) do
|
|
|
|
channel = Chiya.Channels.get_channel_by_slug!(channel_slug)
|
|
|
|
notes = Notes.list_notes_by_channel(channel)
|
2023-04-08 15:27:59 +02:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> with_channels()
|
|
|
|
|> render(:index, notes: notes)
|
2023-04-08 11:40:41 +02:00
|
|
|
end
|
|
|
|
|
2023-03-05 17:23:16 +01:00
|
|
|
def index(conn, _params) do
|
|
|
|
notes = Notes.list_notes()
|
2023-04-08 15:27:59 +02:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> with_channels()
|
|
|
|
|> render(:index, notes: notes)
|
2023-03-05 17:23:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def new(conn, _params) do
|
2023-06-23 06:38:31 +02:00
|
|
|
default_channels = get_default_channels(conn)
|
2023-06-20 20:28:45 +02:00
|
|
|
|
|
|
|
changeset =
|
2023-06-23 06:38:31 +02:00
|
|
|
%Note{}
|
|
|
|
|> Notes.preload_note()
|
2023-06-20 20:28:45 +02:00
|
|
|
|> Notes.change_note()
|
2023-04-10 19:18:27 +02:00
|
|
|
|
|
|
|
render(conn, :new,
|
|
|
|
changeset: changeset,
|
|
|
|
channels: to_channel_options(),
|
2023-06-23 06:38:31 +02:00
|
|
|
selected_channels: default_channels,
|
2023-04-10 19:18:27 +02:00
|
|
|
tags: []
|
|
|
|
)
|
2023-03-05 17:23:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def create(conn, %{"note" => note_params}) do
|
2023-03-05 23:25:49 +01:00
|
|
|
note_params = from_channel_ids(note_params)
|
|
|
|
|
2023-03-05 17:23:16 +01:00
|
|
|
case Notes.create_note(note_params) do
|
|
|
|
{:ok, note} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Note created successfully.")
|
2023-03-06 19:57:19 +01:00
|
|
|
|> redirect(to: ~p"/admin/notes/#{note}")
|
2023-03-05 17:23:16 +01:00
|
|
|
|
2023-06-23 06:38:31 +02:00
|
|
|
# TODO: set channels from changeset when error happened?
|
|
|
|
|
2023-03-05 17:23:16 +01:00
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
2023-06-23 06:38:31 +02:00
|
|
|
render(conn, :new,
|
|
|
|
changeset: changeset,
|
|
|
|
channels: to_channel_options(),
|
|
|
|
selected_channels: nil,
|
|
|
|
tags: []
|
|
|
|
)
|
2023-03-05 17:23:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show(conn, %{"id" => id}) do
|
2023-03-09 07:28:06 +01:00
|
|
|
# note = Notes.get_note!(id)
|
|
|
|
# render(conn, :show, note: note)
|
2023-03-09 21:43:30 +01:00
|
|
|
live_render(conn, NoteShowLive, session: %{"note_id" => id})
|
2023-03-05 17:23:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit(conn, %{"id" => id}) do
|
2023-03-05 23:25:49 +01:00
|
|
|
note = Notes.get_note_preloaded!(id)
|
2023-03-05 17:23:16 +01:00
|
|
|
changeset = Notes.change_note(note)
|
2023-06-23 06:38:31 +02:00
|
|
|
selected_channels = Enum.map(note.channels, fn c -> c.id end)
|
2023-04-10 19:18:27 +02:00
|
|
|
|
|
|
|
render(conn, :edit,
|
|
|
|
note: note,
|
|
|
|
changeset: changeset,
|
|
|
|
channels: to_channel_options(),
|
2023-06-23 06:38:31 +02:00
|
|
|
selected_channels: selected_channels,
|
2023-04-10 19:18:27 +02:00
|
|
|
tags: note.tags
|
|
|
|
)
|
2023-03-05 17:23:16 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def update(conn, %{"id" => id, "note" => note_params}) do
|
2023-03-05 23:25:49 +01:00
|
|
|
note_params = from_channel_ids(note_params)
|
|
|
|
note = Notes.get_note_preloaded!(id)
|
2023-03-05 17:23:16 +01:00
|
|
|
|
|
|
|
case Notes.update_note(note, note_params) do
|
|
|
|
{:ok, note} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Note updated successfully.")
|
2023-03-06 19:57:19 +01:00
|
|
|
|> redirect(to: ~p"/admin/notes/#{note}")
|
2023-03-05 17:23:16 +01:00
|
|
|
|
2023-06-23 06:38:31 +02:00
|
|
|
# TODO: set channels from changeset when error happened?
|
|
|
|
|
2023-03-05 17:23:16 +01:00
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
2023-04-10 19:22:28 +02:00
|
|
|
render(conn, :edit,
|
|
|
|
note: note,
|
|
|
|
changeset: changeset,
|
|
|
|
channels: to_channel_options(),
|
2023-06-23 06:38:31 +02:00
|
|
|
selected_channels: nil,
|
2023-04-10 19:22:28 +02:00
|
|
|
tags: note.tags
|
|
|
|
)
|
2023-03-05 17:23:16 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete(conn, %{"id" => id}) do
|
|
|
|
note = Notes.get_note!(id)
|
|
|
|
{:ok, _note} = Notes.delete_note(note)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Note deleted successfully.")
|
2023-03-06 19:57:19 +01:00
|
|
|
|> redirect(to: ~p"/admin/notes")
|
2023-03-05 17:23:16 +01:00
|
|
|
end
|
2023-03-05 23:25:49 +01:00
|
|
|
|
2023-03-31 22:20:32 +02:00
|
|
|
def raw(conn, %{"id" => id}) do
|
|
|
|
raw_note =
|
|
|
|
id
|
|
|
|
|> Notes.get_note!()
|
|
|
|
|> Notes.preload_note()
|
|
|
|
|> ChiyaWeb.Exim.export_note()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> text(raw_note)
|
|
|
|
end
|
|
|
|
|
2023-05-21 09:53:17 +02:00
|
|
|
def publish(conn, %{"id" => id}) do
|
|
|
|
note = Notes.get_note_preloaded!(id)
|
|
|
|
|
2023-06-19 22:07:37 +02:00
|
|
|
case Notes.publish_note(note, NaiveDateTime.local_now()) do
|
2023-05-21 09:53:17 +02:00
|
|
|
{:ok, note} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Note published successfully.")
|
|
|
|
|> redirect(to: ~p"/admin/notes/#{note}")
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
render(conn, :edit,
|
|
|
|
note: note,
|
|
|
|
changeset: changeset,
|
|
|
|
channels: to_channel_options(),
|
|
|
|
tags: note.tags
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def unpublish(conn, %{"id" => id}) do
|
|
|
|
note = Notes.get_note_preloaded!(id)
|
|
|
|
|
2023-06-19 22:07:37 +02:00
|
|
|
case Notes.publish_note(note, nil) do
|
2023-05-21 09:53:17 +02:00
|
|
|
{:ok, note} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Note un-published successfully.")
|
|
|
|
|> redirect(to: ~p"/admin/notes/#{note}")
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
render(conn, :edit,
|
|
|
|
note: note,
|
|
|
|
changeset: changeset,
|
|
|
|
channels: to_channel_options(),
|
|
|
|
tags: note.tags
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-06 15:03:04 +02:00
|
|
|
def edit_image(conn, %{"image_id" => id}) do
|
|
|
|
image = Notes.get_note_image!(id)
|
|
|
|
changeset = Notes.change_note_image(image)
|
|
|
|
render(conn, :edit_image, image: image, changeset: changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_image(conn, %{"image_id" => id, "note_image" => image_params}) do
|
|
|
|
image = Notes.get_note_image!(id)
|
|
|
|
|
|
|
|
case Notes.update_note_image(image, image_params) do
|
|
|
|
{:ok, image} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Image updated successfully.")
|
|
|
|
|> redirect(to: ~p"/admin/notes/#{image.note_id}")
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
render(conn, :edit_image, image: image, changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-07 11:37:55 +02:00
|
|
|
def import_prepare(conn, _params) do
|
|
|
|
render(conn, :import, changeset: NoteImport.change_note_import(%{}))
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_run(conn, %{
|
|
|
|
"note_import" => %{
|
|
|
|
"file" => %{
|
|
|
|
path: path,
|
|
|
|
content_type: "text/markdown",
|
|
|
|
filename: filename
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}) do
|
|
|
|
case File.read(path) do
|
|
|
|
{:ok, content} ->
|
|
|
|
note_params = %{
|
|
|
|
name: filename,
|
|
|
|
content: content
|
|
|
|
}
|
|
|
|
|
|
|
|
case Notes.create_note(note_params) do
|
|
|
|
{:ok, note} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:info, "Note created successfully.")
|
|
|
|
|> redirect(to: ~p"/admin/notes/#{note}")
|
|
|
|
|
|
|
|
{:error, %Ecto.Changeset{} = changeset} ->
|
|
|
|
render(conn, :new, changeset: changeset, channels: to_channel_options())
|
|
|
|
end
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
render(conn, :import, changeset: NoteImport.change_note_import(%{}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def import_run(conn, _params) do
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Error while importing.")
|
|
|
|
|> redirect(to: ~p"/admin/notes")
|
|
|
|
end
|
|
|
|
|
2023-04-08 15:27:59 +02:00
|
|
|
defp with_channels(conn) do
|
|
|
|
assign(
|
|
|
|
conn,
|
|
|
|
:channels,
|
|
|
|
Chiya.Channels.list_channels()
|
|
|
|
|> Chiya.Channels.preload_channel()
|
|
|
|
|> Enum.filter(fn c -> not Enum.empty?(c.notes) end)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-06-23 06:38:31 +02:00
|
|
|
defp get_default_channels(conn = %Plug.Conn{}) do
|
|
|
|
if conn.assigns.settings.default_channel do
|
|
|
|
[conn.assigns.settings.default_channel.id]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-05 23:25:49 +01:00
|
|
|
defp from_channel_ids(note_params) do
|
|
|
|
selected_ids = Enum.map(note_params["channels"] || [], &String.to_integer/1)
|
|
|
|
|
|
|
|
selected_channels =
|
|
|
|
Chiya.Channels.list_channels()
|
|
|
|
|> Enum.filter(fn c -> Enum.member?(selected_ids, c.id) end)
|
|
|
|
|
|
|
|
Map.put(note_params, "channels", selected_channels)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp to_channel_options(items \\ nil),
|
2023-04-08 09:53:27 +02:00
|
|
|
do:
|
|
|
|
Enum.map(items || Chiya.Channels.list_channels(), fn c ->
|
|
|
|
{Chiya.Channels.Channel.icon(c) <> " " <> c.name, c.id}
|
|
|
|
end)
|
2023-03-05 17:23:16 +01:00
|
|
|
end
|