1 defmodule Pleroma.Web.Streamer do
4 alias Pleroma.{User, Notification, Activity, Object, Repo}
5 alias Pleroma.Web.ActivityPub.ActivityPub
14 Process.sleep(1000 * 30)
15 GenServer.cast(__MODULE__, %{action: :ping})
18 GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
21 def add_socket(topic, socket) do
22 GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic})
25 def remove_socket(topic, socket) do
26 GenServer.cast(__MODULE__, %{action: :remove, socket: socket, topic: topic})
29 def stream(topic, item) do
30 GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item})
33 def handle_cast(%{action: :ping}, topics) do
36 |> Enum.each(fn socket ->
37 Logger.debug("Sending keepalive ping")
38 send(socket.transport_pid, {:text, ""})
43 Process.sleep(1000 * 30)
44 GenServer.cast(__MODULE__, %{action: :ping})
50 def handle_cast(%{action: :stream, topic: "direct", item: item}, topics) do
52 User.get_recipients_from_activity(item)
53 |> Enum.map(fn %{id: id} -> "direct:#{id}" end)
55 Enum.each(recipient_topics || [], fn user_topic ->
56 Logger.debug("Trying to push direct message to #{user_topic}\n\n")
57 push_to_socket(topics, user_topic, item)
63 def handle_cast(%{action: :stream, topic: "list", item: item}, topics) do
64 # filter the recipient list if the activity is not public, see #270.
66 case ActivityPub.is_public?(item) do
68 Pleroma.List.get_lists_from_activity(item)
71 Pleroma.List.get_lists_from_activity(item)
72 |> Enum.filter(fn list ->
73 owner = Repo.get(User, list.user_id)
75 ActivityPub.visible_for_user?(item, owner)
81 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
83 Enum.each(recipient_topics || [], fn list_topic ->
84 Logger.debug("Trying to push message to #{list_topic}\n\n")
85 push_to_socket(topics, list_topic, item)
91 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
92 topic = "user:#{item.user_id}"
94 Enum.each(topics[topic] || [], fn socket ->
97 event: "notification",
99 Pleroma.Web.MastodonAPI.MastodonAPIController.render_notification(
100 socket.assigns["user"],
107 send(socket.transport_pid, {:text, json})
113 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
114 Logger.debug("Trying to push to users")
117 User.get_recipients_from_activity(item)
118 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
120 Enum.each(recipient_topics, fn topic ->
121 push_to_socket(topics, topic, item)
127 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
128 Logger.debug("Trying to push to #{topic}")
129 Logger.debug("Pushing item to #{topic}")
130 push_to_socket(topics, topic, item)
134 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
135 topic = internal_topic(topic, socket)
136 sockets_for_topic = sockets[topic] || []
137 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
138 sockets = Map.put(sockets, topic, sockets_for_topic)
139 Logger.debug("Got new conn for #{topic}")
143 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
144 topic = internal_topic(topic, socket)
145 sockets_for_topic = sockets[topic] || []
146 sockets_for_topic = List.delete(sockets_for_topic, socket)
147 sockets = Map.put(sockets, topic, sockets_for_topic)
148 Logger.debug("Removed conn for #{topic}")
152 def handle_cast(m, state) do
153 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
157 defp represent_update(%Activity{} = activity, %User{} = user) do
161 Pleroma.Web.MastodonAPI.StatusView.render(
171 defp represent_update(%Activity{} = activity) do
175 Pleroma.Web.MastodonAPI.StatusView.render(
184 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
185 Enum.each(topics[topic] || [], fn socket ->
186 # Get the current user so we have up-to-date blocks etc.
187 if socket.assigns[:user] do
188 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
189 blocks = user.info.blocks || []
191 parent = Object.normalize(item.data["object"])
193 unless is_nil(parent) or item.actor in blocks or parent.data["actor"] in blocks do
194 send(socket.transport_pid, {:text, represent_update(item, user)})
197 send(socket.transport_pid, {:text, represent_update(item)})
202 def push_to_socket(topics, topic, item) do
203 Enum.each(topics[topic] || [], fn socket ->
204 # Get the current user so we have up-to-date blocks etc.
205 if socket.assigns[:user] do
206 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
207 blocks = user.info.blocks || []
209 unless item.actor in blocks do
210 send(socket.transport_pid, {:text, represent_update(item, user)})
213 send(socket.transport_pid, {:text, represent_update(item)})
218 defp internal_topic(topic, socket) when topic in ~w[user direct] do
219 "#{topic}:#{socket.assigns[:user].id}"
222 defp internal_topic(topic, _), do: topic