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
8 alias Pleroma.{User, Notification, Activity, Object, Repo}
9 alias Pleroma.Web.ActivityPub.ActivityPub
11 @keepalive_interval :timer.seconds(30)
14 GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
17 def add_socket(topic, socket) do
18 GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic})
21 def remove_socket(topic, socket) do
22 GenServer.cast(__MODULE__, %{action: :remove, socket: socket, topic: topic})
25 def stream(topic, item) do
26 GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item})
32 Process.sleep(@keepalive_interval)
33 GenServer.cast(__MODULE__, %{action: :ping})
39 def handle_cast(%{action: :ping}, topics) do
42 |> Enum.each(fn socket ->
43 Logger.debug("Sending keepalive ping")
44 send(socket.transport_pid, {:text, ""})
49 Process.sleep(@keepalive_interval)
50 GenServer.cast(__MODULE__, %{action: :ping})
56 def handle_cast(%{action: :stream, topic: "direct", item: item}, topics) do
58 User.get_recipients_from_activity(item)
59 |> Enum.map(fn %{id: id} -> "direct:#{id}" end)
61 Enum.each(recipient_topics || [], fn user_topic ->
62 Logger.debug("Trying to push direct message to #{user_topic}\n\n")
63 push_to_socket(topics, user_topic, item)
69 def handle_cast(%{action: :stream, topic: "list", item: item}, topics) do
70 # filter the recipient list if the activity is not public, see #270.
72 case ActivityPub.is_public?(item) do
74 Pleroma.List.get_lists_from_activity(item)
77 Pleroma.List.get_lists_from_activity(item)
78 |> Enum.filter(fn list ->
79 owner = Repo.get(User, list.user_id)
81 ActivityPub.visible_for_user?(item, owner)
87 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
89 Enum.each(recipient_topics || [], fn list_topic ->
90 Logger.debug("Trying to push message to #{list_topic}\n\n")
91 push_to_socket(topics, list_topic, item)
97 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
98 topic = "user:#{item.user_id}"
100 Enum.each(topics[topic] || [], fn socket ->
103 event: "notification",
105 Pleroma.Web.MastodonAPI.MastodonAPIController.render_notification(
106 socket.assigns["user"],
113 send(socket.transport_pid, {:text, json})
119 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
120 Logger.debug("Trying to push to users")
123 User.get_recipients_from_activity(item)
124 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
126 Enum.each(recipient_topics, fn topic ->
127 push_to_socket(topics, topic, item)
133 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
134 Logger.debug("Trying to push to #{topic}")
135 Logger.debug("Pushing item to #{topic}")
136 push_to_socket(topics, topic, item)
140 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
141 topic = internal_topic(topic, socket)
142 sockets_for_topic = sockets[topic] || []
143 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
144 sockets = Map.put(sockets, topic, sockets_for_topic)
145 Logger.debug("Got new conn for #{topic}")
149 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
150 topic = internal_topic(topic, socket)
151 sockets_for_topic = sockets[topic] || []
152 sockets_for_topic = List.delete(sockets_for_topic, socket)
153 sockets = Map.put(sockets, topic, sockets_for_topic)
154 Logger.debug("Removed conn for #{topic}")
158 def handle_cast(m, state) do
159 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
163 defp represent_update(%Activity{} = activity, %User{} = user) do
167 Pleroma.Web.MastodonAPI.StatusView.render(
177 defp represent_update(%Activity{} = activity) do
181 Pleroma.Web.MastodonAPI.StatusView.render(
190 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
191 Enum.each(topics[topic] || [], fn socket ->
192 # Get the current user so we have up-to-date blocks etc.
193 if socket.assigns[:user] do
194 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
195 blocks = user.info.blocks || []
197 parent = Object.normalize(item.data["object"])
199 unless is_nil(parent) or item.actor in blocks or parent.data["actor"] in blocks do
200 send(socket.transport_pid, {:text, represent_update(item, user)})
203 send(socket.transport_pid, {:text, represent_update(item)})
208 def push_to_socket(topics, topic, item) do
209 Enum.each(topics[topic] || [], fn socket ->
210 # Get the current user so we have up-to-date blocks etc.
211 if socket.assigns[:user] do
212 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
213 blocks = user.info.blocks || []
215 unless item.actor in blocks do
216 send(socket.transport_pid, {:text, represent_update(item, user)})
219 send(socket.transport_pid, {:text, represent_update(item)})
224 defp internal_topic(topic, socket) when topic in ~w[user direct] do
225 "#{topic}:#{socket.assigns[:user].id}"
228 defp internal_topic(topic, _), do: topic