From: lain <lain@soykaf.club>
Date: Thu, 27 Aug 2020 10:13:18 +0000 (+0200)
Subject: ChatController: Don't die if the recipient is gone.
X-Git-Url: https://git.squeep.com/?a=commitdiff_plain;h=78939c1d161f09ac38348fc02e8f4a83d8d82d2d;p=akkoma

ChatController: Don't die if the recipient is gone.
---

diff --git a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
index e8a1746d4..1f2e953f7 100644
--- a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
+++ b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
@@ -149,7 +149,9 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
       from(c in Chat,
         where: c.user_id == ^user_id,
         where: c.recipient not in ^blocked_ap_ids,
-        order_by: [desc: c.updated_at]
+        order_by: [desc: c.updated_at],
+        inner_join: u in User,
+        on: u.ap_id == c.recipient
       )
       |> Repo.all()
 
diff --git a/test/web/pleroma_api/controllers/chat_controller_test.exs b/test/web/pleroma_api/controllers/chat_controller_test.exs
index d71e80d03..7be5fe09c 100644
--- a/test/web/pleroma_api/controllers/chat_controller_test.exs
+++ b/test/web/pleroma_api/controllers/chat_controller_test.exs
@@ -267,6 +267,21 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
   describe "GET /api/v1/pleroma/chats" do
     setup do: oauth_access(["read:chats"])
 
+    test "it does not return chats with deleted users", %{conn: conn, user: user} do
+      recipient = insert(:user)
+      {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
+
+      Pleroma.Repo.delete(recipient)
+      User.invalidate_cache(recipient)
+
+      result =
+        conn
+        |> get("/api/v1/pleroma/chats")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 0
+    end
+
     test "it does not return chats with users you blocked", %{conn: conn, user: user} do
       recipient = insert(:user)