Conversations: Load relations in one query.

This commit is contained in:
lain 2019-08-14 17:01:11 +02:00
parent f73212b2a3
commit d3af9e19ed
3 changed files with 28 additions and 6 deletions

View file

@ -94,10 +94,20 @@ defmodule Pleroma.Conversation.Participation 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

View file

@ -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 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
participation =
participation_id
|> Participation.get()
|> Repo.preload(:conversation)
|> Participation.get(preload: [:conversation])
if user.id == participation.user_id do
activities =

View file

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