Merge branch 'feld-varnish' into 'develop'
[akkoma] / lib / pleroma / web / streamer.ex
1 defmodule Pleroma.Web.Streamer do
2 use GenServer
3 require Logger
4 alias Pleroma.{User, Notification}
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: "user", item: %Notification{} = item}, topics) do
50 topic = "user:#{item.user_id}"
51
52 Enum.each(topics[topic] || [], fn socket ->
53 json =
54 %{
55 event: "notification",
56 payload:
57 Pleroma.Web.MastodonAPI.MastodonAPIController.render_notification(
58 socket.assigns["user"],
59 item
60 )
61 |> Jason.encode!()
62 }
63 |> Jason.encode!()
64
65 send(socket.transport_pid, {:text, json})
66 end)
67
68 {:noreply, topics}
69 end
70
71 def handle_cast(%{action: :stream, topic: "user", item: item}, topics) do
72 Logger.debug("Trying to push to users")
73
74 recipient_topics =
75 User.get_recipients_from_activity(item)
76 |> Enum.map(fn %{id: id} -> "user:#{id}" end)
77
78 Enum.each(recipient_topics, fn topic ->
79 push_to_socket(topics, topic, item)
80 end)
81
82 {:noreply, topics}
83 end
84
85 def handle_cast(%{action: :stream, topic: topic, item: item}, topics) do
86 Logger.debug("Trying to push to #{topic}")
87 Logger.debug("Pushing item to #{topic}")
88 push_to_socket(topics, topic, item)
89 {:noreply, topics}
90 end
91
92 def handle_cast(%{action: :add, topic: topic, socket: socket}, sockets) do
93 topic = internal_topic(topic, socket)
94 sockets_for_topic = sockets[topic] || []
95 sockets_for_topic = Enum.uniq([socket | sockets_for_topic])
96 sockets = Map.put(sockets, topic, sockets_for_topic)
97 Logger.debug("Got new conn for #{topic}")
98 {:noreply, sockets}
99 end
100
101 def handle_cast(%{action: :remove, topic: topic, socket: socket}, sockets) do
102 topic = internal_topic(topic, socket)
103 sockets_for_topic = sockets[topic] || []
104 sockets_for_topic = List.delete(sockets_for_topic, socket)
105 sockets = Map.put(sockets, topic, sockets_for_topic)
106 Logger.debug("Removed conn for #{topic}")
107 {:noreply, sockets}
108 end
109
110 def handle_cast(m, state) do
111 Logger.info("Unknown: #{inspect(m)}, #{inspect(state)}")
112 {:noreply, state}
113 end
114
115 def push_to_socket(topics, topic, item) do
116 Enum.each(topics[topic] || [], fn socket ->
117 # Get the current user so we have up-to-date blocks etc.
118 user = User.get_cached_by_ap_id(socket.assigns[:user].ap_id)
119 blocks = user.info["blocks"] || []
120
121 unless item.actor in blocks do
122 json =
123 %{
124 event: "update",
125 payload:
126 Pleroma.Web.MastodonAPI.StatusView.render(
127 "status.json",
128 activity: item,
129 for: user
130 )
131 |> Jason.encode!()
132 }
133 |> Jason.encode!()
134
135 send(socket.transport_pid, {:text, json})
136 end
137 end)
138 end
139
140 defp internal_topic("user", socket) do
141 "user:#{socket.assigns[:user].id}"
142 end
143
144 defp internal_topic(topic, _), do: topic
145 end