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
9 alias Pleroma.Conversation.Participation
10 alias Pleroma.Notification
13 alias Pleroma.Web.ActivityPub.ActivityPub
14 alias Pleroma.Web.ActivityPub.Visibility
15 alias Pleroma.Web.MastodonAPI.NotificationView
17 @keepalive_interval :timer.seconds(30)
20 GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
23 def add_socket(topic, socket) do
24 GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic})
27 def remove_socket(topic, socket) do
28 GenServer.cast(__MODULE__, %{action: :remove, socket: socket, topic: topic})
31 def stream(topic, item) do
32 GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item})
38 Process.sleep(@keepalive_interval)
39 GenServer.cast(__MODULE__, %{action: :ping})
45 def handle_cast(%{action: :ping}, topics) do
48 |> Enum.each(fn socket ->
49 Logger.debug("Sending keepalive ping")
50 send(socket.transport_pid, {:text, ""})
55 Process.sleep(@keepalive_interval)
56 GenServer.cast(__MODULE__, %{action: :ping})
62 def handle_cast(%{action: :stream, topic: "direct", item: item}, topics) do
64 User.get_recipients_from_activity(item)
65 |> Enum.map(fn %{id: id} -> "direct:#{id}" end)
67 Enum.each(recipient_topics || [], fn user_topic ->
68 Logger.debug("Trying to push direct message to #{user_topic}\n\n")
69 push_to_socket(topics, user_topic, item)
75 def handle_cast(%{action: :stream, topic: "participation", item: participation}, topics) do
76 user_topic = "direct:#{participation.user_id}"
77 Logger.debug("Trying to push a conversation participation to #{user_topic}\n\n")
79 push_to_socket(topics, user_topic, participation)
84 def handle_cast(%{action: :stream, topic: "list", item: item}, topics) do
85 # filter the recipient list if the activity is not public, see #270.
87 case Visibility.is_public?(item) do
89 Pleroma.List.get_lists_from_activity(item)
92 Pleroma.List.get_lists_from_activity(item)
93 |> Enum.filter(fn list ->
94 owner = User.get_cached_by_id(list.user_id)
96 Visibility.visible_for_user?(item, owner)
102 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
104 Enum.each(recipient_topics || [], fn list_topic ->
105 Logger.debug("Trying to push message to #{list_topic}\n\n")
106 push_to_socket(topics, list_topic, item)
112 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
113 topic = "user:#{item.user_id}"
115 Enum.each(topics[topic] || [], fn socket ->
118 event: "notification",
120 NotificationView.render("show.json", %{
122 for: socket.assigns["user"]
128 send(socket.transport_pid, {:text, json})
134 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
135 Logger.debug("Trying to push to users")
138 User.get_recipients_from_activity(item)
139 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
141 Enum.each(recipient_topics, fn topic ->
142 push_to_socket(topics, topic, item)
148 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
149 Logger.debug("Trying to push to #{topic}")
150 Logger.debug("Pushing item to #{topic}")
151 push_to_socket(topics, topic, item)
155 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
156 topic = internal_topic(topic, socket)
157 sockets_for_topic = sockets[topic] || []
158 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
159 sockets = Map.put(sockets, topic, sockets_for_topic)
160 Logger.debug("Got new conn for #{topic}")
164 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
165 topic = internal_topic(topic, socket)
166 sockets_for_topic = sockets[topic] || []
167 sockets_for_topic = List.delete(sockets_for_topic, socket)
168 sockets = Map.put(sockets, topic, sockets_for_topic)
169 Logger.debug("Removed conn for #{topic}")
173 def handle_cast(m, state) do
174 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
178 defp represent_update(%Activity{} = activity, %User{} = user) do
182 Pleroma.Web.MastodonAPI.StatusView.render(
192 defp represent_update(%Activity{} = activity) do
196 Pleroma.Web.MastodonAPI.StatusView.render(
205 def represent_conversation(%Participation{} = participation) do
207 event: "conversation",
209 Pleroma.Web.MastodonAPI.ConversationView.render("participation.json", %{
210 participation: participation,
211 user: participation.user
218 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
219 Enum.each(topics[topic] || [], fn socket ->
220 # Get the current user so we have up-to-date blocks etc.
221 if socket.assigns[:user] do
222 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
223 blocks = user.info.blocks || []
224 mutes = user.info.mutes || []
225 reblog_mutes = user.info.muted_reblogs || []
227 parent = Object.normalize(item)
229 unless is_nil(parent) or item.actor in blocks or item.actor in mutes or
230 item.actor in reblog_mutes or not ActivityPub.contain_activity(item, user) or
231 parent.data["actor"] in blocks or parent.data["actor"] in mutes 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 unless item.actor in blocks or item.actor in mutes or
268 not ActivityPub.contain_activity(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