forked from mirrors/akkoma
Conversations: Load relations in one query.
This commit is contained in:
parent
f73212b2a3
commit
d3af9e19ed
3 changed files with 28 additions and 6 deletions
|
@ -94,10 +94,20 @@ defmodule Pleroma.Conversation.Participation do
|
||||||
|> Enum.filter(& &1.last_activity_id)
|
|> Enum.filter(& &1.last_activity_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def get(nil), do: nil
|
def get(_, _ \\ [])
|
||||||
|
def get(nil, _), do: nil
|
||||||
|
|
||||||
def get(id) do
|
def get(id, params) do
|
||||||
Repo.get(__MODULE__, id)
|
query =
|
||||||
|
if preload = params[:preload] do
|
||||||
|
from(p in __MODULE__,
|
||||||
|
preload: ^preload
|
||||||
|
)
|
||||||
|
else
|
||||||
|
__MODULE__
|
||||||
|
end
|
||||||
|
|
||||||
|
Repo.get(query, id)
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_recipients(participation, user_ids) do
|
def set_recipients(participation, user_ids) do
|
||||||
|
|
|
@ -8,7 +8,6 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
|
||||||
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 7]
|
import Pleroma.Web.ControllerHelper, only: [add_link_headers: 7]
|
||||||
|
|
||||||
alias Pleroma.Conversation.Participation
|
alias Pleroma.Conversation.Participation
|
||||||
alias Pleroma.Repo
|
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
alias Pleroma.Web.MastodonAPI.ConversationView
|
alias Pleroma.Web.MastodonAPI.ConversationView
|
||||||
alias Pleroma.Web.MastodonAPI.StatusView
|
alias Pleroma.Web.MastodonAPI.StatusView
|
||||||
|
@ -34,8 +33,7 @@ defmodule Pleroma.Web.PleromaAPI.PleromaAPIController do
|
||||||
|
|
||||||
participation =
|
participation =
|
||||||
participation_id
|
participation_id
|
||||||
|> Participation.get()
|
|> Participation.get(preload: [:conversation])
|
||||||
|> Repo.preload(:conversation)
|
|
||||||
|
|
||||||
if user.id == participation.user_id do
|
if user.id == participation.user_id do
|
||||||
activities =
|
activities =
|
||||||
|
|
|
@ -8,6 +8,20 @@ defmodule Pleroma.Conversation.ParticipationTest do
|
||||||
alias Pleroma.Conversation.Participation
|
alias Pleroma.Conversation.Participation
|
||||||
alias Pleroma.Web.CommonAPI
|
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
|
test "for a new conversation, it sets the recipents of the participation" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
other_user = insert(:user)
|
other_user = insert(:user)
|
||||||
|
|
Loading…
Reference in a new issue