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.Notification
10 alias Pleroma.Activity
13 alias Pleroma.Web.ActivityPub.Visibility
15 @keepalive_interval :timer.seconds(30)
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})
36 Process.sleep(@keepalive_interval)
37 GenServer.cast(__MODULE__, %{action: :ping})
43 def handle_cast(%{action: :ping}, topics) do
46 |> Enum.each(fn socket ->
47 Logger.debug("Sending keepalive ping")
48 send(socket.transport_pid, {:text, ""})
53 Process.sleep(@keepalive_interval)
54 GenServer.cast(__MODULE__, %{action: :ping})
60 def handle_cast(%{action: :stream, topic: "direct", item: item}, topics) do
62 User.get_recipients_from_activity(item)
63 |> Enum.map(fn %{id: id} -> "direct:#{id}" end)
65 Enum.each(recipient_topics || [], fn user_topic ->
66 Logger.debug("Trying to push direct message to #{user_topic}\n\n")
67 push_to_socket(topics, user_topic, item)
73 def handle_cast(%{action: :stream, topic: "list", item: item}, topics) do
74 # filter the recipient list if the activity is not public, see #270.
76 case Visibility.is_public?(item) do
78 Pleroma.List.get_lists_from_activity(item)
81 Pleroma.List.get_lists_from_activity(item)
82 |> Enum.filter(fn list ->
83 owner = Repo.get(User, list.user_id)
85 Visibility.visible_for_user?(item, owner)
91 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
93 Enum.each(recipient_topics || [], fn list_topic ->
94 Logger.debug("Trying to push message to #{list_topic}\n\n")
95 push_to_socket(topics, list_topic, item)
101 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
102 topic = "user:#{item.user_id}"
104 Enum.each(topics[topic] || [], fn socket ->
107 event: "notification",
109 Pleroma.Web.MastodonAPI.MastodonAPIController.render_notification(
110 socket.assigns["user"],
117 send(socket.transport_pid, {:text, json})
123 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
124 Logger.debug("Trying to push to users")
127 User.get_recipients_from_activity(item)
128 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
130 Enum.each(recipient_topics, fn topic ->
131 push_to_socket(topics, topic, item)
137 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
138 Logger.debug("Trying to push to #{topic}")
139 Logger.debug("Pushing item to #{topic}")
140 push_to_socket(topics, topic, item)
144 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
145 topic = internal_topic(topic, socket)
146 sockets_for_topic = sockets[topic] || []
147 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
148 sockets = Map.put(sockets, topic, sockets_for_topic)
149 Logger.debug("Got new conn for #{topic}")
153 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
154 topic = internal_topic(topic, socket)
155 sockets_for_topic = sockets[topic] || []
156 sockets_for_topic = List.delete(sockets_for_topic, socket)
157 sockets = Map.put(sockets, topic, sockets_for_topic)
158 Logger.debug("Removed conn for #{topic}")
162 def handle_cast(m, state) do
163 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
167 defp represent_update(%Activity{} = activity, %User{} = user) do
171 Pleroma.Web.MastodonAPI.StatusView.render(
181 defp represent_update(%Activity{} = activity) do
185 Pleroma.Web.MastodonAPI.StatusView.render(
194 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
195 Enum.each(topics[topic] || [], fn socket ->
196 # Get the current user so we have up-to-date blocks etc.
197 if socket.assigns[:user] do
198 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
199 blocks = user.info.blocks || []
201 parent = Object.normalize(item.data["object"])
203 unless is_nil(parent) or item.actor in blocks or parent.data["actor"] in blocks do
204 send(socket.transport_pid, {:text, represent_update(item, user)})
207 send(socket.transport_pid, {:text, represent_update(item)})
212 def push_to_socket(topics, topic, %Activity{id: id, data: %{"type" => "Delete"}}) do
213 Enum.each(topics[topic] || [], fn socket ->
215 socket.transport_pid,
216 {:text, %{event: "delete", payload: to_string(id)} |> Jason.encode!()}
221 def push_to_socket(topics, topic, item) do
222 Enum.each(topics[topic] || [], fn socket ->
223 # Get the current user so we have up-to-date blocks etc.
224 if socket.assigns[:user] do
225 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
226 blocks = user.info.blocks || []
228 unless item.actor in blocks do
229 send(socket.transport_pid, {:text, represent_update(item, user)})
232 send(socket.transport_pid, {:text, represent_update(item)})
237 defp internal_topic(topic, socket) when topic in ~w[user direct] do
238 "#{topic}:#{socket.assigns[:user].id}"
241 defp internal_topic(topic, _), do: topic