ChatController: Don't return chats for user you've blocked.
authorlain <lain@soykaf.club>
Tue, 12 May 2020 14:43:04 +0000 (16:43 +0200)
committerlain <lain@soykaf.club>
Tue, 12 May 2020 14:43:04 +0000 (16:43 +0200)
lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
test/web/pleroma_api/controllers/chat_controller_test.exs

index 4ce3e7419cd44bb07e24af4c120c1d15ea8e7c72..496cb8e876105919cd5e3094db80e82709239481 100644 (file)
@@ -102,10 +102,13 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
     end
   end
 
-  def index(%{assigns: %{user: %{id: user_id}}} = conn, params) do
+  def index(%{assigns: %{user: %{id: user_id} = user}} = conn, params) do
+    blocked_ap_ids = User.blocked_users_ap_ids(user)
+
     chats =
       from(c in Chat,
         where: c.user_id == ^user_id,
+        where: c.recipient not in ^blocked_ap_ids,
         order_by: [desc: c.updated_at]
       )
       |> Pagination.fetch_paginated(params |> stringify_keys)
index 861ef10b09d1824819d20beb16639b90cc9d26e8..037dd229728b696f4d99cf0a35c54c0ed2ed4cd9 100644 (file)
@@ -6,6 +6,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
   alias Pleroma.Chat
   alias Pleroma.Object
+  alias Pleroma.User
   alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.CommonAPI
 
@@ -209,6 +210,28 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
   describe "GET /api/v1/pleroma/chats" do
     setup do: oauth_access(["read:statuses"])
 
+    test "it does not return chats with users you blocked", %{conn: conn, user: user} do
+      recipient = insert(:user)
+
+      {:ok, _} = Chat.get_or_create(user.id, recipient.ap_id)
+
+      result =
+        conn
+        |> get("/api/v1/pleroma/chats")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 1
+
+      User.block(user, recipient)
+
+      result =
+        conn
+        |> get("/api/v1/pleroma/chats")
+        |> json_response_and_validate_schema(200)
+
+      assert length(result) == 0
+    end
+
     test "it paginates", %{conn: conn, user: user} do
       Enum.each(1..30, fn _ ->
         recipient = insert(:user)