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)
76 author.follower_address in owner.following
82 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
84 Enum.each(recipient_topics || [], fn list_topic ->
85 Logger.debug("Trying to push message to #{list_topic}\n\n")
86 push_to_socket(topics, list_topic, item)
92 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
93 topic = "user:#{item.user_id}"
95 Enum.each(topics[topic] || [], fn socket ->
98 event: "notification",
100 Pleroma.Web.MastodonAPI.MastodonAPIController.render_notification(
101 socket.assigns["user"],
108 send(socket.transport_pid, {:text, json})
114 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
115 Logger.debug("Trying to push to users")
118 User.get_recipients_from_activity(item)
119 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
121 Enum.each(recipient_topics, fn topic ->
122 push_to_socket(topics, topic, item)
128 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
129 Logger.debug("Trying to push to #{topic}")
130 Logger.debug("Pushing item to #{topic}")
131 push_to_socket(topics, topic, item)
135 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
136 topic = internal_topic(topic, socket)
137 sockets_for_topic = sockets[topic] || []
138 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
139 sockets = Map.put(sockets, topic, sockets_for_topic)
140 Logger.debug("Got new conn for #{topic}")
144 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
145 topic = internal_topic(topic, socket)
146 sockets_for_topic = sockets[topic] || []
147 sockets_for_topic = List.delete(sockets_for_topic, socket)
148 sockets = Map.put(sockets, topic, sockets_for_topic)
149 Logger.debug("Removed conn for #{topic}")
153 def handle_cast(m, state) do
154 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
158 defp represent_update(%Activity{} = activity, %User{} = user) do
162 Pleroma.Web.MastodonAPI.StatusView.render(
172 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
173 Enum.each(topics[topic] || [], fn socket ->
174 # Get the current user so we have up-to-date blocks etc.
175 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
176 blocks = user.info["blocks"] || []
178 parent = Object.normalize(item.data["object"])
180 unless is_nil(parent) or item.actor in blocks or parent.data["actor"] in blocks do
181 send(socket.transport_pid, {:text, represent_update(item, user)})
186 def push_to_socket(topics, topic, item) do
187 Enum.each(topics[topic] || [], fn socket ->
188 # Get the current user so we have up-to-date blocks etc.
189 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
190 blocks = user.info["blocks"] || []
192 unless item.actor in blocks do
193 send(socket.transport_pid, {:text, represent_update(item, user)})
198 defp internal_topic(topic, socket) when topic in ~w[user direct] do
199 "#{topic}:#{socket.assigns[:user].id}"
202 defp internal_topic(topic, _), do: topic