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
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: "list", item: item}, topics) do
76 # filter the recipient list if the activity is not public, see #270.
78 case Visibility.is_public?(item) do
80 Pleroma.List.get_lists_from_activity(item)
83 Pleroma.List.get_lists_from_activity(item)
84 |> Enum.filter(fn list ->
85 owner = Repo.get(User, list.user_id)
87 Visibility.visible_for_user?(item, owner)
93 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
95 Enum.each(recipient_topics || [], fn list_topic ->
96 Logger.debug("Trying to push message to #{list_topic}\n\n")
97 push_to_socket(topics, list_topic, item)
103 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
104 topic = "user:#{item.user_id}"
106 Enum.each(topics[topic] || [], fn socket ->
109 event: "notification",
111 NotificationView.render("show.json", %{
113 for: socket.assigns["user"]
119 send(socket.transport_pid, {:text, json})
125 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
126 Logger.debug("Trying to push to users")
129 User.get_recipients_from_activity(item)
130 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
132 Enum.each(recipient_topics, fn topic ->
133 push_to_socket(topics, topic, item)
139 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
140 Logger.debug("Trying to push to #{topic}")
141 Logger.debug("Pushing item to #{topic}")
142 push_to_socket(topics, topic, item)
146 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
147 topic = internal_topic(topic, socket)
148 sockets_for_topic = sockets[topic] || []
149 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
150 sockets = Map.put(sockets, topic, sockets_for_topic)
151 Logger.debug("Got new conn for #{topic}")
155 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
156 topic = internal_topic(topic, socket)
157 sockets_for_topic = sockets[topic] || []
158 sockets_for_topic = List.delete(sockets_for_topic, socket)
159 sockets = Map.put(sockets, topic, sockets_for_topic)
160 Logger.debug("Removed conn for #{topic}")
164 def handle_cast(m, state) do
165 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
169 defp represent_update(%Activity{} = activity, %User{} = user) do
173 Pleroma.Web.MastodonAPI.StatusView.render(
183 defp represent_update(%Activity{} = activity) do
187 Pleroma.Web.MastodonAPI.StatusView.render(
196 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
197 Enum.each(topics[topic] || [], fn socket ->
198 # Get the current user so we have up-to-date blocks etc.
199 if socket.assigns[:user] do
200 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
201 blocks = user.info.blocks || []
202 mutes = user.info.mutes || []
203 reblog_mutes = user.info.muted_reblogs || []
205 parent = Object.normalize(item)
207 unless is_nil(parent) or item.actor in blocks or item.actor in mutes or
208 item.actor in reblog_mutes or not ActivityPub.contain_activity(item, user) or
209 parent.data["actor"] in blocks or parent.data["actor"] in mutes do
210 send(socket.transport_pid, {:text, represent_update(item, user)})
213 send(socket.transport_pid, {:text, represent_update(item)})
218 def push_to_socket(topics, topic, %Activity{
219 data: %{"type" => "Delete", "deleted_activity_id" => deleted_activity_id}
221 Enum.each(topics[topic] || [], fn socket ->
223 socket.transport_pid,
224 {:text, %{event: "delete", payload: to_string(deleted_activity_id)} |> Jason.encode!()}
229 def push_to_socket(_topics, _topic, %Activity{data: %{"type" => "Delete"}}), do: :noop
231 def push_to_socket(topics, topic, item) do
232 Enum.each(topics[topic] || [], fn socket ->
233 # Get the current user so we have up-to-date blocks etc.
234 if socket.assigns[:user] do
235 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
236 blocks = user.info.blocks || []
237 mutes = user.info.mutes || []
239 unless item.actor in blocks or item.actor in mutes or
240 not ActivityPub.contain_activity(item, user) do
241 send(socket.transport_pid, {:text, represent_update(item, user)})
244 send(socket.transport_pid, {:text, represent_update(item)})
249 defp internal_topic(topic, socket) when topic in ~w[user direct] do
250 "#{topic}:#{socket.assigns[:user].id}"
253 defp internal_topic(topic, _), do: topic