ChatView: Add the last message to the view.
authorlain <lain@soykaf.club>
Sun, 10 May 2020 11:00:01 +0000 (13:00 +0200)
committerlain <lain@soykaf.club>
Sun, 10 May 2020 11:00:01 +0000 (13:00 +0200)
lib/pleroma/chat.ex
lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
lib/pleroma/web/pleroma_api/views/chat_view.ex
test/web/pleroma_api/views/chat_view_test.exs

index 1a092b9924f2be366f840f72798672b3008c08a6..6a03ee3c1d2e687a93ab76cf1b6e72749c43c45f 100644 (file)
@@ -4,8 +4,11 @@
 
 defmodule Pleroma.Chat do
   use Ecto.Schema
+
   import Ecto.Changeset
+  import Ecto.Query
 
+  alias Pleroma.Object
   alias Pleroma.Repo
   alias Pleroma.User
 
@@ -23,6 +26,37 @@ defmodule Pleroma.Chat do
     timestamps()
   end
 
+  def last_message_for_chat(chat) do
+    messages_for_chat_query(chat)
+    |> order_by(desc: :id)
+    |> 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])
index 450d85332c7dc45d3340e5ed4bd1785e413281ac..1ef3477c8874bc01753b26a7bfb32d7195414217 100644 (file)
@@ -14,9 +14,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
   alias Pleroma.Web.PleromaAPI.ChatMessageView
   alias Pleroma.Web.PleromaAPI.ChatView
 
-  import Pleroma.Web.ActivityPub.ObjectValidator, only: [stringify_keys: 1]
-
   import Ecto.Query
+  import Pleroma.Web.ActivityPub.ObjectValidator, only: [stringify_keys: 1]
 
   # TODO
   # - Error handling
@@ -65,24 +64,8 @@ defmodule Pleroma.Web.PleromaAPI.ChatController do
   def messages(%{assigns: %{user: %{id: user_id} = user}} = conn, %{id: id} = params) do
     with %Chat{} = chat <- Repo.get_by(Chat, id: id, user_id: user_id) do
       messages =
-        from(o in Object,
-          where: fragment("?->>'type' = ?", o.data, "ChatMessage"),
-          where:
-            fragment(
-              """
-              (?->>'actor' = ? and ?->'to' = ?) 
-              OR (?->>'actor' = ? and ?->'to' = ?) 
-              """,
-              o.data,
-              ^user.ap_id,
-              o.data,
-              ^[chat.recipient],
-              o.data,
-              ^chat.recipient,
-              o.data,
-              ^[user.ap_id]
-            )
-        )
+        chat
+        |> Chat.messages_for_chat_query()
         |> Pagination.fetch_paginated(params |> stringify_keys())
 
       conn
index bc3af5ef511853f9dfe10986b0d27e1090b7e062..21f0612fff9a0d154fbeaa48e077bc0cb97fb08f 100644 (file)
@@ -8,14 +8,19 @@ defmodule Pleroma.Web.PleromaAPI.ChatView do
   alias Pleroma.Chat
   alias Pleroma.User
   alias Pleroma.Web.MastodonAPI.AccountView
+  alias Pleroma.Web.PleromaAPI.ChatMessageView
 
   def render("show.json", %{chat: %Chat{} = chat} = opts) do
     recipient = User.get_cached_by_ap_id(chat.recipient)
 
+    last_message = Chat.last_message_for_chat(chat)
+
     %{
       id: chat.id |> to_string(),
       account: AccountView.render("show.json", Map.put(opts, :user, recipient)),
-      unread: chat.unread
+      unread: chat.unread,
+      last_message:
+        last_message && ChatMessageView.render("show.json", chat: chat, object: last_message)
     }
   end
 
index 1ac3483d115306a86a81112fa0b60dc3b7ec1875..8568d98c631423bf605aadfbae3a51c81ea45c7f 100644 (file)
@@ -6,8 +6,11 @@ defmodule Pleroma.Web.PleromaAPI.ChatViewTest do
   use Pleroma.DataCase
 
   alias Pleroma.Chat
+  alias Pleroma.Object
+  alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.MastodonAPI.AccountView
   alias Pleroma.Web.PleromaAPI.ChatView
+  alias Pleroma.Web.PleromaAPI.ChatMessageView
 
   import Pleroma.Factory
 
@@ -22,7 +25,19 @@ defmodule Pleroma.Web.PleromaAPI.ChatViewTest do
     assert represented_chat == %{
              id: "#{chat.id}",
              account: AccountView.render("show.json", user: recipient),
-             unread: 0
+             unread: 0,
+             last_message: nil
            }
+
+    {:ok, chat_message_creation} = CommonAPI.post_chat_message(user, recipient, "hello")
+
+    chat_message = Object.normalize(chat_message_creation, false)
+
+    {:ok, chat} = Chat.get_or_create(user.id, recipient.ap_id)
+
+    represented_chat = ChatView.render("show.json", chat: chat)
+
+    assert represented_chat[:last_message] ==
+             ChatMessageView.render("show.json", chat: chat, object: chat_message)
   end
 end