Stream function to handle direct messages.

This commit is contained in:
csaurus 2018-05-10 22:15:42 -04:00
parent c60a5405db
commit 392bd9ef56
2 changed files with 21 additions and 10 deletions

View file

@ -42,18 +42,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
def stream_out(activity) do def stream_out(activity) do
if activity.data["type"] in ["Create", "Announce"] do if activity.data["type"] in ["Create", "Announce"] do
Pleroma.Web.Streamer.stream("user", activity) Pleroma.Web.Streamer.stream("user", activity)
direct? = activity.data["object"]["visibility"] == "direct"
cond do visibility = Pleroma.Web.MastodonAPI.StatusView.get_visibility(activity.data["object"])
direct? ->
Pleroma.Web.Streamer.stream("direct", activity)
Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") -> case visibility do
"public" ->
Pleroma.Web.Streamer.stream("public", activity) Pleroma.Web.Streamer.stream("public", activity)
if activity.local, do: Pleroma.Web.Streamer.stream("public:local", activity)
if activity.local do "direct" ->
Pleroma.Web.Streamer.stream("public:local", activity) Pleroma.Web.Streamer.stream("direct", activity)
end
end end
end end
end end

View file

@ -46,6 +46,19 @@ defmodule Pleroma.Web.Streamer do
{:noreply, topics} {:noreply, topics}
end end
def handle_cast(%{action: :stream, topic: "direct", item: item}, topics) do
recipient_topics =
User.get_recipients_from_activity(item)
|> Enum.map(fn %{id: id} -> "direct:#{id}" end)
Enum.each(recipient_topics || [], fn user_topic ->
Logger.debug("Trying to push direct message to #{user_topic}\n\n")
push_to_socket(topics, user_topic, item)
end)
{:noreply, topics}
end
def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
topic = "user:#{item.user_id}" topic = "user:#{item.user_id}"
@ -137,8 +150,8 @@ defmodule Pleroma.Web.Streamer do
end) end)
end end
defp internal_topic("user", socket) do defp internal_topic(topic, socket) when topic in ~w[user, direct] do
"user:#{socket.assigns[:user].id}" "#{topic}:#{socket.assigns[:user].id}"
end end
defp internal_topic(topic, _), do: topic defp internal_topic(topic, _), do: topic