ChatMessageReferences: Adjust views

This commit is contained in:
lain 2020-06-03 12:49:53 +02:00
parent aa22fce8f4
commit f3ccd50a33
6 changed files with 34 additions and 11 deletions

View File

@ -66,6 +66,13 @@ def for_chat_query(chat) do
)
end
def last_message_for_chat(chat) do
chat
|> for_chat_query()
|> limit(1)
|> Repo.one()
end
def create(chat, object, seen) do
params = %{
chat_id: chat.id,

View File

@ -6,6 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
use Pleroma.Web, :view
alias Pleroma.Activity
alias Pleroma.ChatMessageReference
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.User
@ -14,7 +15,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView do
alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.MastodonAPI.NotificationView
alias Pleroma.Web.MastodonAPI.StatusView
alias Pleroma.Web.PleromaAPI.ChatMessageView
alias Pleroma.Web.PleromaAPI.ChatMessageReferenceView
@parent_types ~w{Like Announce EmojiReact}
@ -138,8 +139,9 @@ defp put_chat_message(response, activity, reading_user, opts) do
object = Object.normalize(activity)
author = User.get_cached_by_ap_id(object.data["actor"])
chat = Pleroma.Chat.get(reading_user.id, author.ap_id)
render_opts = Map.merge(opts, %{object: object, for: reading_user, chat: chat})
chat_message_render = ChatMessageView.render("show.json", render_opts)
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
render_opts = Map.merge(opts, %{for: reading_user, chat_message_reference: cm_ref})
chat_message_render = ChatMessageReferenceView.render("show.json", render_opts)
Map.put(response, :chat_message, chat_message_render)
end

View File

@ -6,22 +6,24 @@ defmodule Pleroma.Web.PleromaAPI.ChatView do
use Pleroma.Web, :view
alias Pleroma.Chat
alias Pleroma.ChatMessageReference
alias Pleroma.User
alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.PleromaAPI.ChatMessageView
alias Pleroma.Web.PleromaAPI.ChatMessageReferenceView
def render("show.json", %{chat: %Chat{} = chat} = opts) do
recipient = User.get_cached_by_ap_id(chat.recipient)
last_message = opts[:message] || Chat.last_message_for_chat(chat)
last_message = opts[:last_message] || ChatMessageReference.last_message_for_chat(chat)
%{
id: chat.id |> to_string(),
account: AccountView.render("show.json", Map.put(opts, :user, recipient)),
unread: chat.unread,
last_message:
last_message && ChatMessageView.render("show.json", chat: chat, object: last_message),
last_message &&
ChatMessageReferenceView.render("show.json", chat_message_reference: last_message),
updated_at: Utils.to_masto_date(chat.updated_at)
}
end

View File

@ -7,6 +7,7 @@ defmodule Pleroma.Web.StreamerView do
alias Pleroma.Activity
alias Pleroma.Chat
alias Pleroma.ChatMessageReference
alias Pleroma.Conversation.Participation
alias Pleroma.Notification
alias Pleroma.User
@ -15,10 +16,15 @@ defmodule Pleroma.Web.StreamerView do
def render("chat_update.json", object, user, recipients) do
chat = Chat.get(user.id, hd(recipients -- [user.ap_id]))
# Explicitly giving the cmr for the object here, so we don't accidentally
# send a later 'last_message' that was inserted between inserting this and
# streaming it out
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
representation =
Pleroma.Web.PleromaAPI.ChatView.render(
"show.json",
%{message: object, chat: chat}
%{last_message: cm_ref, chat: chat}
)
%{

View File

@ -7,6 +7,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
alias Pleroma.Activity
alias Pleroma.Chat
alias Pleroma.ChatMessageReference
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Repo
@ -16,7 +17,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.MastodonAPI.NotificationView
alias Pleroma.Web.MastodonAPI.StatusView
alias Pleroma.Web.PleromaAPI.ChatMessageView
alias Pleroma.Web.PleromaAPI.ChatMessageReferenceView
import Pleroma.Factory
defp test_notifications_rendering(notifications, user, expected_result) do
@ -44,13 +45,15 @@ test "ChatMessage notification" do
object = Object.normalize(activity)
chat = Chat.get(recipient.id, user.ap_id)
cm_ref = ChatMessageReference.for_chat_and_object(chat, object)
expected = %{
id: to_string(notification.id),
pleroma: %{is_seen: false},
type: "pleroma:chat_mention",
account: AccountView.render("show.json", %{user: user, for: recipient}),
chat_message:
ChatMessageView.render("show.json", %{object: object, for: recipient, chat: chat}),
ChatMessageReferenceView.render("show.json", %{chat_message_reference: cm_ref}),
created_at: Utils.to_masto_date(notification.inserted_at)
}

View File

@ -6,11 +6,12 @@ defmodule Pleroma.Web.PleromaAPI.ChatViewTest do
use Pleroma.DataCase
alias Pleroma.Chat
alias Pleroma.ChatMessageReference
alias Pleroma.Object
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.CommonAPI.Utils
alias Pleroma.Web.MastodonAPI.AccountView
alias Pleroma.Web.PleromaAPI.ChatMessageView
alias Pleroma.Web.PleromaAPI.ChatMessageReferenceView
alias Pleroma.Web.PleromaAPI.ChatView
import Pleroma.Factory
@ -39,7 +40,9 @@ test "it represents a chat" do
represented_chat = ChatView.render("show.json", chat: chat)
cm_ref = ChatMessageReference.for_chat_and_object(chat, chat_message)
assert represented_chat[:last_message] ==
ChatMessageView.render("show.json", chat: chat, object: chat_message)
ChatMessageReferenceView.render("show.json", chat_message_reference: cm_ref)
end
end