copy comments when images are merged

This commit is contained in:
byte[] 2019-12-21 16:37:06 -05:00
parent 3cbd57ba7c
commit 878b923a20
3 changed files with 24 additions and 1 deletions

View file

@ -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

View file

@ -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}}

View file

@ -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)