From d3af9e19edb32e04d101e50ae2868ba6f66cbed9 Mon Sep 17 00:00:00 2001
From: lain <lain@soykaf.club>
Date: Wed, 14 Aug 2019 17:01:11 +0200
Subject: [PATCH] Conversations: Load relations in one query.

---
 lib/pleroma/conversation/participation.ex        | 16 +++++++++++++---
 .../web/pleroma_api/pleroma_api_controller.ex    |  4 +---
 test/conversation/participation_test.exs         | 14 ++++++++++++++
 3 files changed, 28 insertions(+), 6 deletions(-)

diff --git a/lib/pleroma/conversation/participation.ex b/lib/pleroma/conversation/participation.ex
index d17b6f7c5..ea5b9fe17 100644
--- a/lib/pleroma/conversation/participation.ex
+++ b/lib/pleroma/conversation/participation.ex
@@ -94,10 +94,20 @@ def for_user_with_last_activity_id(user, params \\ %{}) do
     |> Enum.filter(& &1.last_activity_id)
   end
 
-  def get(nil), do: nil
+  def get(_, _ \\ [])
+  def get(nil, _), do: nil
 
-  def get(id) do
-    Repo.get(__MODULE__, id)
+  def get(id, params) do
+    query =
+      if preload = params[:preload] do
+        from(p in __MODULE__,
+          preload: ^preload
+        )
+      else
+        __MODULE__
+      end
+
+    Repo.get(query, id)
   end
 
   def set_recipients(participation, user_ids) do
diff --git a/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex b/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex
index 6d74d418e..b6d2bf86b 100644
--- a/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex
+++ b/lib/pleroma/web/pleroma_api/pleroma_api_controller.ex
@@ -8,7 +8,6 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
   import Pleroma.Web.ControllerHelper, only: [add_link_headers: 7]
 
   alias Pleroma.Conversation.Participation
-  alias Pleroma.Repo
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.MastodonAPI.ConversationView
   alias Pleroma.Web.MastodonAPI.StatusView
@@ -34,8 +33,7 @@ def conversation_statuses(
 
     participation =
       participation_id
-      |> Participation.get()
-      |> Repo.preload(:conversation)
+      |> Participation.get(preload: [:conversation])
 
     if user.id == participation.user_id do
       activities =
diff --git a/test/conversation/participation_test.exs b/test/conversation/participation_test.exs
index 7958e8e89..a27167d42 100644
--- a/test/conversation/participation_test.exs
+++ b/test/conversation/participation_test.exs
@@ -8,6 +8,20 @@ defmodule Pleroma.Conversation.ParticipationTest do
   alias Pleroma.Conversation.Participation
   alias Pleroma.Web.CommonAPI
 
+  test "getting a participation will also preload things" do
+    user = insert(:user)
+    other_user = insert(:user)
+
+    {:ok, _activity} =
+      CommonAPI.post(user, %{"status" => "Hey @#{other_user.nickname}.", "visibility" => "direct"})
+
+    [participation] = Participation.for_user(user)
+
+    participation = Participation.get(participation.id, preload: [:conversation])
+
+    assert %Pleroma.Conversation{} = participation.conversation
+  end
+
   test "for a new conversation, it sets the recipents of the participation" do
     user = insert(:user)
     other_user = insert(:user)