From c3f745ddeef4e834f8cbc3d27d7a50f1c9a4b5f3 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sat, 7 Dec 2019 18:41:31 -0500 Subject: [PATCH] display tag/source change counters on image pages --- .../controllers/image/source_controller.ex | 12 ++++++++++-- .../controllers/image/tag_controller.ex | 11 +++++++++-- lib/philomena_web/controllers/image_controller.ex | 10 ++++++++-- lib/philomena_web/templates/image/_source.html.slime | 9 ++++++--- lib/philomena_web/templates/image/_tags.html.slime | 9 ++++++--- lib/philomena_web/templates/image/show.html.slime | 4 ++-- 6 files changed, 41 insertions(+), 14 deletions(-) diff --git a/lib/philomena_web/controllers/image/source_controller.ex b/lib/philomena_web/controllers/image/source_controller.ex index 97351656..f35bae9a 100644 --- a/lib/philomena_web/controllers/image/source_controller.ex +++ b/lib/philomena_web/controllers/image/source_controller.ex @@ -1,9 +1,12 @@ defmodule PhilomenaWeb.Image.SourceController do use PhilomenaWeb, :controller + alias Philomena.SourceChanges.SourceChange alias Philomena.UserStatistics alias Philomena.Images.Image alias Philomena.Images + alias Philomena.Repo + import Ecto.Query plug PhilomenaWeb.FilterBannedUsersPlug plug PhilomenaWeb.CaptchaPlug @@ -20,16 +23,21 @@ defmodule PhilomenaWeb.Image.SourceController do changeset = Images.change_image(image) + source_change_count = + SourceChange + |> where(image_id: ^image.id) + |> Repo.aggregate(:count, :id) + UserStatistics.inc_stat(conn.assigns.current_user, :metadata_updates) conn |> put_view(PhilomenaWeb.ImageView) - |> render("_source.html", layout: false, image: image, changeset: changeset) + |> render("_source.html", layout: false, source_change_count: source_change_count, image: image, changeset: changeset) {:error, :image, changeset, _} -> conn |> put_view(PhilomenaWeb.ImageView) - |> render("_source.html", layout: false, image: image, changeset: changeset) + |> render("_source.html", layout: false, source_change_count: 0, image: image, changeset: changeset) end end end \ No newline at end of file diff --git a/lib/philomena_web/controllers/image/tag_controller.ex b/lib/philomena_web/controllers/image/tag_controller.ex index 35406a59..fd2d6094 100644 --- a/lib/philomena_web/controllers/image/tag_controller.ex +++ b/lib/philomena_web/controllers/image/tag_controller.ex @@ -1,11 +1,13 @@ defmodule PhilomenaWeb.Image.TagController do use PhilomenaWeb, :controller + alias Philomena.TagChanges.TagChange alias Philomena.UserStatistics alias Philomena.Images.Image alias Philomena.Images alias Philomena.Tags alias Philomena.Repo + import Ecto.Query plug PhilomenaWeb.FilterBannedUsersPlug plug PhilomenaWeb.CaptchaPlug @@ -23,6 +25,11 @@ defmodule PhilomenaWeb.Image.TagController do Tags.reindex_tags(added_tags ++ removed_tags) UserStatistics.inc_stat(conn.assigns.current_user, :metadata_updates) + tag_change_count = + TagChange + |> where(image_id: ^image.id) + |> Repo.aggregate(:count, :id) + image = image |> Repo.preload(:tags, force: true) @@ -32,12 +39,12 @@ defmodule PhilomenaWeb.Image.TagController do conn |> put_view(PhilomenaWeb.ImageView) - |> render("_tags.html", layout: false, image: image, changeset: changeset) + |> render("_tags.html", layout: false, tag_change_count: tag_change_count, image: image, changeset: changeset) {:error, :image, changeset, _} -> conn |> put_view(PhilomenaWeb.ImageView) - |> render("_tags.html", layout: false, image: image, changeset: changeset) + |> render("_tags.html", layout: false, tag_change_count: 0, image: image, changeset: changeset) end end end \ No newline at end of file diff --git a/lib/philomena_web/controllers/image_controller.ex b/lib/philomena_web/controllers/image_controller.ex index 6fd494bd..35c1b5fa 100644 --- a/lib/philomena_web/controllers/image_controller.ex +++ b/lib/philomena_web/controllers/image_controller.ex @@ -139,10 +139,13 @@ defmodule PhilomenaWeb.ImageController do defp load_image(conn, _opts) do id = conn.params["id"] - image = + {image, tag_changes, source_changes} = Image |> where(id: ^id) + |> join(:inner_lateral, [i], _ in fragment("SELECT COUNT(*) FROM tag_changes t WHERE t.image_id = ?", i.id)) + |> join(:inner_lateral, [i, _], _ in fragment("SELECT COUNT(*) FROM source_changes s WHERE s.image_id = ?", i.id)) |> preload([:tags, :deleter, user: [awards: :badge]]) + |> select([i, t, s], {i, t.count, s.count}) |> Repo.one() cond do @@ -158,7 +161,10 @@ defmodule PhilomenaWeb.ImageController do render(conn, "deleted.html", image: image) true -> - assign(conn, :image, image) + conn + |> assign(:image, image) + |> assign(:tag_change_count, tag_changes) + |> assign(:source_change_count, source_changes) end end end diff --git a/lib/philomena_web/templates/image/_source.html.slime b/lib/philomena_web/templates/image/_source.html.slime index fc880d84..a9061a03 100644 --- a/lib/philomena_web/templates/image/_source.html.slime +++ b/lib/philomena_web/templates/image/_source.html.slime @@ -35,6 +35,9 @@ - else em> not provided yet - a.button.button--link.button--separate-left href=Routes.image_source_change_path(@conn, :index, @image) title="Source history" - i.fa.fa-history> - ' History + = if @source_change_count > 0 do + a.button.button--link.button--separate-left href=Routes.image_source_change_path(@conn, :index, @image) title="Source history" + i.fa.fa-history> + | History ( + = @source_change_count + | ) diff --git a/lib/philomena_web/templates/image/_tags.html.slime b/lib/philomena_web/templates/image/_tags.html.slime index d98630c0..2bc2df0c 100644 --- a/lib/philomena_web/templates/image/_tags.html.slime +++ b/lib/philomena_web/templates/image/_tags.html.slime @@ -55,8 +55,11 @@ i.fas.fa-edit> ' Tags: - a.button.button--link.button--separate-left href=Routes.image_tag_change_path(@conn, :index, @image) title="Tag history" - i.fa.fa-history> - ' History + = if @tag_change_count > 0 do + a.button.button--link.button--separate-left href=Routes.image_tag_change_path(@conn, :index, @image) title="Tag history" + i.fa.fa-history> + | History ( + = @tag_change_count + | ) = render PhilomenaWeb.TagView, "_tag_list.html", tags: display_order(@image.tags), conn: @conn diff --git a/lib/philomena_web/templates/image/show.html.slime b/lib/philomena_web/templates/image/show.html.slime index ebd53d7d..80f9b8b4 100644 --- a/lib/philomena_web/templates/image/show.html.slime +++ b/lib/philomena_web/templates/image/show.html.slime @@ -9,8 +9,8 @@ = render PhilomenaWeb.ImageView, "_description.html", image: @image, body: @description, conn: @conn = render PhilomenaWeb.Image.DescriptionView, "_form.html", image: @image, changeset: @image_changeset, conn: @conn - = render PhilomenaWeb.ImageView, "_tags.html", image: @image, changeset: @image_changeset, conn: @conn - = render PhilomenaWeb.ImageView, "_source.html", image: @image, changeset: @image_changeset, conn: @conn + = render PhilomenaWeb.ImageView, "_tags.html", image: @image, tag_change_count: @tag_change_count, changeset: @image_changeset, conn: @conn + = render PhilomenaWeb.ImageView, "_source.html", image: @image, source_change_count: @source_change_count, changeset: @image_changeset, conn: @conn = render PhilomenaWeb.ImageView, "_options.html", image: @image, conn: @conn h4 Comments