From ff5c01ce07b4c7b35d38c0f0655c41063de8cce6 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Mon, 14 Sep 2020 19:25:31 -0400 Subject: [PATCH] adjust counters during wipes (derpibooru/philomena#197) --- lib/philomena/user_downvote_wipe.ex | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/lib/philomena/user_downvote_wipe.ex b/lib/philomena/user_downvote_wipe.ex index c2751376..06c69aea 100644 --- a/lib/philomena/user_downvote_wipe.ex +++ b/lib/philomena/user_downvote_wipe.ex @@ -1,6 +1,7 @@ defmodule Philomena.UserDownvoteWipe do alias Philomena.Batch alias Philomena.Elasticsearch + alias Philomena.Users.User alias Philomena.Images.Image alias Philomena.Images alias Philomena.ImageVotes.ImageVote @@ -13,7 +14,13 @@ defmodule Philomena.UserDownvoteWipe do |> where(user_id: ^user.id, up: false) |> Batch.query_batches([id_field: :image_id], fn queryable -> {_, image_ids} = Repo.delete_all(select(queryable, [i_v], i_v.image_id)) - Repo.update_all(where(Image, [i], i.id in ^image_ids), inc: [downvotes_count: -1, score: 1]) + + {count, nil} = + Repo.update_all(where(Image, [i], i.id in ^image_ids), + inc: [downvotes_count: -1, score: 1] + ) + + Repo.update_all(where(User, id: ^user.id), inc: [votes_cast_count: -count]) reindex(image_ids) end) @@ -24,7 +31,12 @@ defmodule Philomena.UserDownvoteWipe do |> Batch.query_batches([id_field: :image_id], fn queryable -> {_, image_ids} = Repo.delete_all(select(queryable, [i_v], i_v.image_id)) - Repo.update_all(where(Image, [i], i.id in ^image_ids), inc: [upvotes_count: -1, score: -1]) + {count, nil} = + Repo.update_all(where(Image, [i], i.id in ^image_ids), + inc: [upvotes_count: -1, score: -1] + ) + + Repo.update_all(where(User, id: ^user.id), inc: [votes_cast_count: -count]) reindex(image_ids) end) @@ -33,7 +45,11 @@ defmodule Philomena.UserDownvoteWipe do |> where(user_id: ^user.id) |> Batch.query_batches([id_field: :image_id], fn queryable -> {_, image_ids} = Repo.delete_all(select(queryable, [i_f], i_f.image_id)) - Repo.update_all(where(Image, [i], i.id in ^image_ids), inc: [faves_count: -1]) + + {count, nil} = + Repo.update_all(where(Image, [i], i.id in ^image_ids), inc: [faves_count: -1]) + + Repo.update_all(where(User, id: ^user.id), inc: [images_favourited_count: -count]) reindex(image_ids) end)