1 defmodule Pleroma.Web.Streamer do
4 alias Pleroma.{User, Notification}
13 Process.sleep(1000 * 30)
14 GenServer.cast(__MODULE__, %{action: :ping})
17 GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
20 def add_socket(topic, socket) do
21 GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic})
24 def remove_socket(topic, socket) do
25 GenServer.cast(__MODULE__, %{action: :remove, socket: socket, topic: topic})
28 def stream(topic, item) do
29 GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item})
32 def handle_cast(%{action: :ping}, topics) do
35 |> Enum.each(fn socket ->
36 Logger.debug("Sending keepalive ping")
37 send(socket.transport_pid, {:text, ""})
42 Process.sleep(1000 * 30)
43 GenServer.cast(__MODULE__, %{action: :ping})
49 def handle_cast(%{action: :stream, topic: "direct", item: item}, topics) do
51 User.get_recipients_from_activity(item)
52 |> Enum.map(fn %{id: id} -> "direct:#{id}" end)
54 Enum.each(recipient_topics || [], fn user_topic ->
55 Logger.debug("Trying to push direct message to #{user_topic}\n\n")
56 push_to_socket(topics, user_topic, item)
62 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
63 topic = "user:#{item.user_id}"
65 Enum.each(topics[topic] || [], fn socket ->
68 event: "notification",
70 Pleroma.Web.MastodonAPI.MastodonAPIController.render_notification(
71 socket.assigns["user"],
78 send(socket.transport_pid, {:text, json})
84 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
85 Logger.debug("Trying to push to users")
88 User.get_recipients_from_activity(item)
89 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
91 Enum.each(recipient_topics, fn topic ->
92 push_to_socket(topics, topic, item)
98 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
99 Logger.debug("Trying to push to #{topic}")
100 Logger.debug("Pushing item to #{topic}")
101 push_to_socket(topics, topic, item)
105 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
106 topic = internal_topic(topic, socket)
107 sockets_for_topic = sockets[topic] || []
108 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
109 sockets = Map.put(sockets, topic, sockets_for_topic)
110 Logger.debug("Got new conn for #{topic}")
114 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
115 topic = internal_topic(topic, socket)
116 sockets_for_topic = sockets[topic] || []
117 sockets_for_topic = List.delete(sockets_for_topic, socket)
118 sockets = Map.put(sockets, topic, sockets_for_topic)
119 Logger.debug("Removed conn for #{topic}")
123 def handle_cast(m, state) do
124 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
128 def push_to_socket(topics, topic, item) do
129 Enum.each(topics[topic] || [], fn socket ->
130 # Get the current user so we have up-to-date blocks etc.
131 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
132 blocks = user.info["blocks"] || []
134 unless item.actor in blocks do
139 Pleroma.Web.MastodonAPI.StatusView.render(
148 send(socket.transport_pid, {:text, json})
153 defp internal_topic(topic, socket) when topic in ~w[user direct] do
154 "#{topic}:#{socket.assigns[:user].id}"
157 defp internal_topic(topic, _), do: topic