diff options
| author | Melonai <einebeere@gmail.com> | 2021-05-22 18:18:56 +0200 |
|---|---|---|
| committer | Melonai <einebeere@gmail.com> | 2021-05-22 18:18:56 +0200 |
| commit | e0cabfea7c7b442acd3636e7495958b87e253176 (patch) | |
| tree | 62e15eb61189c1da58265e37b4a8328a75017205 /lib/rook_web | |
| parent | 0bd75cadf7164979cc06f001ecc057f6275a2e3b (diff) | |
| download | rook-e0cabfea7c7b442acd3636e7495958b87e253176.tar.zst rook-e0cabfea7c7b442acd3636e7495958b87e253176.zip | |
Request and Share communication
Diffstat (limited to 'lib/rook_web')
| -rw-r--r-- | lib/rook_web/channels/request_channel.ex | 20 | ||||
| -rw-r--r-- | lib/rook_web/channels/share_channel.ex | 4 | ||||
| -rw-r--r-- | lib/rook_web/channels/user_socket.ex | 1 | ||||
| -rw-r--r-- | lib/rook_web/controllers/app_controller.ex | 4 | ||||
| -rw-r--r-- | lib/rook_web/templates/app/request.html.eex | 8 |
5 files changed, 32 insertions, 5 deletions
diff --git a/lib/rook_web/channels/request_channel.ex b/lib/rook_web/channels/request_channel.ex new file mode 100644 index 0000000..6290ae6 --- /dev/null +++ b/lib/rook_web/channels/request_channel.ex @@ -0,0 +1,20 @@ +defmodule RookWeb.RequestChannel do + use RookWeb, :channel + + def join("request:" <> token, %{"share" => share_token}, socket) do + if Rook.Token.match?(token, socket) do + if Rook.Share.exists?(share_token) do + Rook.Request.start(token, share_token) + {: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 +end diff --git a/lib/rook_web/channels/share_channel.ex b/lib/rook_web/channels/share_channel.ex index 2d99d87..0efb3ba 100644 --- a/lib/rook_web/channels/share_channel.ex +++ b/lib/rook_web/channels/share_channel.ex @@ -21,8 +21,8 @@ defmodule RookWeb.ShareChannel do {:error, %{reason: "No request given to accept."}} end - def handle_out("new_request", msg, socket) do - # TODO: Send ACK + def handle_out("new_request", %{request: request_token} = msg, socket) do + Rook.Request.acknowledge_request(request_token) push(socket, "new_request", msg) {:noreply, socket} end diff --git a/lib/rook_web/channels/user_socket.ex b/lib/rook_web/channels/user_socket.ex index ae82b6b..d6ce9c4 100644 --- a/lib/rook_web/channels/user_socket.ex +++ b/lib/rook_web/channels/user_socket.ex @@ -4,6 +4,7 @@ defmodule RookWeb.UserSocket do ## Channels channel "token", RookWeb.TokenChannel channel "share:*", RookWeb.ShareChannel + channel "request:*", RookWeb.RequestChannel # Socket params are passed from the client and can # be used to verify and authenticate a user. After diff --git a/lib/rook_web/controllers/app_controller.ex b/lib/rook_web/controllers/app_controller.ex index 9e36f3c..f252086 100644 --- a/lib/rook_web/controllers/app_controller.ex +++ b/lib/rook_web/controllers/app_controller.ex @@ -5,7 +5,7 @@ defmodule RookWeb.AppController do render(conn, "share.html") end - def request(conn, _params) do - render(conn, "request.html") + def request(conn, %{"token" => share_token}) do + render(conn, "request.html", share_token: share_token) end end diff --git a/lib/rook_web/templates/app/request.html.eex b/lib/rook_web/templates/app/request.html.eex index b7ff7bd..1e17e45 100644 --- a/lib/rook_web/templates/app/request.html.eex +++ b/lib/rook_web/templates/app/request.html.eex @@ -1 +1,7 @@ -<%= render_app(@conn, "request") %> +<%= if Rook.Share.exists?(@share_token) do %> + <%= render_app(@conn, "request") %> +<% else %> + <p> + The share you tried to request either expired or never existed. + </p> +<% end %> |
