about summary refs log tree commit diff
path: root/lib/rook_web/channels/request_channel.ex
blob: 9773d30c0f7322fd7c8f26dd83d21df5ca0ce977 (plain)
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
defmodule RookWeb.RequestChannel do
  use RookWeb, :channel

  def join("request:" <> token, %{"share" => share_token, "user_agent" => user_agent}, socket) do
    if Rook.Token.match?(token, socket) do
      if Rook.Share.exists?(share_token) do
        ip = socket.assigns[:ip]

        info = %{
          ip: ip,
          location: Rook.Identity.get_location_from_ip(ip),
          client: Rook.Identity.get_client_from_user_agent(user_agent)
        }

        Rook.Request.Actions.start(token, share_token, info)
        {:ok, socket}
      else
        {:error, %{reason: "No such share exists."}}
      end
    else
      {:error, %{reason: "Wrong token."}}
    end
  end

  def join("request:" <> _token, _params, _socket) do
    {:error, %{reason: "No share given to request."}}
  end

  def handle_in("accept_share", description, socket) do
    Rook.Request.Actions.accept_share(socket.assigns.token, description)
    {:noreply, socket}
  end

  def handle_in("ice_candidate", %{"candidate" => candidate}, socket) do
    Rook.Request.Actions.ice_candidate(socket.assigns.token, candidate)
    {:noreply, socket}
  end
end