add back sort option for oldest comments first

This commit is contained in:
byte[] 2019-12-19 15:11:44 -05:00
parent 96892f7dbd
commit 091fafd5e2
7 changed files with 64 additions and 41 deletions

View file

@ -201,7 +201,7 @@ defmodule Philomena.Users.User do
:fancy_tag_field_on_edit, :anonymous_by_default, :scale_large_images, :fancy_tag_field_on_edit, :anonymous_by_default, :scale_large_images,
:comments_per_page, :theme, :watched_images_query_str, :comments_per_page, :theme, :watched_images_query_str,
:no_spoilered_in_watched, :watched_images_exclude_str, :no_spoilered_in_watched, :watched_images_exclude_str,
:use_centered_layout, :hide_vote_counts :use_centered_layout, :hide_vote_counts, :comments_newest_first
]) ])
|> validate_required([ |> validate_required([
:images_per_page, :fancy_tag_field_on_upload, :fancy_tag_field_on_edit, :images_per_page, :fancy_tag_field_on_upload, :fancy_tag_field_on_edit,

View file

@ -0,0 +1,46 @@
defmodule PhilomenaWeb.CommentLoader do
alias Philomena.Comments.Comment
alias Philomena.Repo
import Ecto.Query
def load_comments(conn, image) do
pref = load_direction(conn.assigns.current_user)
Comment
|> where(image_id: ^image.id)
|> order_by([{^pref, :created_at}])
|> preload([:image, :deleted_by, user: [awards: :badge]])
|> Repo.paginate(conn.assigns.comment_scrivener)
end
def find_page(conn, image, comment_id) do
user = conn.assigns.current_user
comment =
Comment
|> where(image_id: ^image.id)
|> where(id: ^comment_id)
|> Repo.one!()
offset =
Comment
|> where(image_id: ^image.id)
|> filter_direction(comment.created_at, user)
|> Repo.aggregate(:count, :id)
|> IO.inspect()
page_size = conn.assigns.comment_scrivener[:page_size]
# Pagination starts at page 1
div(offset, page_size) + 1
end
defp load_direction(%{comments_newest_first: false}), do: :asc
defp load_direction(_user), do: :desc
defp filter_direction(query, time, %{comments_newest_first: false}),
do: where(query, [c], c.created_at <= ^time)
defp filter_direction(query, time, _user),
do: where(query, [c], c.created_at >= ^time)
end

View file

@ -1,6 +1,7 @@
defmodule PhilomenaWeb.Image.CommentController do defmodule PhilomenaWeb.Image.CommentController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.CommentLoader
alias Philomena.{Images.Image, Comments.Comment, Textile.Renderer} alias Philomena.{Images.Image, Comments.Comment, Textile.Renderer}
alias Philomena.UserStatistics alias Philomena.UserStatistics
alias Philomena.Comments alias Philomena.Comments
@ -20,36 +21,15 @@ defmodule PhilomenaWeb.Image.CommentController do
plug :authorize_resource, model: Comment, only: [:show, :edit, :update], preload: [:image, user: [awards: :badge]] plug :authorize_resource, model: Comment, only: [:show, :edit, :update], preload: [:image, user: [awards: :badge]]
def index(conn, %{"comment_id" => comment_id}) do def index(conn, %{"comment_id" => comment_id}) do
comment = page = CommentLoader.find_page(conn, conn.assigns.image, comment_id)
Comment
|> where(image_id: ^conn.assigns.image.id)
|> where(id: ^comment_id)
|> Repo.one!()
offset = redirect(conn, to: Routes.image_comment_path(conn, :index, conn.assigns.image, page: page))
Comment
|> where(image_id: ^conn.assigns.image.id)
|> where([c], c.created_at > ^comment.created_at)
|> Repo.aggregate(:count, :id)
%{page_size: page_size} = conn.assigns.pagination
page = div(offset, page_size)
conn
|> redirect(to: Routes.image_comment_path(conn, :index, conn.assigns.image, page: page))
end end
def index(conn, _params) do def index(conn, _params) do
comments = comments = CommentLoader.load_comments(conn, conn.assigns.image)
Comment
|> where(image_id: ^conn.assigns.image.id)
|> order_by(desc: :created_at)
|> preload([:image, user: [awards: :badge]])
|> Repo.paginate(conn.assigns.comment_scrivener)
rendered = rendered = Renderer.render_collection(comments.entries, conn)
comments.entries
|> Renderer.render_collection(conn)
comments = comments =
%{comments | entries: Enum.zip(comments.entries, rendered)} %{comments | entries: Enum.zip(comments.entries, rendered)}

View file

@ -2,6 +2,7 @@ defmodule PhilomenaWeb.ImageController do
use PhilomenaWeb, :controller use PhilomenaWeb, :controller
alias PhilomenaWeb.ImageLoader alias PhilomenaWeb.ImageLoader
alias PhilomenaWeb.CommentLoader
alias PhilomenaWeb.NotificationCountPlug alias PhilomenaWeb.NotificationCountPlug
alias Philomena.{Images, Images.Image, Comments.Comment, Galleries.Gallery, Galleries.Interaction, Textile.Renderer} alias Philomena.{Images, Images.Image, Comments.Comment, Galleries.Gallery, Galleries.Interaction, Textile.Renderer}
alias Philomena.Servers.ImageProcessor alias Philomena.Servers.ImageProcessor
@ -38,17 +39,9 @@ defmodule PhilomenaWeb.ImageController do
# Update the notification ticker in the header # Update the notification ticker in the header
conn = NotificationCountPlug.call(conn) conn = NotificationCountPlug.call(conn)
comments = comments = CommentLoader.load_comments(conn, image)
Comment
|> where(image_id: ^image.id)
|> preload([:image, :deleted_by, user: [awards: :badge]])
|> order_by(desc: :created_at)
|> limit(25)
|> Repo.paginate(conn.assigns.comment_scrivener)
rendered = rendered = Renderer.render_collection(comments.entries, conn)
comments.entries
|> Renderer.render_collection(conn)
comments = comments =
%{comments | entries: Enum.zip(comments.entries, rendered)} %{comments | entries: Enum.zip(comments.entries, rendered)}

View file

@ -33,14 +33,14 @@ defmodule PhilomenaWeb.ProfileController do
ImageLoader.search_string( ImageLoader.search_string(
conn, conn,
"uploader_id:#{user.id}", "uploader_id:#{user.id}",
pagination: %{page_number: 1, page_size: 6} pagination: %{page_number: 1, page_size: 8}
) )
{:ok, {recent_faves, _tags}} = {:ok, {recent_faves, _tags}} =
ImageLoader.search_string( ImageLoader.search_string(
conn, conn,
"faved_by_id:#{user.id}", "faved_by_id:#{user.id}",
pagination: %{page_number: 1, page_size: 6} pagination: %{page_number: 1, page_size: 8}
) )
tags = tags(conn.assigns.user.public_links) tags = tags(conn.assigns.user.public_links)
@ -195,7 +195,7 @@ defmodule PhilomenaWeb.ProfileController do
ImageLoader.query( ImageLoader.query(
conn, conn,
%{terms: %{tag_ids: Enum.map(tags, & &1.id)}}, %{terms: %{tag_ids: Enum.map(tags, & &1.id)}},
pagination: %{page_number: 1, page_size: 6} pagination: %{page_number: 1, page_size: 8}
) )
images images

View file

@ -23,5 +23,5 @@
- true -> - true ->
#comments data-current-url=Routes.image_comment_path(@conn, :index, @image, page: 1) data-loaded="true" #comments data-current-url=Routes.image_comment_path(@conn, :index, @image) data-loaded="true"
= render PhilomenaWeb.Image.CommentView, "index.html", image: @image, comments: @comments, conn: @conn = render PhilomenaWeb.Image.CommentView, "index.html", image: @image, comments: @comments, conn: @conn

View file

@ -59,6 +59,10 @@ h1 Content Settings
=> label f, :hide_vote_counts => label f, :hide_vote_counts
=> checkbox f, :hide_vote_counts, class: "checkbox" => checkbox f, :hide_vote_counts, class: "checkbox"
.fieldlabel: i Hide upvote and downvote counts on images, showing only the overall score .fieldlabel: i Hide upvote and downvote counts on images, showing only the overall score
.field
=> label f, :comments_newest_first, "Newest comments first"
=> checkbox f, :comments_newest_first
.fieldlabel: i Display the newest comments at the top of the page.
.field .field
=> label f, :images_per_page => label f, :images_per_page
=> number_input f, :images_per_page, min: 15, max: 50, step: 1, class: "input" => number_input f, :images_per_page, min: 15, max: 50, step: 1, class: "input"