From b513e66578cea6ba74820c0bc49c579705ba3001 Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Thu, 20 Apr 2023 14:02:24 -0400 Subject: [PATCH] Clear deleted posts and topics from listings --- lib/philomena/posts.ex | 12 ++++++++++++ lib/philomena/topics.ex | 21 +++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/philomena/posts.ex b/lib/philomena/posts.ex index cefde12c..7a7fb7a5 100644 --- a/lib/philomena/posts.ex +++ b/lib/philomena/posts.ex @@ -222,11 +222,23 @@ defmodule Philomena.Posts do |> select([r], r.id) |> update(set: [open: false, state: "closed", admin_id: ^user.id]) + topics = + Topic + |> where(last_post_id: ^post.id) + |> update(set: [last_post_id: nil]) + + forums = + Forum + |> where(last_post_id: ^post.id) + |> update(set: [last_post_id: nil]) + post = Post.hide_changeset(post, attrs, user) Multi.new() |> Multi.update(:post, post) |> Multi.update_all(:reports, reports, []) + |> Multi.update_all(:topics, topics, []) + |> Multi.update_all(:forums, forums, []) |> Repo.transaction() |> case do {:ok, %{post: post, reports: {_count, reports}}} -> diff --git a/lib/philomena/topics.ex b/lib/philomena/topics.ex index 8adeb927..4892c06d 100644 --- a/lib/philomena/topics.ex +++ b/lib/philomena/topics.ex @@ -268,8 +268,25 @@ defmodule Philomena.Topics do end def hide_topic(topic, deletion_reason, user) do - Topic.hide_changeset(topic, deletion_reason, user) - |> Repo.update() + topic_changes = Topic.hide_changeset(topic, deletion_reason, user) + + forums = + Forum + |> join(:inner, [f], _ in assoc(f, :last_post)) + |> where([f, p], p.topic_id == ^topic.id) + |> update(set: [last_post_id: nil]) + + Multi.new() + |> Multi.update(:topic, topic_changes) + |> Multi.update_all(:forums, forums, []) + |> Repo.transaction() + |> case do + {:ok, %{topic: topic}} -> + {:ok, topic} + + error -> + error + end end def unhide_topic(topic) do