From 878b923a20d9d116dfe0b2287ad5e86fe8e8e620 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sat, 21 Dec 2019 16:37:06 -0500 Subject: [PATCH] copy comments when images are merged --- lib/philomena/comments.ex | 19 +++++++++++++++++++ lib/philomena/images.ex | 2 ++ .../controllers/image/tag_controller.ex | 4 +++- 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/philomena/comments.ex b/lib/philomena/comments.ex index 9ed501a7..a1d207a5 100644 --- a/lib/philomena/comments.ex +++ b/lib/philomena/comments.ex @@ -162,6 +162,14 @@ defmodule Philomena.Comments do |> Repo.update() end + def migrate_comments(image, duplicate_of_image) do + Comment + |> where(image_id: ^image.id) + |> Repo.update_all(set: [image_id: duplicate_of_image.id]) + + reindex_comments(duplicate_of_image) + end + @doc """ Returns an `%Ecto.Changeset{}` for tracking comment changes. @@ -187,6 +195,17 @@ defmodule Philomena.Comments do comment end + def reindex_comments(image) do + spawn fn -> + Comment + |> preload(^indexing_preloads()) + |> where(image_id: ^image.id) + |> Comment.reindex() + end + + image + end + def indexing_preloads do [:user, image: :tags] end diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index 8331520c..9d8bcd53 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -20,6 +20,7 @@ defmodule Philomena.Images do alias Philomena.Notifications alias Philomena.Interactions alias Philomena.Reports.Report + alias Philomena.Comments @doc """ Gets a single image. @@ -321,6 +322,7 @@ defmodule Philomena.Images do case result do {:ok, changes} -> tags = Tags.copy_tags(image, duplicate_of_image) + Comments.migrate_comments(image, duplicate_of_image) Interactions.migrate_interactions(image, duplicate_of_image) {:ok, %{changes | tags: changes.tags ++ tags}} diff --git a/lib/philomena_web/controllers/image/tag_controller.ex b/lib/philomena_web/controllers/image/tag_controller.ex index 4c703e28..052d2416 100644 --- a/lib/philomena_web/controllers/image/tag_controller.ex +++ b/lib/philomena_web/controllers/image/tag_controller.ex @@ -3,6 +3,7 @@ defmodule PhilomenaWeb.Image.TagController do alias Philomena.TagChanges.TagChange alias Philomena.UserStatistics + alias Philomena.Comments alias Philomena.Images.Image alias Philomena.Images alias Philomena.Tags @@ -21,6 +22,7 @@ defmodule PhilomenaWeb.Image.TagController do case Images.update_tags(image, attributes, image_params) do {:ok, %{image: {image, added_tags, removed_tags}}} -> + Comments.reindex_comments(image) Images.reindex_image(image) Tags.reindex_tags(added_tags ++ removed_tags) @@ -50,4 +52,4 @@ defmodule PhilomenaWeb.Image.TagController do |> render("_tags.html", layout: false, tag_change_count: 0, image: image, changeset: changeset) end end -end \ No newline at end of file +end