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.MastodonAPI.NotificationView
18 @keepalive_interval :timer.seconds(30)
21 GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
24 def add_socket(topic, socket) do
25 GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic})
28 def remove_socket(topic, socket) do
29 GenServer.cast(__MODULE__, %{action: :remove, socket: socket, topic: topic})
32 def stream(topic, item) do
33 GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item})
39 Process.sleep(@keepalive_interval)
40 GenServer.cast(__MODULE__, %{action: :ping})
46 def handle_cast(%{action: :ping}, topics) do
49 |> Enum.each(fn socket ->
50 Logger.debug("Sending keepalive ping")
51 send(socket.transport_pid, {:text, ""})
56 Process.sleep(@keepalive_interval)
57 GenServer.cast(__MODULE__, %{action: :ping})
63 def handle_cast(%{action: :stream, topic: "direct", item: item}, topics) do
65 User.get_recipients_from_activity(item)
66 |> Enum.map(fn %{id: id} -> "direct:#{id}" end)
68 Enum.each(recipient_topics || [], fn user_topic ->
69 Logger.debug("Trying to push direct message to #{user_topic}\n\n")
70 push_to_socket(topics, user_topic, item)
76 def handle_cast(%{action: :stream, topic: "participation", item: participation}, topics) do
77 user_topic = "direct:#{participation.user_id}"
78 Logger.debug("Trying to push a conversation participation to #{user_topic}\n\n")
80 push_to_socket(topics, user_topic, participation)
85 def handle_cast(%{action: :stream, topic: "list", item: item}, topics) do
86 # filter the recipient list if the activity is not public, see #270.
88 case Visibility.is_public?(item) do
90 Pleroma.List.get_lists_from_activity(item)
93 Pleroma.List.get_lists_from_activity(item)
94 |> Enum.filter(fn list ->
95 owner = User.get_cached_by_id(list.user_id)
97 Visibility.visible_for_user?(item, owner)
103 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
105 Enum.each(recipient_topics || [], fn list_topic ->
106 Logger.debug("Trying to push message to #{list_topic}\n\n")
107 push_to_socket(topics, list_topic, item)
113 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
114 topic = "user:#{item.user_id}"
116 Enum.each(topics[topic] || [], fn socket ->
119 event: "notification",
121 NotificationView.render("show.json", %{
123 for: socket.assigns["user"]
129 send(socket.transport_pid, {:text, json})
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 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
220 Enum.each(topics[topic] || [], fn socket ->
221 # Get the current user so we have up-to-date blocks etc.
222 if socket.assigns[:user] do
223 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
224 blocks = user.info.blocks || []
225 mutes = user.info.mutes || []
226 reblog_mutes = user.info.muted_reblogs || []
228 with parent when not is_nil(parent) <- Object.normalize(item),
229 true <- Enum.all?([blocks, mutes, reblog_mutes], &(item.actor not in &1)),
230 true <- Enum.all?([blocks, mutes], &(parent.data["actor"] not in &1)),
231 true <- thread_containment(item, user) do
232 send(socket.transport_pid, {:text, represent_update(item, user)})
235 send(socket.transport_pid, {:text, represent_update(item)})
240 def push_to_socket(topics, topic, %Participation{} = participation) do
241 Enum.each(topics[topic] || [], fn socket ->
242 send(socket.transport_pid, {:text, represent_conversation(participation)})
246 def push_to_socket(topics, topic, %Activity{
247 data: %{"type" => "Delete", "deleted_activity_id" => deleted_activity_id}
249 Enum.each(topics[topic] || [], fn socket ->
251 socket.transport_pid,
252 {:text, %{event: "delete", payload: to_string(deleted_activity_id)} |> Jason.encode!()}
257 def push_to_socket(_topics, _topic, %Activity{data: %{"type" => "Delete"}}), do: :noop
259 def push_to_socket(topics, topic, item) do
260 Enum.each(topics[topic] || [], fn socket ->
261 # Get the current user so we have up-to-date blocks etc.
262 if socket.assigns[:user] do
263 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
264 blocks = user.info.blocks || []
265 mutes = user.info.mutes || []
267 with true <- Enum.all?([blocks, mutes], &(item.actor not in &1)),
268 true <- thread_containment(item, user) do
269 send(socket.transport_pid, {:text, represent_update(item, user)})
272 send(socket.transport_pid, {:text, represent_update(item)})
277 defp internal_topic(topic, socket) when topic in ~w[user direct] do
278 "#{topic}:#{socket.assigns[:user].id}"
281 defp internal_topic(topic, _), do: topic
283 @spec thread_containment(Activity.t(), User.t()) :: boolean()
284 defp thread_containment(_activity, %User{info: %{skip_thread_containment: true}}), do: true
286 defp thread_containment(activity, user) do
287 if Config.get([:instance, :skip_thread_containment]) do
290 ActivityPub.contain_activity(activity, user)