aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2020-05-10 13:00:01 +0200
committerlain <lain@soykaf.club>2020-05-10 13:00:01 +0200
commitf335e1404a9cd19451b531e32e3591aa323761ff (patch)
treec55690b40016354a56150822b79d8b83d3f6de03
parente297d8c649a03510023cff61dc6e0c7131bc29dc (diff)
downloadpleroma-f335e1404a9cd19451b531e32e3591aa323761ff.tar.gz
ChatView: Add the last message to the view.
-rw-r--r--lib/pleroma/chat.ex34
-rw-r--r--lib/pleroma/web/pleroma_api/controllers/chat_controller.ex23
-rw-r--r--lib/pleroma/web/pleroma_api/views/chat_view.ex7
-rw-r--r--test/web/pleroma_api/views/chat_view_test.exs17
4 files changed, 59 insertions, 22 deletions
diff --git a/lib/pleroma/chat.ex b/lib/pleroma/chat.ex
index 1a092b992..6a03ee3c1 100644
--- a/lib/pleroma/chat.ex
+++ b/lib/pleroma/chat.ex
@@ -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])
diff --git a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
index 450d85332..1ef3477c8 100644
--- a/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
+++ b/lib/pleroma/web/pleroma_api/controllers/chat_controller.ex
@@ -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
diff --git a/lib/pleroma/web/pleroma_api/views/chat_view.ex b/lib/pleroma/web/pleroma_api/views/chat_view.ex
index bc3af5ef5..21f0612ff 100644
--- a/lib/pleroma/web/pleroma_api/views/chat_view.ex
+++ b/lib/pleroma/web/pleroma_api/views/chat_view.ex
@@ -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
diff --git a/test/web/pleroma_api/views/chat_view_test.exs b/test/web/pleroma_api/views/chat_view_test.exs
index 1ac3483d1..8568d98c6 100644
--- a/test/web/pleroma_api/views/chat_view_test.exs
+++ b/test/web/pleroma_api/views/chat_view_test.exs
@@ -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