From 95ace30af5c4d0e84cf3fc41b94e46791ee536ff Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 11 Nov 2019 21:38:51 -0500 Subject: [PATCH] add comments behavior --- assets/js/comment.js | 3 +- lib/philomena/users/ability.ex | 11 ++++++- .../controllers/image/comment_controller.ex | 33 +++++++++++++++++++ .../controllers/image_controller.ex | 6 ++-- lib/philomena_web/router.ex | 4 ++- .../templates/image/comment/index.html.slime | 20 +++++++++++ .../templates/image/comment/show.html.slime | 1 + .../templates/image/show.html.slime | 5 ++- lib/philomena_web/views/image/comment_view.ex | 3 ++ 9 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 lib/philomena_web/controllers/image/comment_controller.ex create mode 100644 lib/philomena_web/templates/image/comment/index.html.slime create mode 100644 lib/philomena_web/templates/image/comment/show.html.slime create mode 100644 lib/philomena_web/views/image/comment_view.ex diff --git a/assets/js/comment.js b/assets/js/comment.js index 72c6fcf5..8ba6bf0d 100644 --- a/assets/js/comment.js +++ b/assets/js/comment.js @@ -58,8 +58,7 @@ function loadParentPost(event) { // If the regex matched, get the image and comment ID const [ , imageId, commentId ] = commentMatches; - // Use .html because the default response is JSON - fetchHtml(`/images/${imageId}/comments/${commentId}.html`) + fetchHtml(`/images/${imageId}/comments/${commentId}`) .then(handleError) .then(data => { clearParentPost(clickedLink, fullComment); diff --git a/lib/philomena/users/ability.ex b/lib/philomena/users/ability.ex index 76e5c17f..f16d604e 100644 --- a/lib/philomena/users/ability.ex +++ b/lib/philomena/users/ability.ex @@ -1,5 +1,6 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do alias Philomena.Users.User + alias Philomena.Comments.Comment alias Philomena.Images.Image alias Philomena.Forums.Forum alias Philomena.Topics.Topic @@ -18,6 +19,9 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do # View images def can?(%User{role: "moderator"}, :show, %Image{}), do: true + # View comments + def can?(%User{role: "moderator"}, :show, %Comment{}), do: true + # View forums def can?(%User{role: "moderator"}, :show, %Forum{access_level: level}) when level in ["normal", "assistant", "staff"], do: true @@ -48,7 +52,12 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do when action in [:new, :create, :index], do: true - def can?(_user, :show, %Image{hidden_from_users: false}), do: true + def can?(_user, action, %Image{hidden_from_users: false}) + when action in [:show, :index], + do: true + + # View non-deleted comments + def can?(_user, :show, %Comment{hidden_from_users: false}), do: true # View forums def can?(_user, :show, %Forum{access_level: "normal"}), do: true diff --git a/lib/philomena_web/controllers/image/comment_controller.ex b/lib/philomena_web/controllers/image/comment_controller.ex new file mode 100644 index 00000000..1ce3df02 --- /dev/null +++ b/lib/philomena_web/controllers/image/comment_controller.ex @@ -0,0 +1,33 @@ +defmodule PhilomenaWeb.Image.CommentController do + use PhilomenaWeb, :controller + + alias Philomena.{Images.Image, Comments.Comment, Textile.Renderer} + alias Philomena.Repo + import Ecto.Query + + plug :load_and_authorize_resource, model: Image, id_name: "image_id", persisted: true + plug :load_and_authorize_resource, model: Comment, only: [:show], preload: [:image, :user] + + def index(conn, _params) do + comments = + Comment + |> where(image_id: ^conn.assigns.image.id) + |> order_by(desc: :created_at) + |> preload([:image, :user]) + |> Repo.paginate(conn.assigns.scrivener) + + rendered = + comments.entries + |> Renderer.render_collection() + + comments = + %{comments | entries: Enum.zip(comments.entries, rendered)} + + render(conn, "index.html", layout: false, image: conn.assigns.image, comments: comments) + end + + def show(conn, _params) do + rendered = Renderer.render_one(conn.assigns.comment) + render(conn, "show.html", layout: false, image: conn.assigns.image, comment: conn.assigns.comment, body: rendered) + end +end diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index f80ca5ac..e85a91d6 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -30,14 +30,14 @@ defmodule PhilomenaWeb.ImageController do |> preload([:user, :image]) |> order_by(desc: :created_at) |> limit(25) - |> Repo.all() + |> Repo.paginate(conn.assigns.scrivener) rendered = - comments + comments.entries |> Renderer.render_collection() comments = - Enum.zip(comments, rendered) + %{comments | entries: Enum.zip(comments.entries, rendered)} description = %{body: conn.assigns.image.description} diff --git a/lib/philomena_web/router.ex b/lib/philomena_web/router.ex index 1eca70ef..96fe1d74 100644 --- a/lib/philomena_web/router.ex +++ b/lib/philomena_web/router.ex @@ -28,7 +28,9 @@ defmodule PhilomenaWeb.Router do get "/", ActivityController, :index resources "/activity", ActivityController, only: [:index] - resources "/images", ImageController, only: [:index, :show] + resources "/images", ImageController, only: [:index, :show] do + resources "/comments", Image.CommentController, only: [:index, :show] + end resources "/tags", TagController, only: [:index, :show] resources "/search", SearchController, only: [:index] resources "/forums", ForumController, only: [:index, :show] do diff --git a/lib/philomena_web/templates/image/comment/index.html.slime b/lib/philomena_web/templates/image/comment/index.html.slime new file mode 100644 index 00000000..be525a95 --- /dev/null +++ b/lib/philomena_web/templates/image/comment/index.html.slime @@ -0,0 +1,20 @@ +elixir: + route = fn p -> Routes.image_comment_path(@conn, :index, @image, p) end + pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @comments, route: route + +.block + .block__header + =<> pagination + span.block__header__title + =<> @image.comments_count + => pluralize("comment", "comments", @image.comments_count) + ' posted + button.button#js-refresh-comments title="Refresh" data-disable-with="..." + span.hide-mobile<> Refresh + += for {comment, body} <- @comments do + = render PhilomenaWeb.CommentView, "_comment.html", comment: comment, body: body, conn: @conn + +.block + .block__header.block__header--light + = pagination \ No newline at end of file diff --git a/lib/philomena_web/templates/image/comment/show.html.slime b/lib/philomena_web/templates/image/comment/show.html.slime new file mode 100644 index 00000000..9497f8db --- /dev/null +++ b/lib/philomena_web/templates/image/comment/show.html.slime @@ -0,0 +1 @@ += render PhilomenaWeb.CommentView, "_comment.html", comment: @comment, body: @body, conn: @conn \ No newline at end of file diff --git a/lib/philomena_web/templates/image/show.html.slime b/lib/philomena_web/templates/image/show.html.slime index cb5d95ca..6a0e1850 100644 --- a/lib/philomena_web/templates/image/show.html.slime +++ b/lib/philomena_web/templates/image/show.html.slime @@ -19,6 +19,5 @@ em> no source provided yet h4 Comments - #comments data-current-url="" data-loaded="true" - = for {comment, body} <- @comments do - = render PhilomenaWeb.CommentView, "_comment.html", comment: comment, body: body, conn: @conn + #comments data-current-url=Routes.image_comment_path(@conn, :index, @image, page: 1) data-loaded="true" + = render PhilomenaWeb.Image.CommentView, "index.html", image: @image, comments: @comments, conn: @conn \ No newline at end of file diff --git a/lib/philomena_web/views/image/comment_view.ex b/lib/philomena_web/views/image/comment_view.ex new file mode 100644 index 00000000..78c838ca --- /dev/null +++ b/lib/philomena_web/views/image/comment_view.ex @@ -0,0 +1,3 @@ +defmodule PhilomenaWeb.Image.CommentView do + use PhilomenaWeb, :view +end