1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Streamer do
10 alias Pleroma.Conversation.Participation
11 alias Pleroma.Notification
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Visibility
16 alias Pleroma.Web.CommonAPI
17 alias Pleroma.Web.MastodonAPI.NotificationView
19 @keepalive_interval :timer.seconds(30)
22 GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
25 def add_socket(topic, socket) do
26 GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic})
29 def remove_socket(topic, socket) do
30 GenServer.cast(__MODULE__, %{action: :remove, socket: socket, topic: topic})
33 def stream(topic, item) do
34 GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item})
40 Process.sleep(@keepalive_interval)
41 GenServer.cast(__MODULE__, %{action: :ping})
47 def handle_cast(%{action: :ping}, topics) do
50 |> Enum.each(fn socket ->
51 Logger.debug("Sending keepalive ping")
52 send(socket.transport_pid, {:text, ""})
57 Process.sleep(@keepalive_interval)
58 GenServer.cast(__MODULE__, %{action: :ping})
64 def handle_cast(%{action: :stream, topic: "direct", item: item}, topics) do
66 User.get_recipients_from_activity(item)
67 |> Enum.map(fn %{id: id} -> "direct:#{id}" end)
69 Enum.each(recipient_topics || [], fn user_topic ->
70 Logger.debug("Trying to push direct message to #{user_topic}\n\n")
71 push_to_socket(topics, user_topic, item)
77 def handle_cast(%{action: :stream, topic: "participation", item: participation}, topics) do
78 user_topic = "direct:#{participation.user_id}"
79 Logger.debug("Trying to push a conversation participation to #{user_topic}\n\n")
81 push_to_socket(topics, user_topic, participation)
86 def handle_cast(%{action: :stream, topic: "list", item: item}, topics) do
87 # filter the recipient list if the activity is not public, see #270.
89 case Visibility.is_public?(item) do
91 Pleroma.List.get_lists_from_activity(item)
94 Pleroma.List.get_lists_from_activity(item)
95 |> Enum.filter(fn list ->
96 owner = User.get_cached_by_id(list.user_id)
98 Visibility.visible_for_user?(item, owner)
104 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
106 Enum.each(recipient_topics || [], fn list_topic ->
107 Logger.debug("Trying to push message to #{list_topic}\n\n")
108 push_to_socket(topics, list_topic, item)
115 %{action: :stream, topic: topic, item: %Notification{} = item},
118 when topic in ["user", "user:notification"] do
120 |> Map.get("#{topic}:#{item.user_id}", [])
121 |> Enum.each(fn socket ->
122 with %User{} = user <- User.get_cached_by_ap_id(socket.assigns[:user].ap_id),
123 true <- should_send?(user, item),
124 false <- CommonAPI.thread_muted?(user, item.activity) do
126 socket.transport_pid,
127 {:text, represent_notification(socket.assigns[:user], item)}
135 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
136 Logger.debug("Trying to push to users")
139 User.get_recipients_from_activity(item)
140 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
142 Enum.each(recipient_topics, fn topic ->
143 push_to_socket(topics, topic, item)
149 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
150 Logger.debug("Trying to push to #{topic}")
151 Logger.debug("Pushing item to #{topic}")
152 push_to_socket(topics, topic, item)
156 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
157 topic = internal_topic(topic, socket)
158 sockets_for_topic = sockets[topic] || []
159 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
160 sockets = Map.put(sockets, topic, sockets_for_topic)
161 Logger.debug("Got new conn for #{topic}")
165 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
166 topic = internal_topic(topic, socket)
167 sockets_for_topic = sockets[topic] || []
168 sockets_for_topic = List.delete(sockets_for_topic, socket)
169 sockets = Map.put(sockets, topic, sockets_for_topic)
170 Logger.debug("Removed conn for #{topic}")
174 def handle_cast(m, state) do
175 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
179 defp represent_update(%Activity{} = activity, %User{} = user) do
183 Pleroma.Web.MastodonAPI.StatusView.render(
193 defp represent_update(%Activity{} = activity) do
197 Pleroma.Web.MastodonAPI.StatusView.render(
206 def represent_conversation(%Participation{} = participation) do
208 event: "conversation",
210 Pleroma.Web.MastodonAPI.ConversationView.render("participation.json", %{
211 participation: participation,
212 user: participation.user
219 @spec represent_notification(User.t(), Notification.t()) :: binary()
220 defp represent_notification(%User{} = user, %Notification{} = notify) do
222 event: "notification",
224 NotificationView.render(
226 %{notification: notify, for: user}
233 defp should_send?(%User{} = user, %Activity{} = item) do
234 blocks = user.info.blocks || []
235 mutes = user.info.mutes || []
236 reblog_mutes = user.info.muted_reblogs || []
238 with parent when not is_nil(parent) <- Object.normalize(item),
239 true <- Enum.all?([blocks, mutes, reblog_mutes], &(item.actor not in &1)),
240 true <- Enum.all?([blocks, mutes], &(parent.data["actor"] not in &1)),
241 true <- thread_containment(item, user) do
248 defp should_send?(%User{} = user, %Notification{activity: activity}) do
249 should_send?(user, activity)
252 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
253 Enum.each(topics[topic] || [], fn socket ->
254 # Get the current user so we have up-to-date blocks etc.
255 if socket.assigns[:user] do
256 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
258 if should_send?(user, item) do
259 send(socket.transport_pid, {:text, represent_update(item, user)})
262 send(socket.transport_pid, {:text, represent_update(item)})
267 def push_to_socket(topics, topic, %Participation{} = participation) do
268 Enum.each(topics[topic] || [], fn socket ->
269 send(socket.transport_pid, {:text, represent_conversation(participation)})
273 def push_to_socket(topics, topic, %Activity{
274 data: %{"type" => "Delete", "deleted_activity_id" => deleted_activity_id}
276 Enum.each(topics[topic] || [], fn socket ->
278 socket.transport_pid,
279 {:text, %{event: "delete", payload: to_string(deleted_activity_id)} |> Jason.encode!()}
284 def push_to_socket(_topics, _topic, %Activity{data: %{"type" => "Delete"}}), do: :noop
286 def push_to_socket(topics, topic, item) do
287 Enum.each(topics[topic] || [], fn socket ->
288 # Get the current user so we have up-to-date blocks etc.
289 if socket.assigns[:user] do
290 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
291 blocks = user.info.blocks || []
292 mutes = user.info.mutes || []
294 with true <- Enum.all?([blocks, mutes], &(item.actor not in &1)),
295 true <- thread_containment(item, user) do
296 send(socket.transport_pid, {:text, represent_update(item, user)})
299 send(socket.transport_pid, {:text, represent_update(item)})
304 defp internal_topic(topic, socket) when topic in ~w[user user:notification direct] do
305 "#{topic}:#{socket.assigns[:user].id}"
308 defp internal_topic(topic, _), do: topic
310 @spec thread_containment(Activity.t(), User.t()) :: boolean()
311 defp thread_containment(_activity, %User{info: %{skip_thread_containment: true}}), do: true
313 defp thread_containment(activity, user) do
314 if Config.get([:instance, :skip_thread_containment]) do
317 ActivityPub.contain_activity(activity, user)