Skip to content

Commit d6a38a1

Browse files
committed
Add visitors live views
Add cachex depedency Add room reset route Handle client disconnect
1 parent 5299287 commit d6a38a1

File tree

10 files changed

+82
-7
lines changed

10 files changed

+82
-7
lines changed

lib/yail/application.ex

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ defmodule Yail.Application do
1515
YailWeb.Endpoint,
1616
# Start a worker by calling: Yail.Worker.start_link(arg)
1717
# {Yail.Worker, arg}
18+
{Cachex, name: :yail},
19+
Yail.LiveMonitor,
1820
Yail.Session.Session
1921
]
2022

lib/yail/live_monitor.ex

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
defmodule Yail.LiveMonitor do
2+
use GenServer
3+
4+
def start_link(default) do
5+
GenServer.start_link(__MODULE__, default, name: __MODULE__)
6+
end
7+
8+
def monitor(pid, view_module, meta) do
9+
GenServer.call(__MODULE__, {:monitor, pid, view_module, meta})
10+
end
11+
12+
def init(_) do
13+
{:ok, %{views: %{}}}
14+
end
15+
16+
def handle_call({:monitor, pid, view_module, meta}, _, %{views: views} = state) do
17+
Process.monitor(pid)
18+
{:reply, :ok, %{state | views: Map.put(views, pid, {view_module, meta})}}
19+
end
20+
21+
def handle_info({:DOWN, _ref, :process, pid, reason}, state) do
22+
{{module, meta}, new_views} = Map.pop(state.views, pid)
23+
spawn(fn -> module.unmount(reason, meta) end)
24+
{:noreply, %{state | views: new_views}}
25+
end
26+
end

lib/yail/session/room.ex

+11
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,15 @@ defmodule Yail.Session.Room do
2525
refresh_token: refresh_token
2626
}
2727
end
28+
29+
def update(room, conn) do
30+
%{"spotify_access_token" => access_token, "spotify_refresh_token" => refresh_token} =
31+
Plug.Conn.get_session(conn)
32+
33+
%Room{
34+
room
35+
| access_token: access_token,
36+
refresh_token: refresh_token
37+
}
38+
end
2839
end

lib/yail_web/controllers/page_controller.ex

+14
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ defmodule YailWeb.PageController do
66
Session
77
}
88

9+
def reset(conn, _params) do
10+
case get_session(conn, :room_id) do
11+
id -> Session.delete(id)
12+
end
13+
14+
room = Room.new(conn)
15+
Session.put(room.id, room)
16+
17+
conn
18+
|> put_session(:room_id, room.id)
19+
|> redirect(to: "/#{room.id}")
20+
end
21+
922
def new(conn, _params) do
1023
room =
1124
case get_session(conn, :room_id) do
@@ -19,6 +32,7 @@ defmodule YailWeb.PageController do
1932
end
2033
end
2134

35+
room = Room.update(room, conn)
2236
Session.put(room.id, room)
2337

2438
conn

lib/yail_web/live/page_live.ex

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule YailWeb.PageLive do
66
"""
77

88
use YailWeb, :live_view
9+
alias Yail.LiveMonitor
910
alias Yail.Session.Playback
1011
alias Yail.Session.Session
1112
require Logger
@@ -19,6 +20,8 @@ defmodule YailWeb.PageLive do
1920
socket
2021
) do
2122
if connected?(socket) do
23+
Cachex.incr(:yail, "#{room_id}_count", 1, initial: 0)
24+
LiveMonitor.monitor(self(), __MODULE__, %{room_id: room_id})
2225
Process.send_after(self(), :update, 0)
2326
:timer.send_interval(@playback_update_interval, self(), :update)
2427
end
@@ -29,12 +32,18 @@ defmodule YailWeb.PageLive do
2932
is_playing: false,
3033
playback: nil,
3134
query: "",
35+
views: 0,
3236
results: []
3337
}
3438

3539
{:ok, assign(socket, assigns)}
3640
end
3741

42+
def unmount(_reason, %{room_id: room_id}) do
43+
Cachex.decr(:yail, "#{room_id}_count", 1, initial: 1)
44+
:ok
45+
end
46+
3847
@impl true
3948
def handle_event("play", _params, socket) do
4049
socket =
@@ -230,6 +239,10 @@ defmodule YailWeb.PageLive do
230239
})
231240
end
232241

242+
room_id = socket.assigns.room_id
243+
{:ok, views} = Cachex.get(:yail, "#{room_id}_count")
244+
socket = assign(socket, :views, views)
245+
233246
{:noreply, socket}
234247
end
235248

lib/yail_web/live/page_live.html.leex

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
<% end %>
2020
</div>
2121

22-
<p>is_creator: <%= @is_admin %>
22+
<p>is_creator: <%= @is_admin %></p>
23+
<p>viewers: <%= @views %></p>
2324

2425
<div class="py-16 w-full">
2526
<form phx-blur="search" phx-submit="search" class="flex flex-row justify-center gap-2 ">

lib/yail_web/router.ex

+7-6
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,16 @@ defmodule YailWeb.Router do
2323
plug :accepts, ["json"]
2424
end
2525

26+
scope "/", YailWeb do
27+
pipe_through [:browser, :auth_required]
28+
get "/", PageController, :new
29+
get "/reset", PageController, :reset
30+
get "/logout", AuthController, :delete
31+
end
32+
2633
scope "/", YailWeb do
2734
pipe_through :browser
2835

29-
get "/", PageController, :new
3036
get "/login", PageController, :login
3137
get "/not_found", PageController, :not_found
3238

@@ -35,11 +41,6 @@ defmodule YailWeb.Router do
3541
live "/:room_id", PageLive, :index
3642
end
3743

38-
scope "/", YailWeb do
39-
pipe_through [:browser, :auth_required]
40-
get "/logout", AuthController, :delete
41-
end
42-
4344
scope "/auth", YailWeb do
4445
pipe_through :browser
4546

lib/yail_web/templates/layout/root.html.leex

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
</a>
2020
<nav role="navigation">
2121
<%= if assigns[:is_authenticated] do %>
22+
<a href="/reset" class="link">Reset room</a>
2223
<a href="/logout" class="link">Logout</a>
2324
<% else %>
2425
<a href="/login" class="link">Login</a>

mix.exs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ defmodule Yail.MixProject do
3838
{:floki, ">= 0.27.0", only: :test},
3939
{:spotify_ex, "~> 2.2"},
4040
{:envy, "~> 1.1"},
41+
{:cachex, "~> 3.3"},
4142
{:phoenix_html, "~> 2.11"},
4243
{:phoenix_live_reload, "~> 1.2", only: :dev},
4344
{:credo, "~> 1.5", only: [:dev, :test], runtime: false},

mix.lock

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"artificery": {:hex, :artificery, "0.4.3", "0bc4260f988dcb9dda4b23f9fc3c6c8b99a6220a331534fdf5bf2fd0d4333b02", [:mix], [], "hexpm", "12e95333a30e20884e937abdbefa3e7f5e05609c2ba8cf37b33f000b9ffc0504"},
33
"blankable": {:hex, :blankable, "1.0.0", "89ab564a63c55af117e115144e3b3b57eb53ad43ba0f15553357eb283e0ed425", [:mix], [], "hexpm", "7cf11aac0e44f4eedbee0c15c1d37d94c090cb72a8d9fddf9f7aec30f9278899"},
44
"bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"},
5+
"cachex": {:hex, :cachex, "3.3.0", "6f2ebb8f27491fe39121bd207c78badc499214d76c695658b19d6079beeca5c2", [:mix], [{:eternal, "~> 1.2", [hex: :eternal, repo: "hexpm", optional: false]}, {:jumper, "~> 1.0", [hex: :jumper, repo: "hexpm", optional: false]}, {:sleeplocks, "~> 1.1", [hex: :sleeplocks, repo: "hexpm", optional: false]}, {:unsafe, "~> 1.0", [hex: :unsafe, repo: "hexpm", optional: false]}], "hexpm", "d90e5ee1dde14cef33f6b187af4335b88748b72b30c038969176cd4e6ccc31a1"},
56
"certifi": {:hex, :certifi, "2.6.1", "dbab8e5e155a0763eea978c913ca280a6b544bfa115633fa20249c3d396d9493", [:rebar3], [], "hexpm", "524c97b4991b3849dd5c17a631223896272c6b0af446778ba4675a1dff53bb7e"},
67
"cowboy": {:hex, :cowboy, "2.8.0", "f3dc62e35797ecd9ac1b50db74611193c29815401e53bac9a5c0577bd7bc667d", [:rebar3], [{:cowlib, "~> 2.9.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "4643e4fba74ac96d4d152c75803de6fad0b3fa5df354c71afdd6cbeeb15fac8a"},
78
"cowboy_telemetry": {:hex, :cowboy_telemetry, "0.3.1", "ebd1a1d7aff97f27c66654e78ece187abdc646992714164380d8a041eda16754", [:rebar3], [{:cowboy, "~> 2.7", [hex: :cowboy, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "3a6efd3366130eab84ca372cbd4a7d3c3a97bdfcfb4911233b035d117063f0af"},
@@ -10,6 +11,7 @@
1011
"cuid": {:hex, :cuid, "0.1.0", "6f077491a8e8b572c7bca3fa4bcdd83a190cc1b0c7b414eab4800ed426761f4e", [:mix], [], "hexpm", "80cd46bd323e05b706c60008368e631b559307b554c0acc54292ab2c73a3340b"},
1112
"distillery": {:hex, :distillery, "2.1.1", "f9332afc2eec8a1a2b86f22429e068ef35f84a93ea1718265e740d90dd367814", [:mix], [{:artificery, "~> 0.2", [hex: :artificery, repo: "hexpm", optional: false]}], "hexpm", "bbc7008b0161a6f130d8d903b5b3232351fccc9c31a991f8fcbf2a12ace22995"},
1213
"envy": {:hex, :envy, "1.1.1", "0bc9bd654dec24fcdf203f7c5aa1b8f30620f12cfb28c589d5e9c38fe1b07475", [:mix], [], "hexpm", "7061eb1a47415fd757145d8dec10dc0b1e48344960265cb108f194c4252c3a89"},
14+
"eternal": {:hex, :eternal, "1.2.2", "d1641c86368de99375b98d183042dd6c2b234262b8d08dfd72b9eeaafc2a1abd", [:mix], [], "hexpm", "2c9fe32b9c3726703ba5e1d43a1d255a4f3f2d8f8f9bc19f094c7cb1a7a9e782"},
1315
"file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"},
1416
"floki": {:hex, :floki, "0.30.1", "75d35526d3a1459920b6e87fdbc2e0b8a3670f965dd0903708d2b267e0904c55", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "e9c03524447d1c4cbfccd672d739b8c18453eee377846b119d4fd71b1a176bb8"},
1517
"gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"},
@@ -19,6 +21,7 @@
1921
"httpoison": {:hex, :httpoison, "1.0.0", "1f02f827148d945d40b24f0b0a89afe40bfe037171a6cf70f2486976d86921cd", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "07967c56199f716ce9adb27415ccea1bd76c44f777dd0a6d4166c3d932f37fdf"},
2022
"idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"},
2123
"jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"},
24+
"jumper": {:hex, :jumper, "1.0.1", "3c00542ef1a83532b72269fab9f0f0c82bf23a35e27d278bfd9ed0865cecabff", [:mix], [], "hexpm", "318c59078ac220e966d27af3646026db9b5a5e6703cb2aa3e26bcfaba65b7433"},
2225
"metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"},
2326
"mime": {:hex, :mime, "1.6.0", "dabde576a497cef4bbdd60aceee8160e02a6c89250d6c0b29e56c0dfb00db3d2", [:mix], [], "hexpm", "31a1a8613f8321143dde1dafc36006a17d28d02bdfecb9e95a880fa7aabd19a7"},
2427
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
@@ -36,10 +39,12 @@
3639
"poison": {:hex, :poison, "3.1.0", "d9eb636610e096f86f25d9a46f35a9facac35609a7591b3be3326e99a0484665", [:mix], [], "hexpm", "fec8660eb7733ee4117b85f55799fd3833eb769a6df71ccf8903e8dc5447cfce"},
3740
"ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"},
3841
"recase": {:hex, :recase, "0.7.0", "3f2f719f0886c7a3b7fe469058ec539cb7bbe0023604ae3bce920e186305e5ae", [:mix], [], "hexpm", "36f5756a9f552f4a94b54a695870e32f4e72d5fad9c25e61bc4a3151c08a4e0c"},
42+
"sleeplocks": {:hex, :sleeplocks, "1.1.1", "3d462a0639a6ef36cc75d6038b7393ae537ab394641beb59830a1b8271faeed3", [:rebar3], [], "hexpm", "84ee37aeff4d0d92b290fff986d6a95ac5eedf9b383fadfd1d88e9b84a1c02e1"},
3943
"spotify_ex": {:hex, :spotify_ex, "2.2.0", "b62fb7b19db0bdd8b49e171554b6b417efca48bf7c0f4a14419c9992e47ddd9e", [:mix], [{:httpoison, "~> 1.0.0", [hex: :httpoison, repo: "hexpm", optional: false]}, {:plug, ">= 1.4.5", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 3.1", [hex: :poison, repo: "hexpm", optional: false]}], "hexpm", "74dc85864192812b69a0df6f13030a78f0df36f19ec82093418f1036d326aba0"},
4044
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
4145
"telemetry": {:hex, :telemetry, "0.4.3", "a06428a514bdbc63293cd9a6263aad00ddeb66f608163bdec7c8995784080818", [:rebar3], [], "hexpm", "eb72b8365ffda5bed68a620d1da88525e326cb82a75ee61354fc24b844768041"},
4246
"telemetry_metrics": {:hex, :telemetry_metrics, "0.6.0", "da9d49ee7e6bb1c259d36ce6539cd45ae14d81247a2b0c90edf55e2b50507f7b", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "5cfe67ad464b243835512aa44321cee91faed6ea868d7fb761d7016e02915c3d"},
4347
"telemetry_poller": {:hex, :telemetry_poller, "0.5.1", "21071cc2e536810bac5628b935521ff3e28f0303e770951158c73eaaa01e962a", [:rebar3], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4cab72069210bc6e7a080cec9afffad1b33370149ed5d379b81c7c5f0c663fd4"},
4448
"unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"},
49+
"unsafe": {:hex, :unsafe, "1.0.1", "a27e1874f72ee49312e0a9ec2e0b27924214a05e3ddac90e91727bc76f8613d8", [:mix], [], "hexpm", "6c7729a2d214806450d29766abc2afaa7a2cbecf415be64f36a6691afebb50e5"},
4550
}

0 commit comments

Comments
 (0)