1 defmodule Pleroma.Web.Streamer do
4 alias Pleroma.{User, Notification, Activity, Object}
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: "list", item: item}, topics) do
64 Pleroma.List.get_lists_from_activity(item)
65 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
67 Enum.each(recipient_topics || [], fn list_topic ->
68 Logger.debug("Trying to push message to #{list_topic}\n\n")
69 push_to_socket(topics, list_topic, item)
75 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
76 topic = "user:#{item.user_id}"
78 Enum.each(topics[topic] || [], fn socket ->
81 event: "notification",
83 Pleroma.Web.MastodonAPI.MastodonAPIController.render_notification(
84 socket.assigns["user"],
91 send(socket.transport_pid, {:text, json})
97 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
98 Logger.debug("Trying to push to users")
101 User.get_recipients_from_activity(item)
102 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
104 Enum.each(recipient_topics, fn topic ->
105 push_to_socket(topics, topic, item)
111 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
112 Logger.debug("Trying to push to #{topic}")
113 Logger.debug("Pushing item to #{topic}")
114 push_to_socket(topics, topic, item)
118 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
119 topic = internal_topic(topic, socket)
120 sockets_for_topic = sockets[topic] || []
121 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
122 sockets = Map.put(sockets, topic, sockets_for_topic)
123 Logger.debug("Got new conn for #{topic}")
127 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
128 topic = internal_topic(topic, socket)
129 sockets_for_topic = sockets[topic] || []
130 sockets_for_topic = List.delete(sockets_for_topic, socket)
131 sockets = Map.put(sockets, topic, sockets_for_topic)
132 Logger.debug("Removed conn for #{topic}")
136 def handle_cast(m, state) do
137 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
141 defp represent_update(%Activity{} = activity, %User{} = user) do
145 Pleroma.Web.MastodonAPI.StatusView.render(
155 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
156 Enum.each(topics[topic] || [], fn socket ->
157 # Get the current user so we have up-to-date blocks etc.
158 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
159 blocks = user.info["blocks"] || []
161 parent = Object.get_by_ap_id(item.data["object"])
163 unless is_nil(parent) or item.actor in blocks or parent.data["actor"] in blocks do
164 send(socket.transport_pid, {:text, represent_update(item, user)})
169 def push_to_socket(topics, topic, item) do
170 Enum.each(topics[topic] || [], fn socket ->
171 # Get the current user so we have up-to-date blocks etc.
172 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
173 blocks = user.info["blocks"] || []
175 unless item.actor in blocks do
176 send(socket.transport_pid, {:text, represent_update(item, user)})
181 defp internal_topic(topic, socket) when topic in ~w[user direct] do
182 "#{topic}:#{socket.assigns[:user].id}"
185 defp internal_topic(topic, _), do: topic