diff --git a/lib/philomena/conversations.ex b/lib/philomena/conversations.ex index 757c295b..597b64f7 100644 --- a/lib/philomena/conversations.ex +++ b/lib/philomena/conversations.ex @@ -7,6 +7,7 @@ defmodule Philomena.Conversations do alias Ecto.Multi alias Philomena.Repo alias Philomena.Reports + alias Philomena.Reports.Report alias Philomena.Conversations.Conversation @doc """ @@ -207,7 +208,13 @@ defmodule Philomena.Conversations do |> Repo.transaction() end - def approve_conversation_message(message) do + def approve_conversation_message(message, user) do + reports_query = + Report + |> where(reportable_type: "Conversation", reportable_id: ^message.conversation_id) + |> select([r], r.id) + |> update(set: [open: false, state: "closed", admin_id: ^user.id]) + message_query = message |> Message.approve_changeset() @@ -216,12 +223,20 @@ defmodule Philomena.Conversations do Conversation |> where(id: ^message.conversation_id) - now = DateTime.utc_now() - Multi.new() |> Multi.update(:message, message_query) |> Multi.update_all(:conversation, conversation_query, set: [to_read: false]) + |> Multi.update_all(:reports, reports_query, []) |> Repo.transaction() + |> case do + {:ok, %{reports: {_count, reports}} = result} -> + Reports.reindex_reports(reports) + + {:ok, result} + + error -> + error + end end def report_non_approved(id) do diff --git a/lib/philomena_web/controllers/conversation/message/approve_controller.ex b/lib/philomena_web/controllers/conversation/message/approve_controller.ex index a5265a59..db03d5c5 100644 --- a/lib/philomena_web/controllers/conversation/message/approve_controller.ex +++ b/lib/philomena_web/controllers/conversation/message/approve_controller.ex @@ -15,7 +15,7 @@ defmodule PhilomenaWeb.Conversation.Message.ApproveController do def create(conn, _params) do message = conn.assigns.message - {:ok, _message} = Conversations.approve_conversation_message(message) + {:ok, _message} = Conversations.approve_conversation_message(message, conn.assigns.current_user) conn |> put_flash(:info, "Conversation message approved.")