state.ex 1.72 KB
Newer Older
Steven Fuchs's avatar
Steven Fuchs committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
defmodule Pleroma.Web.Streamer.State do
  use GenServer
  require Logger

  alias Pleroma.Web.Streamer.StreamerSocket

  def start_link(_) do
    GenServer.start_link(__MODULE__, %{sockets: %{}}, name: __MODULE__)
  end

  def add_socket(topic, socket) do
    GenServer.call(__MODULE__, {:add, socket, topic})
  end

  def remove_socket(topic, socket) do
    GenServer.call(__MODULE__, {:remove, socket, topic})
  end

  def get_sockets do
    %{sockets: stream_sockets} = GenServer.call(__MODULE__, :get_state)
    stream_sockets
  end

  def init(init_arg) do
    {:ok, init_arg}
  end

  def handle_call(:get_state, _from, state) do
    {:reply, state, state}
  end

  def handle_call({:add, socket, topic}, _from, %{sockets: sockets} = state) do
    internal_topic = internal_topic(topic, socket)
    stream_socket = StreamerSocket.from_socket(socket)

    sockets_for_topic =
      sockets
      |> Map.get(internal_topic, [])
      |> List.insert_at(0, stream_socket)
      |> Enum.uniq()

    state = put_in(state, [:sockets, internal_topic], sockets_for_topic)
    Logger.debug("Got new conn for #{topic}")
    {:reply, state, state}
  end

  def handle_call({:remove, socket, topic}, _from, %{sockets: sockets} = state) do
    internal_topic = internal_topic(topic, socket)
    stream_socket = StreamerSocket.from_socket(socket)

    sockets_for_topic =
      sockets
      |> Map.get(internal_topic, [])
      |> List.delete(stream_socket)

    state = Kernel.put_in(state, [:sockets, internal_topic], sockets_for_topic)
    {:reply, state, state}
  end

  defp internal_topic(topic, socket)
       when topic in ~w[user user:notification direct] do
    "#{topic}:#{socket.assigns[:user].id}"
  end

  defp internal_topic(topic, _) do
    topic
  end
end