Streamer, SideEffects: Stream out ChatMessageReferences

Saves us a few calles to fetch things from the DB that we already
have.
This commit is contained in:
lain 2020-06-03 16:45:04 +02:00
commit fb4ae9c720
5 changed files with 58 additions and 50 deletions

View file

@ -140,11 +140,15 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
|> Enum.each(fn [user, other_user] ->
if user.local do
{:ok, chat} = Chat.bump_or_create(user.id, other_user.ap_id)
ChatMessageReference.create(chat, object, user.ap_id == actor.ap_id)
{:ok, cm_ref} = ChatMessageReference.create(chat, object, user.ap_id == actor.ap_id)
Streamer.stream(
["user", "user:pleroma_chat"],
{user, %{cm_ref | chat: chat, object: object}}
)
end
end)
Streamer.stream(["user", "user:pleroma_chat"], object)
{:ok, object, meta}
end
end

View file

@ -6,11 +6,11 @@ defmodule Pleroma.Web.Streamer do
require Logger
alias Pleroma.Activity
alias Pleroma.ChatMessageReference
alias Pleroma.Config
alias Pleroma.Conversation.Participation
alias Pleroma.Notification
alias Pleroma.Object
alias Pleroma.Repo
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Visibility
@ -201,22 +201,15 @@ defmodule Pleroma.Web.Streamer do
end)
end
defp do_stream(topic, %{data: %{"type" => "ChatMessage"}} = object)
defp do_stream(topic, {user, %ChatMessageReference{} = cm_ref})
when topic in ["user", "user:pleroma_chat"] do
recipients = [object.data["actor"] | object.data["to"]]
topic = "#{topic}:#{user.id}"
topics =
%{ap_id: recipients, local: true}
|> Pleroma.User.Query.build()
|> Repo.all()
|> Enum.map(fn %{id: id} = user -> {user, "#{topic}:#{id}"} end)
text = StreamerView.render("chat_update.json", %{chat_message_reference: cm_ref})
Enum.each(topics, fn {user, topic} ->
Registry.dispatch(@registry, topic, fn list ->
Enum.each(list, fn {pid, _auth} ->
text = StreamerView.render("chat_update.json", object, user, recipients)
send(pid, {:text, text})
end)
Registry.dispatch(@registry, topic, fn list ->
Enum.each(list, fn {pid, _auth} ->
send(pid, {:text, text})
end)
end)
end

View file

@ -6,36 +6,11 @@ defmodule Pleroma.Web.StreamerView do
use Pleroma.Web, :view
alias Pleroma.Activity
alias Pleroma.Chat
alias Pleroma.ChatMessageReference
alias Pleroma.Conversation.Participation
alias Pleroma.Notification
alias Pleroma.User
alias Pleroma.Web.MastodonAPI.NotificationView
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",
%{last_message: cm_ref, chat: chat}
)
%{
event: "pleroma:chat_update",
payload:
representation
|> Jason.encode!()
}
|> Jason.encode!()
end
def render("update.json", %Activity{} = activity, %User{} = user) do
%{
event: "update",
@ -76,6 +51,27 @@ defmodule Pleroma.Web.StreamerView do
|> Jason.encode!()
end
def render("chat_update.json", %{chat_message_reference: cm_ref}) do
# 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
Logger.debug("Trying to stream out #{inspect(cm_ref)}")
representation =
Pleroma.Web.PleromaAPI.ChatView.render(
"show.json",
%{last_message: cm_ref, chat: cm_ref.chat}
)
%{
event: "pleroma:chat_update",
payload:
representation
|> Jason.encode!()
}
|> Jason.encode!()
end
def render("conversation.json", %Participation{} = participation) do
%{
event: "conversation",