Add ActivityPub.contain_activity checks to streamer
[akkoma] / lib / pleroma / web / streamer.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Streamer do
6 use GenServer
7 require Logger
8 alias Pleroma.Activity
9 alias Pleroma.Notification
10 alias Pleroma.Object
11 alias Pleroma.Repo
12 alias Pleroma.User
13 alias Pleroma.Web.ActivityPub.Visibility
14 alias Pleroma.Web.MastodonAPI.NotificationView
15 alias Pleroma.Web.ActivityPub.ActivityPub
16
17 @keepalive_interval :timer.seconds(30)
18
19 def start_link do
20 GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
21 end
22
23 def add_socket(topic, socket) do
24 GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic})
25 end
26
27 def remove_socket(topic, socket) do
28 GenServer.cast(__MODULE__, %{action: :remove, socket: socket, topic: topic})
29 end
30
31 def stream(topic, item) do
32 GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item})
33 end
34
35 def init(args) do
36 spawn(fn ->
37 # 30 seconds
38 Process.sleep(@keepalive_interval)
39 GenServer.cast(__MODULE__, %{action: :ping})
40 end)
41
42 {:ok, args}
43 end
44
45 def handle_cast(%{action: :ping}, topics) do
46 Map.values(topics)
47 |> List.flatten()
48 |> Enum.each(fn socket ->
49 Logger.debug("Sending keepalive ping")
50 send(socket.transport_pid, {:text, ""})
51 end)
52
53 spawn(fn ->
54 # 30 seconds
55 Process.sleep(@keepalive_interval)
56 GenServer.cast(__MODULE__, %{action: :ping})
57 end)
58
59 {:noreply, topics}
60 end
61
62 def handle_cast(%{action: :stream, topic: "direct", item: item}, topics) do
63 recipient_topics =
64 User.get_recipients_from_activity(item)
65 |> Enum.map(fn %{id: id} -> "direct:#{id}" end)
66
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)
70 end)
71
72 {:noreply, topics}
73 end
74
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.
77 recipient_lists =
78 case Visibility.is_public?(item) do
79 true ->
80 Pleroma.List.get_lists_from_activity(item)
81
82 _ ->
83 Pleroma.List.get_lists_from_activity(item)
84 |> Enum.filter(fn list ->
85 owner = Repo.get(User, list.user_id)
86
87 Visibility.visible_for_user?(item, owner)
88 end)
89 end
90
91 recipient_topics =
92 recipient_lists
93 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
94
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)
98 end)
99
100 {:noreply, topics}
101 end
102
103 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
104 topic = "user:#{item.user_id}"
105
106 Enum.each(topics[topic] || [], fn socket ->
107 json =
108 %{
109 event: "notification",
110 payload:
111 NotificationView.render("show.json", %{
112 notification: item,
113 for: socket.assigns["user"]
114 })
115 |> Jason.encode!()
116 }
117 |> Jason.encode!()
118
119 send(socket.transport_pid, {:text, json})
120 end)
121
122 {:noreply, topics}
123 end
124
125 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
126 Logger.debug("Trying to push to users")
127
128 recipient_topics =
129 User.get_recipients_from_activity(item)
130 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
131
132 Enum.each(recipient_topics, fn topic ->
133 push_to_socket(topics, topic, item)
134 end)
135
136 {:noreply, topics}
137 end
138
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)
143 {:noreply, topics}
144 end
145
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}")
152 {:noreply, sockets}
153 end
154
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}")
161 {:noreply, sockets}
162 end
163
164 def handle_cast(m, state) do
165 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
166 {:noreply, state}
167 end
168
169 defp represent_update(%Activity{} = activity, %User{} = user) do
170 %{
171 event: "update",
172 payload:
173 Pleroma.Web.MastodonAPI.StatusView.render(
174 "status.json",
175 activity: activity,
176 for: user
177 )
178 |> Jason.encode!()
179 }
180 |> Jason.encode!()
181 end
182
183 defp represent_update(%Activity{} = activity) do
184 %{
185 event: "update",
186 payload:
187 Pleroma.Web.MastodonAPI.StatusView.render(
188 "status.json",
189 activity: activity
190 )
191 |> Jason.encode!()
192 }
193 |> Jason.encode!()
194 end
195
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
204 parent = Object.normalize(item.data["object"])
205
206 unless is_nil(parent) or item.actor in blocks or item.actor in mutes or
207 not ActivityPub.contain_activity(item, user) or
208 parent.data["actor"] in blocks or
209 parent.data["actor"] in mutes do
210 send(socket.transport_pid, {:text, represent_update(item, user)})
211 end
212 else
213 send(socket.transport_pid, {:text, represent_update(item)})
214 end
215 end)
216 end
217
218 def push_to_socket(topics, topic, %Activity{
219 data: %{"type" => "Delete", "deleted_activity_id" => deleted_activity_id}
220 }) do
221 Enum.each(topics[topic] || [], fn socket ->
222 send(
223 socket.transport_pid,
224 {:text, %{event: "delete", payload: to_string(deleted_activity_id)} |> Jason.encode!()}
225 )
226 end)
227 end
228
229 def push_to_socket(_topics, _topic, %Activity{data: %{"type" => "Delete"}}), do: :noop
230
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 || []
238
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)})
242 end
243 else
244 send(socket.transport_pid, {:text, represent_update(item)})
245 end
246 end)
247 end
248
249 defp internal_topic(topic, socket) when topic in ~w[user direct] do
250 "#{topic}:#{socket.assigns[:user].id}"
251 end
252
253 defp internal_topic(topic, _), do: topic
254 end