Chats: Remove `unread` from the db, calculate from unseen messages.
[akkoma] / test / web / pleroma_api / controllers / chat_controller_test.exs
index d79aa3148d4b391a578a63bcfff554730e055180..e62b717995c941f7c4d4457fee7b32dd986a1aaf 100644 (file)
@@ -5,6 +5,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
   use Pleroma.Web.ConnCase, async: true
 
   alias Pleroma.Chat
+  alias Pleroma.ChatMessageReference
   alias Pleroma.Object
   alias Pleroma.User
   alias Pleroma.Web.ActivityPub.ActivityPub
@@ -18,9 +19,12 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
     test "it marks all messages in a chat as read", %{conn: conn, user: user} do
       other_user = insert(:user)
 
-      {:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
+      {:ok, create} = CommonAPI.post_chat_message(other_user, user, "sup")
+      {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
+      object = Object.normalize(create, false)
+      cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
 
-      assert chat.unread == 1
+      assert cm_ref.seen == false
 
       result =
         conn
@@ -29,9 +33,9 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       assert result["unread"] == 0
 
-      {:ok, chat} = Chat.get_or_create(user.id, other_user.ap_id)
+      cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
 
-      assert chat.unread == 0
+      assert cm_ref.seen == true
     end
   end
 
@@ -95,7 +99,7 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
   describe "DELETE /api/v1/pleroma/chats/:id/messages/:message_id" do
     setup do: oauth_access(["write:statuses"])
 
-    test "it deletes a message for the author of the message", %{conn: conn, user: user} do
+    test "it deletes a message from the chat", %{conn: conn, user: user} do
       recipient = insert(:user)
 
       {:ok, message} =
@@ -107,23 +111,32 @@ defmodule Pleroma.Web.PleromaAPI.ChatControllerTest do
 
       chat = Chat.get(user.id, recipient.ap_id)
 
+      cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
+
+      # Deleting your own message removes the message and the reference
       result =
         conn
         |> put_req_header("content-type", "application/json")
-        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
+        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
         |> json_response_and_validate_schema(200)
 
-      assert result["id"] == to_string(object.id)
+      assert result["id"] == cm_ref.id
+      refute ChatMessageReference.get_by_id(cm_ref.id)
+      assert %{data: %{"type" => "Tombstone"}} = Object.get_by_id(object.id)
 
+      # Deleting other people's messages just removes the reference
       object = Object.normalize(other_message, false)
+      cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
 
       result =
         conn
         |> put_req_header("content-type", "application/json")
-        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{object.id}")
-        |> json_response(400)
+        |> delete("/api/v1/pleroma/chats/#{chat.id}/messages/#{cm_ref.id}")
+        |> json_response_and_validate_schema(200)
 
-      assert result == %{"error" => "could_not_delete"}
+      assert result["id"] == cm_ref.id
+      refute ChatMessageReference.get_by_id(cm_ref.id)
+      assert Object.get_by_id(object.id)
     end
   end