order_by: [desc: p.updated_at],
preload: [conversation: [:users]]
)
+ |> restrict_recipients(user, params)
|> Pleroma.Pagination.fetch_paginated(params)
end
+ def restrict_recipients(query, user, %{"recipients" => user_ids}) do
+ user_ids =
+ [user.id | user_ids]
+ |> Enum.uniq()
+ |> Enum.reduce([], fn user_id, acc ->
+ case FlakeId.Ecto.CompatType.dump(user_id) do
+ {:ok, user_id} -> [user_id | acc]
+ _ -> acc
+ end
+ end)
+
+ conversation_subquery =
+ __MODULE__
+ |> group_by([p], p.conversation_id)
+ |> having(
+ [p],
+ count(p.user_id) == ^length(user_ids) and
+ fragment("array_agg(?) @> ?", p.user_id, ^user_ids)
+ )
+ |> select([p], %{id: p.conversation_id})
+
+ query
+ |> join(:inner, [p], c in subquery(conversation_subquery), on: p.conversation_id == c.id)
+ end
+
+ def restrict_recipients(query, _, _), do: query
+
def for_user_and_conversation(user, conversation) do
from(p in __MODULE__,
where: p.user_id == ^user.id,
assert User.get_cached_by_id(user_one.id).unread_conversation_count == 0
end
+ test "filters conversations by recipients", %{conn: conn} do
+ user_one = insert(:user)
+ user_two = insert(:user)
+ user_three = insert(:user)
+
+ {:ok, direct1} =
+ CommonAPI.post(user_one, %{
+ "status" => "Hi @#{user_two.nickname}!",
+ "visibility" => "direct"
+ })
+
+ {:ok, _direct2} =
+ CommonAPI.post(user_one, %{
+ "status" => "Hi @#{user_three.nickname}!",
+ "visibility" => "direct"
+ })
+
+ {:ok, direct3} =
+ CommonAPI.post(user_one, %{
+ "status" => "Hi @#{user_two.nickname}, @#{user_three.nickname}!",
+ "visibility" => "direct"
+ })
+
+ {:ok, _direct4} =
+ CommonAPI.post(user_two, %{
+ "status" => "Hi @#{user_three.nickname}!",
+ "visibility" => "direct"
+ })
+
+ {:ok, direct5} =
+ CommonAPI.post(user_two, %{
+ "status" => "Hi @#{user_one.nickname}!",
+ "visibility" => "direct"
+ })
+
+ [conversation1, conversation2] =
+ conn
+ |> assign(:user, user_one)
+ |> get("/api/v1/conversations", %{"recipients" => [user_two.id]})
+ |> json_response(200)
+
+ assert conversation1["last_status"]["id"] == direct5.id
+ assert conversation2["last_status"]["id"] == direct1.id
+
+ [conversation1] =
+ conn
+ |> assign(:user, user_one)
+ |> get("/api/v1/conversations", %{"recipients" => [user_two.id, user_three.id]})
+ |> json_response(200)
+
+ assert conversation1["last_status"]["id"] == direct3.id
+ end
+
test "updates the last_status on reply", %{conn: conn} do
user_one = insert(:user)
user_two = insert(:user)