aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorlain <lain@soykaf.club>2020-06-03 12:49:53 +0200
committerlain <lain@soykaf.club>2020-06-03 12:49:53 +0200
commitf3ccd50a33c9eec3661bf2116fe38542f04986aa (patch)
treece52ee1dc979054911d6d508714f13f7cd2a5d2b /lib
parentaa22fce8f46cf2e7f871b3584fbfff7ac2ebe4c2 (diff)
downloadpleroma-f3ccd50a33c9eec3661bf2116fe38542f04986aa.tar.gz
ChatMessageReferences: Adjust views
Diffstat (limited to 'lib')
-rw-r--r--lib/pleroma/chat_message_reference.ex7
-rw-r--r--lib/pleroma/web/mastodon_api/views/notification_view.ex8
-rw-r--r--lib/pleroma/web/pleroma_api/views/chat_view.ex8
-rw-r--r--lib/pleroma/web/views/streamer_view.ex8
4 files changed, 24 insertions, 7 deletions
diff --git a/lib/pleroma/chat_message_reference.ex b/lib/pleroma/chat_message_reference.ex
index e9ca3dfe8..6808d1365 100644
--- a/lib/pleroma/chat_message_reference.ex
+++ b/lib/pleroma/chat_message_reference.ex
@@ -66,6 +66,13 @@ defmodule Pleroma.ChatMessageReference 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,
diff --git a/lib/pleroma/web/mastodon_api/views/notification_view.ex b/lib/pleroma/web/mastodon_api/views/notification_view.ex
index af15bba48..2ae82eb2d 100644
--- a/lib/pleroma/web/mastodon_api/views/notification_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/notification_view.ex
@@ -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 @@ defmodule Pleroma.Web.MastodonAPI.NotificationView 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
diff --git a/lib/pleroma/web/pleroma_api/views/chat_view.ex b/lib/pleroma/web/pleroma_api/views/chat_view.ex
index 223b64987..331c1d282 100644
--- a/lib/pleroma/web/pleroma_api/views/chat_view.ex
+++ b/lib/pleroma/web/pleroma_api/views/chat_view.ex
@@ -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
diff --git a/lib/pleroma/web/views/streamer_view.ex b/lib/pleroma/web/views/streamer_view.ex
index 5e953d770..616e0c4f2 100644
--- a/lib/pleroma/web/views/streamer_view.ex
+++ b/lib/pleroma/web/views/streamer_view.ex
@@ -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}
)
%{