add comments

This commit is contained in:
Inhji 2023-04-30 13:35:22 +02:00
parent a5dd041c42
commit 43cad45705
9 changed files with 138 additions and 10 deletions

View file

@ -5,9 +5,22 @@ defmodule Chiya.Notes do
import Ecto.Query, warn: false
alias Chiya.Repo
alias Chiya.Notes.{Note, NoteImage, NoteNote, NoteTag}
alias Chiya.Notes.{Note, NoteImage, NoteNote, NoteTag, NoteComment}
@preloads [:channels, :images, :links_from, :links_to, :tags]
@preloads [
:channels,
:images,
:links_from,
:links_to,
:tags,
comments:
from(c in NoteComment,
order_by: [
desc: :approved_at,
desc: :inserted_at
]
)
]
def note_preloads(), do: @preloads
@doc """
@ -264,4 +277,18 @@ defmodule Chiya.Notes do
def delete_note_tag(%NoteTag{} = note_tag) do
Repo.delete(note_tag)
end
def create_note_comment(attrs \\ %{}) do
%NoteComment{}
|> NoteComment.changeset(attrs)
|> Repo.insert()
end
def delete_note_comment(%NoteComment{} = note_comment) do
Repo.delete(note_comment)
end
def change_note_comment(%NoteComment{} = note_comment, attrs \\ %{}) do
NoteComment.changeset(note_comment, attrs)
end
end

View file

@ -1,9 +1,9 @@
defmodule Chiya.Notes.Note do
use Ecto.Schema
import Ecto.Changeset
alias Chiya.Notes.NoteSlug
alias Chiya.Notes.{Note, NoteSlug, NoteNote, NoteTag}
@reserved_slugs []
@reserved_slugs ~w(user admin dev api)
@derive {Jason.Encoder, only: [:id, :name, :content, :slug, :channels]}
schema "notes" do
@ -23,19 +23,20 @@ defmodule Chiya.Notes.Note do
join_keys: [note: :id, channel: :id],
on_replace: :delete
many_to_many :links_from, Chiya.Notes.Note,
join_through: Chiya.Notes.NoteNote,
many_to_many :links_from, Note,
join_through: NoteNote,
join_keys: [source_id: :id, target_id: :id]
many_to_many :links_to, Chiya.Notes.Note,
join_through: Chiya.Notes.NoteNote,
many_to_many :links_to, Note,
join_through: NoteNote,
join_keys: [target_id: :id, source_id: :id]
many_to_many :tags, Chiya.Tags.Tag,
join_through: Chiya.Notes.NoteTag,
join_through: NoteTag,
join_keys: [note_id: :id, tag_id: :id]
has_many :images, Chiya.Notes.NoteImage
has_many :comments, Chiya.Notes.NoteComment
field :tags_string, :string,
virtual: true,

View file

@ -0,0 +1,23 @@
defmodule Chiya.Notes.NoteComment do
use Ecto.Schema
import Ecto.Changeset
schema "note_comments" do
field :approved_at, :naive_datetime
field :author_name, :string
field :author_id, :id
field :content, :string
field :kind, Ecto.Enum, values: [:anon, :fedi], default: :anon
belongs_to :note, Chiya.Notes.Note
timestamps()
end
@doc false
def changeset(note_comment, attrs) do
note_comment
|> cast(attrs, [:content, :author_name, :author_id, :kind, :note_id, :approved_at])
|> validate_required([:content, :author_name, :kind, :note_id])
end
end

View file

@ -0,0 +1,17 @@
defmodule ChiyaWeb.CommentController do
use ChiyaWeb, :controller
def create(conn, %{"slug" => note_slug, "note_comment" => comment_params}) do
note = Chiya.Notes.get_note_by_slug_preloaded!(note_slug)
IO.inspect(comment_params)
case Chiya.Notes.create_note_comment(comment_params) do
{:ok, _comment} ->
redirect(conn, to: ~p"/#{note_slug}?error=0")
{:error, changeset} ->
redirect(conn, to: ~p"/#{note_slug}?error=1")
end
end
end

View file

@ -0,0 +1,5 @@
defmodule ChiyaWeb.CommentHTML do
use ChiyaWeb, :html
embed_templates "comment_html/*"
end

View file

@ -41,6 +41,7 @@ defmodule ChiyaWeb.PageController do
def note(conn, %{"slug" => note_slug}) do
note = Chiya.Notes.get_note_by_slug_preloaded!(note_slug)
changeset = Chiya.Notes.change_note_comment(%Chiya.Notes.NoteComment{}, %{note_id: note.id})
if is_nil(note.published_at) and is_nil(conn.assigns.current_user) do
render_error(conn, :not_found)
@ -48,7 +49,8 @@ defmodule ChiyaWeb.PageController do
render(conn, :note,
layout: {ChiyaWeb.Layouts, "public.html"},
note: note,
page_title: note.name
page_title: note.name,
changeset: changeset
)
end
end

View file

@ -50,4 +50,37 @@
<% end %>
</div>
<% end %>
<%= if not Enum.empty?(@note.comments) do %>
<.line />
<h2 class="mb-6 text-theme-base"><%= Enum.count(@note.comments) %> Comments</h2>
<aside id="comments" class="flex flex-col gap-6">
<%= for comment <- @note.comments do %>
<article class="text-theme-base bg-theme-base/10 p-1">
<header class="flex flex-row justify-between">
<strong class="text-theme-primary"><%= comment.author_name %></strong>
<span class="text-theme-dim"><%= from_now(comment.inserted_at) %></span>
</header>
<p><%= comment.content %></p>
</article>
<% end %>
</aside>
<% end %>
<.line />
<.simple_form :let={f} for={@changeset} action={~p"/#{@note.slug}/comment"}>
<.error :if={@changeset.action}>
Oops, something went wrong! Please check the errors below.
</.error>
<.input field={f[:author_name]} type="text" placeholder="Name" />
<.input field={f[:content]} type="textarea" placeholder="Content" rows="3" />
<.input field={f[:note_id]} type="hidden" />
<:actions>
<.button>Submit Comment</.button>
</:actions>
</.simple_form>
</div>

View file

@ -119,5 +119,7 @@ defmodule ChiyaWeb.Router do
get "/c/:slug", PageController, :channel
get "/t/:slug", PageController, :tag
get "/", PageController, :home
post "/:slug/comment", CommentController, :create
end
end

View file

@ -0,0 +1,18 @@
defmodule Chiya.Repo.Migrations.CreateNoteComments do
use Ecto.Migration
def change do
create table(:note_comments) do
add :content, :text
add :author_name, :string
add :author_id, :integer
add :kind, :string
add :approved_at, :naive_datetime
add :note_id, references(:notes, on_delete: :delete_all)
timestamps()
end
create index(:note_comments, [:note_id])
end
end