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 author = User.get_cached_by_ap_id(item.data["actor"])
66 # filter the recipient list if the activity is not public, see #270.
68 case ActivityPub.is_public?(item) do
70 Pleroma.List.get_lists_from_activity(item)
73 Pleroma.List.get_lists_from_activity(item)
74 |> Enum.filter(fn list ->
75 owner = Repo.get(User, list.user_id)
77 ActivityPub.visible_for_user?(item, owner)
83 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
85 Enum.each(recipient_topics || [], fn list_topic ->
86 Logger.debug("Trying to push message to #{list_topic}\n\n")
87 push_to_socket(topics, list_topic, item)
93 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
94 topic = "user:#{item.user_id}"
96 Enum.each(topics[topic] || [], fn socket ->
99 event: "notification",
101 Pleroma.Web.MastodonAPI.MastodonAPIController.render_notification(
102 socket.assigns["user"],
109 send(socket.transport_pid, {:text, json})
115 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
116 Logger.debug("Trying to push to users")
119 User.get_recipients_from_activity(item)
120 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
122 Enum.each(recipient_topics, fn topic ->
123 push_to_socket(topics, topic, item)
129 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
130 Logger.debug("Trying to push to #{topic}")
131 Logger.debug("Pushing item to #{topic}")
132 push_to_socket(topics, topic, item)
136 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
137 topic = internal_topic(topic, socket)
138 sockets_for_topic = sockets[topic] || []
139 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
140 sockets = Map.put(sockets, topic, sockets_for_topic)
141 Logger.debug("Got new conn for #{topic}")
145 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
146 topic = internal_topic(topic, socket)
147 sockets_for_topic = sockets[topic] || []
148 sockets_for_topic = List.delete(sockets_for_topic, socket)
149 sockets = Map.put(sockets, topic, sockets_for_topic)
150 Logger.debug("Removed conn for #{topic}")
154 def handle_cast(m, state) do
155 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
159 defp represent_update(%Activity{} = activity, %User{} = user) do
163 Pleroma.Web.MastodonAPI.StatusView.render(
173 defp represent_update(%Activity{} = activity) do
177 Pleroma.Web.MastodonAPI.StatusView.render(
186 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
187 Enum.each(topics[topic] || [], fn socket ->
188 # Get the current user so we have up-to-date blocks etc.
189 if socket.assigns[:user] do
190 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
191 blocks = user.info.blocks || []
193 parent = Object.normalize(item.data["object"])
195 unless is_nil(parent) or item.actor in blocks or parent.data["actor"] in blocks do
196 send(socket.transport_pid, {:text, represent_update(item, user)})
199 send(socket.transport_pid, {:text, represent_update(item)})
204 def push_to_socket(topics, topic, item) do
205 Enum.each(topics[topic] || [], fn socket ->
206 # Get the current user so we have up-to-date blocks etc.
207 if socket.assigns[:user] do
208 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
209 blocks = user.info.blocks || []
211 unless item.actor in blocks do
212 send(socket.transport_pid, {:text, represent_update(item, user)})
215 send(socket.transport_pid, {:text, represent_update(item)})
220 defp internal_topic(topic, socket) when topic in ~w[user direct] do
221 "#{topic}:#{socket.assigns[:user].id}"
224 defp internal_topic(topic, _), do: topic