ChatController: Don't die if the recipient is gone.
authorlain <lain@soykaf.club>
Thu, 27 Aug 2020 10:13:18 +0000 (12:13 +0200)
committerlain <lain@soykaf.club>
Thu, 27 Aug 2020 10:13:18 +0000 (12:13 +0200)
lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
test/web/pleroma_api/controllers/chat_controller_test.exs

index e8a1746d46a82d459dedc10799c58e92f4af2063..1f2e953f761cc4e915263946d13e9ad97a3cf97d 100644 (file)
@@ -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()
 
index d71e80d03ca37799d3664f9b79068f1730f2d2b8..7be5fe09c16b119230659137c09e1e81ce2f106c 100644 (file)
@@ -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)