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 || []
237 domain_blocks = Pleroma.Web.ActivityPub.MRF.subdomains_regex(user.info.domain_blocks)
239 with parent when not is_nil(parent) <- Object.normalize(item),
240 true <- Enum.all?([blocks, mutes, reblog_mutes], &(item.actor not in &1)),
241 true <- Enum.all?([blocks, mutes], &(parent.data["actor"] not in &1)),
242 %{host: item_host} <- URI.parse(item.actor),
243 %{host: parent_host} <- URI.parse(parent.data["actor"]),
244 false <- Pleroma.Web.ActivityPub.MRF.subdomain_match?(domain_blocks, item_host),
245 false <- Pleroma.Web.ActivityPub.MRF.subdomain_match?(domain_blocks, parent_host),
246 true <- thread_containment(item, user) do
253 defp should_send?(%User{} = user, %Notification{activity: activity}) do
254 should_send?(user, activity)
257 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
258 Enum.each(topics[topic] || [], fn socket ->
259 # Get the current user so we have up-to-date blocks etc.
260 if socket.assigns[:user] do
261 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
263 if should_send?(user, item) do
264 send(socket.transport_pid, {:text, represent_update(item, user)})
267 send(socket.transport_pid, {:text, represent_update(item)})
272 def push_to_socket(topics, topic, %Participation{} = participation) do
273 Enum.each(topics[topic] || [], fn socket ->
274 send(socket.transport_pid, {:text, represent_conversation(participation)})
278 def push_to_socket(topics, topic, %Activity{
279 data: %{"type" => "Delete", "deleted_activity_id" => deleted_activity_id}
281 Enum.each(topics[topic] || [], fn socket ->
283 socket.transport_pid,
284 {:text, %{event: "delete", payload: to_string(deleted_activity_id)} |> Jason.encode!()}
289 def push_to_socket(_topics, _topic, %Activity{data: %{"type" => "Delete"}}), do: :noop
291 def push_to_socket(topics, topic, item) do
292 Enum.each(topics[topic] || [], fn socket ->
293 # Get the current user so we have up-to-date blocks etc.
294 if socket.assigns[:user] do
295 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
296 blocks = user.info.blocks || []
297 mutes = user.info.mutes || []
299 with true <- Enum.all?([blocks, mutes], &(item.actor not in &1)),
300 true <- thread_containment(item, user) do
301 send(socket.transport_pid, {:text, represent_update(item, user)})
304 send(socket.transport_pid, {:text, represent_update(item)})
309 defp internal_topic(topic, socket) when topic in ~w[user user:notification direct] do
310 "#{topic}:#{socket.assigns[:user].id}"
313 defp internal_topic(topic, _), do: topic
315 @spec thread_containment(Activity.t(), User.t()) :: boolean()
316 defp thread_containment(_activity, %User{info: %{skip_thread_containment: true}}), do: true
318 defp thread_containment(activity, user) do
319 if Config.get([:instance, :skip_thread_containment]) do
322 ActivityPub.contain_activity(activity, user)