Stream function to handle direct messages.
authorcsaurus <csaurus@mailbox.org>
Fri, 11 May 2018 02:15:42 +0000 (22:15 -0400)
committercsaurus <csaurus@mailbox.org>
Sat, 12 May 2018 21:40:59 +0000 (17:40 -0400)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/streamer.ex

index 38e3a84fbccbe39de47d2f238940bfff5d7b55c5..8c1ba1ea39977099e4929157b17e52086e7e995d 100644 (file)
@@ -42,18 +42,16 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   def stream_out(activity) do
     if activity.data["type"] in ["Create", "Announce"] do
       Pleroma.Web.Streamer.stream("user", activity)
-      direct? = activity.data["object"]["visibility"] == "direct"
 
-      cond do
-        direct? ->
-          Pleroma.Web.Streamer.stream("direct", activity)
+      visibility = Pleroma.Web.MastodonAPI.StatusView.get_visibility(activity.data["object"])
 
-        Enum.member?(activity.data["to"], "https://www.w3.org/ns/activitystreams#Public") ->
+      case visibility do
+        "public" ->
           Pleroma.Web.Streamer.stream("public", activity)
+          if activity.local, do: Pleroma.Web.Streamer.stream("public:local", activity)
 
-          if activity.local do
-            Pleroma.Web.Streamer.stream("public:local", activity)
-          end
+        "direct" ->
+          Pleroma.Web.Streamer.stream("direct", activity)
       end
     end
   end
index 33041ec124a8079c40d37cdb7e0d5845b0e0a435..6aac472dcdefbc6486d0d0151a477b1eb1b146b7 100644 (file)
@@ -46,6 +46,19 @@ defmodule Pleroma.Web.Streamer do
     {:noreply, topics}
   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
     topic = "user:#{item.user_id}"
 
@@ -137,8 +150,8 @@ defmodule Pleroma.Web.Streamer do
     end)
   end
 
-  defp internal_topic("user", socket) do
-    "user:#{socket.assigns[:user].id}"
+  defp internal_topic(topic, socket) when topic in ~w[user, direct] do
+    "#{topic}:#{socket.assigns[:user].id}"
   end
 
   defp internal_topic(topic, _), do: topic