ChatMessageReferences: Move tests
authorlain <lain@soykaf.club>
Wed, 3 Jun 2020 10:56:39 +0000 (12:56 +0200)
committerlain <lain@soykaf.club>
Wed, 3 Jun 2020 10:56:39 +0000 (12:56 +0200)
lib/pleroma/chat.ex
test/chat_message_reference_test.exs [new file with mode: 0644]
test/chat_test.exs

index 211b872f9722c17d2cc7aedf642ce5c8b54325dc..65938c7a450ba162c26b2438c697fbacb2518019 100644 (file)
@@ -6,9 +6,7 @@ defmodule Pleroma.Chat do
   use Ecto.Schema
 
   import Ecto.Changeset
-  import Ecto.Query
 
-  alias Pleroma.Object
   alias Pleroma.Repo
   alias Pleroma.User
 
@@ -26,38 +24,6 @@ defmodule Pleroma.Chat do
     timestamps()
   end
 
-  def last_message_for_chat(chat) do
-    messages_for_chat_query(chat)
-    |> order_by(desc: :id)
-    |> limit(1)
-    |> Repo.one()
-  end
-
-  def messages_for_chat_query(chat) do
-    chat =
-      chat
-      |> Repo.preload(:user)
-
-    from(o in Object,
-      where: fragment("?->>'type' = ?", o.data, "ChatMessage"),
-      where:
-        fragment(
-          """
-          (?->>'actor' = ? and ?->'to' = ?) 
-          OR (?->>'actor' = ? and ?->'to' = ?) 
-          """,
-          o.data,
-          ^chat.user.ap_id,
-          o.data,
-          ^[chat.recipient],
-          o.data,
-          ^chat.recipient,
-          o.data,
-          ^[chat.user.ap_id]
-        )
-    )
-  end
-
   def creation_cng(struct, params) do
     struct
     |> cast(params, [:user_id, :recipient, :unread])
diff --git a/test/chat_message_reference_test.exs b/test/chat_message_reference_test.exs
new file mode 100644 (file)
index 0000000..963a0e2
--- /dev/null
@@ -0,0 +1,29 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.ChatMessageReferencTest do
+  use Pleroma.DataCase, async: true
+
+  alias Pleroma.Chat
+  alias Pleroma.ChatMessageReference
+  alias Pleroma.Web.CommonAPI
+
+  import Pleroma.Factory
+
+  describe "messages" do
+    test "it returns the last message in a chat" do
+      user = insert(:user)
+      recipient = insert(:user)
+
+      {:ok, _message_1} = CommonAPI.post_chat_message(user, recipient, "hey")
+      {:ok, _message_2} = CommonAPI.post_chat_message(recipient, user, "ho")
+
+      {:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
+
+      message = ChatMessageReference.last_message_for_chat(chat)
+
+      assert message.object.data["content"] == "ho"
+    end
+  end
+end
index dfcb6422e113de1dad9d81aaa154c287009b6354..42e01fe2728933405e52be8f16970ba1ded9e8c8 100644 (file)
@@ -10,22 +10,6 @@ defmodule Pleroma.ChatTest do
 
   import Pleroma.Factory
 
-  describe "messages" do
-    test "it returns the last message in a chat" do
-      user = insert(:user)
-      recipient = insert(:user)
-
-      {:ok, _message_1} = CommonAPI.post_chat_message(user, recipient, "hey")
-      {:ok, _message_2} = CommonAPI.post_chat_message(recipient, user, "ho")
-
-      {:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
-
-      message = Chat.last_message_for_chat(chat)
-
-      assert message.data["content"] == "ho"
-    end
-  end
-
   describe "creation and getting" do
     test "it only works if the recipient is a valid user (for now)" do
       user = insert(:user)