Merge remote-tracking branch 'upstream/develop' into feature/filter_exif
[akkoma] / lib / pleroma / web / streamer.ex
1 defmodule Pleroma.Web.Streamer do
2 use GenServer
3 require Logger
4 alias Pleroma.{User, Notification, Activity, Object}
5
6 def init(args) do
7 {:ok, args}
8 end
9
10 def start_link do
11 spawn(fn ->
12 # 30 seconds
13 Process.sleep(1000 * 30)
14 GenServer.cast(__MODULE__, %{action: :ping})
15 end)
16
17 GenServer.start_link(__MODULE__, %{}, name: __MODULE__)
18 end
19
20 def add_socket(topic, socket) do
21 GenServer.cast(__MODULE__, %{action: :add, socket: socket, topic: topic})
22 end
23
24 def remove_socket(topic, socket) do
25 GenServer.cast(__MODULE__, %{action: :remove, socket: socket, topic: topic})
26 end
27
28 def stream(topic, item) do
29 GenServer.cast(__MODULE__, %{action: :stream, topic: topic, item: item})
30 end
31
32 def handle_cast(%{action: :ping}, topics) do
33 Map.values(topics)
34 |> List.flatten()
35 |> Enum.each(fn socket ->
36 Logger.debug("Sending keepalive ping")
37 send(socket.transport_pid, {:text, ""})
38 end)
39
40 spawn(fn ->
41 # 30 seconds
42 Process.sleep(1000 * 30)
43 GenServer.cast(__MODULE__, %{action: :ping})
44 end)
45
46 {:noreply, topics}
47 end
48
49 def handle_cast(%{action: :stream, topic: "direct", item: item}, topics) do
50 recipient_topics =
51 User.get_recipients_from_activity(item)
52 |> Enum.map(fn %{id: id} -> "direct:#{id}" end)
53
54 Enum.each(recipient_topics || [], fn user_topic ->
55 Logger.debug("Trying to push direct message to #{user_topic}\n\n")
56 push_to_socket(topics, user_topic, item)
57 end)
58
59 {:noreply, topics}
60 end
61
62 def handle_cast(%{action: :stream, topic: "list", item: item}, topics) do
63 recipient_topics =
64 Pleroma.List.get_lists_from_activity(item)
65 |> Enum.map(fn %{id: id} -> "list:#{id}" end)
66
67 Enum.each(recipient_topics || [], fn list_topic ->
68 Logger.debug("Trying to push message to #{list_topic}\n\n")
69 push_to_socket(topics, list_topic, item)
70 end)
71
72 {:noreply, topics}
73 end
74
75 def handle_cast(%{action: :stream, topic: "user", item: %Notification{} = item}, topics) do
76 topic = "user:#{item.user_id}"
77
78 Enum.each(topics[topic] || [], fn socket ->
79 json =
80 %{
81 event: "notification",
82 payload:
83 Pleroma.Web.MastodonAPI.MastodonAPIController.render_notification(
84 socket.assigns["user"],
85 item
86 )
87 |> Jason.encode!()
88 }
89 |> Jason.encode!()
90
91 send(socket.transport_pid, {:text, json})
92 end)
93
94 {:noreply, topics}
95 end
96
97 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
98 Logger.debug("Trying to push to users")
99
100 recipient_topics =
101 User.get_recipients_from_activity(item)
102 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
103
104 Enum.each(recipient_topics, fn topic ->
105 push_to_socket(topics, topic, item)
106 end)
107
108 {:noreply, topics}
109 end
110
111 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
112 Logger.debug("Trying to push to #{topic}")
113 Logger.debug("Pushing item to #{topic}")
114 push_to_socket(topics, topic, item)
115 {:noreply, topics}
116 end
117
118 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
119 topic = internal_topic(topic, socket)
120 sockets_for_topic = sockets[topic] || []
121 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
122 sockets = Map.put(sockets, topic, sockets_for_topic)
123 Logger.debug("Got new conn for #{topic}")
124 {:noreply, sockets}
125 end
126
127 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
128 topic = internal_topic(topic, socket)
129 sockets_for_topic = sockets[topic] || []
130 sockets_for_topic = List.delete(sockets_for_topic, socket)
131 sockets = Map.put(sockets, topic, sockets_for_topic)
132 Logger.debug("Removed conn for #{topic}")
133 {:noreply, sockets}
134 end
135
136 def handle_cast(m, state) do
137 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
138 {:noreply, state}
139 end
140
141 defp represent_update(%Activity{} = activity, %User{} = user) do
142 %{
143 event: "update",
144 payload:
145 Pleroma.Web.MastodonAPI.StatusView.render(
146 "status.json",
147 activity: activity,
148 for: user
149 )
150 |> Jason.encode!()
151 }
152 |> Jason.encode!()
153 end
154
155 def push_to_socket(topics, topic, %Activity{data: %{"type" => "Announce"}} = item) do
156 Enum.each(topics[topic] || [], fn socket ->
157 # Get the current user so we have up-to-date blocks etc.
158 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
159 blocks = user.info["blocks"] || []
160
161 parent = Object.normalize(item.data["object"])
162
163 unless is_nil(parent) or item.actor in blocks or parent.data["actor"] in blocks do
164 send(socket.transport_pid, {:text, represent_update(item, user)})
165 end
166 end)
167 end
168
169 def push_to_socket(topics, topic, item) do
170 Enum.each(topics[topic] || [], fn socket ->
171 # Get the current user so we have up-to-date blocks etc.
172 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
173 blocks = user.info["blocks"] || []
174
175 unless item.actor in blocks do
176 send(socket.transport_pid, {:text, represent_update(item, user)})
177 end
178 end)
179 end
180
181 defp internal_topic(topic, socket) when topic in ~w[user direct] do
182 "#{topic}:#{socket.assigns[:user].id}"
183 end
184
185 defp internal_topic(topic, _), do: topic
186 end