diff --git a/lib/chesstrainer/accounts.ex b/lib/chesstrainer/accounts.ex index 9119bba..d46d150 100644 --- a/lib/chesstrainer/accounts.ex +++ b/lib/chesstrainer/accounts.ex @@ -332,6 +332,22 @@ defmodule Chesstrainer.Accounts do Repo.one(query) end + @doc """ + Returns the number of users with at least one valid session token. + + Session tokens stay valid for `UserToken.session_validity_in_days/0` days, so + this counts users who logged in during that window, not necessarily users who + are connected right now. A user with multiple sessions counts once. + """ + def count_logged_in_users do + days = UserToken.session_validity_in_days() + + UserToken + |> where([t], t.context == "session" and t.inserted_at > ago(^days, "day")) + |> select([t], count(t.user_id, :distinct)) + |> Repo.one() + end + @doc ~S""" Delivers the update email instructions to the given user. diff --git a/lib/chesstrainer/accounts/user_token.ex b/lib/chesstrainer/accounts/user_token.ex index ae3b103..c6de3ea 100644 --- a/lib/chesstrainer/accounts/user_token.ex +++ b/lib/chesstrainer/accounts/user_token.ex @@ -48,6 +48,9 @@ defmodule Chesstrainer.Accounts.UserToken do {token, %UserToken{token: token, context: "session", user_id: user.id, authenticated_at: dt}} end + @doc "How long a session token stays valid, in days." + def session_validity_in_days, do: @session_validity_in_days + @doc """ Checks if the token is valid and returns its underlying lookup query. diff --git a/lib/chesstrainer_web/controllers/page_controller.ex b/lib/chesstrainer_web/controllers/page_controller.ex index c3b22a8..d69471c 100644 --- a/lib/chesstrainer_web/controllers/page_controller.ex +++ b/lib/chesstrainer_web/controllers/page_controller.ex @@ -1,6 +1,7 @@ defmodule ChesstrainerWeb.PageController do use ChesstrainerWeb, :controller + alias Chesstrainer.Accounts alias Chesstrainer.Attempts alias Chesstrainer.Branding alias Chesstrainer.DailyPuzzles @@ -23,6 +24,7 @@ defmodule ChesstrainerWeb.PageController do active_endgame: DailyPuzzles.today(), puzzle_count: Endgames.count_endgames(), user_count: Attempts.count_users_with_attempts(), + logged_in_count: Accounts.count_logged_in_users(), today: Date.utc_today() ) end diff --git a/lib/chesstrainer_web/controllers/page_html/home.html.heex b/lib/chesstrainer_web/controllers/page_html/home.html.heex index 2192a95..d151c41 100644 --- a/lib/chesstrainer_web/controllers/page_html/home.html.heex +++ b/lib/chesstrainer_web/controllers/page_html/home.html.heex @@ -23,7 +23,7 @@ ♜ - {count_label(@user_count)} players training + {count_label(@logged_in_count)} players training diff --git a/lib/chesstrainer_web/live/endgame_live/play.ex b/lib/chesstrainer_web/live/endgame_live/play.ex index 8e563ef..cbef366 100644 --- a/lib/chesstrainer_web/live/endgame_live/play.ex +++ b/lib/chesstrainer_web/live/endgame_live/play.ex @@ -58,7 +58,7 @@ defmodule ChesstrainerWeb.EndgameLive.Play do awaiting_best_line={@awaiting_best_line} /> -
+
<.rating_guide rating={@user_rating} deviation={@user_deviation} />
diff --git a/priv/repo/seeds/endgames/win.ex b/priv/repo/seeds/endgames/win.ex index d5794b3..a4344d7 100644 --- a/priv/repo/seeds/endgames/win.ex +++ b/priv/repo/seeds/endgames/win.ex @@ -23,11 +23,6 @@ defmodule Chesstrainer.Repo.Seeds.Endgames.Win do fen: "8/7p/6p1/8/k7/8/2K3P1/8 b - - 0 0", color: :black, result: :win - }, - %{ - fen: "R7/8/8/6p1/4k3/3rPp1P/8/6K1 b - - 0 1", - color: :black, - result: :win }, %{ fen: "8/8/7k/8/8/8/5q2/3B2RK b - - 0 1", diff --git a/test/chesstrainer/accounts_test.exs b/test/chesstrainer/accounts_test.exs index 4dd788d..cf8d34e 100644 --- a/test/chesstrainer/accounts_test.exs +++ b/test/chesstrainer/accounts_test.exs @@ -583,6 +583,25 @@ defmodule Chesstrainer.AccountsTest do end end + describe "count_logged_in_users/0" do + test "counts distinct users with a valid session token" do + user = user_fixture() + _ = Accounts.generate_user_session_token(user) + _ = Accounts.generate_user_session_token(user) + _other = Accounts.generate_user_session_token(user_fixture()) + + assert Accounts.count_logged_in_users() == 2 + end + + test "ignores expired session tokens" do + user = user_fixture() + user_token = Accounts.generate_user_session_token(user) + offset_user_token(user_token, -15, :day) + + assert Accounts.count_logged_in_users() == 0 + end + end + describe "delete_user/1" do test "deletes the user and their session tokens" do user = user_fixture() diff --git a/test/chesstrainer/seeds_test.exs b/test/chesstrainer/seeds_test.exs index 764d6ba..4fadcfb 100644 --- a/test/chesstrainer/seeds_test.exs +++ b/test/chesstrainer/seeds_test.exs @@ -3,6 +3,8 @@ defmodule Chesstrainer.SeedsTest do alias Chesstrainer.FEN + Code.require_file("../../priv/repo/seeds/endgames/win.ex", __DIR__) + Code.require_file("../../priv/repo/seeds/endgames/draw.ex", __DIR__) Code.require_file("../../priv/repo/seeds/endgames.ex", __DIR__) describe "seed endgames" do diff --git a/test/chesstrainer_web/controllers/page_controller_test.exs b/test/chesstrainer_web/controllers/page_controller_test.exs index 27a24d4..33697ac 100644 --- a/test/chesstrainer_web/controllers/page_controller_test.exs +++ b/test/chesstrainer_web/controllers/page_controller_test.exs @@ -4,6 +4,7 @@ defmodule ChesstrainerWeb.PageControllerTest do import Chesstrainer.AccountsFixtures import Chesstrainer.EndgamesFixtures + alias Chesstrainer.Accounts alias Chesstrainer.Accounts.Scope alias Chesstrainer.Attempts alias Chesstrainer.Branding @@ -158,7 +159,17 @@ defmodule ChesstrainerWeb.PageControllerTest do assert html =~ ~s(content="Test Brand") end - test "hero shows the number of players with at least one attempt", %{conn: conn} do + test "hero shows the number of users with a valid session", %{conn: conn} do + user = user_fixture() + _token = Accounts.generate_user_session_token(user) + + html = conn |> get(~p"/") |> html_response(200) + document = LazyHTML.from_document(html) + + assert LazyHTML.text(LazyHTML.query(document, ".rating-note strong")) == "1" + end + + test "hero does not count users who have only trained", %{conn: conn} do user = user_fixture() endgame = endgame_fixture() {:ok, _} = Attempts.record_attempt(Scope.for_user(user), endgame, :solved) @@ -166,7 +177,7 @@ defmodule ChesstrainerWeb.PageControllerTest do html = conn |> get(~p"/") |> html_response(200) document = LazyHTML.from_document(html) - assert LazyHTML.text(LazyHTML.query(document, ".rating-note strong")) == "1" + assert LazyHTML.text(LazyHTML.query(document, ".rating-note strong")) == "0" end test "guest navbar links to log in and register", %{conn: conn} do @@ -211,11 +222,10 @@ defmodule ChesstrainerWeb.PageControllerTest do assert LazyHTML.text(LazyHTML.query(document, ".board-topline")) =~ "White" assert html =~ "Try today's puzzle" - assert html =~ "Begin with the basics" assert html =~ "puzzles" end - test "hero and progress CTAs link to the daily puzzle", %{conn: conn} do + test "hero CTA links to the daily puzzle", %{conn: conn} do curated!() daily = DailyPuzzles.today() @@ -225,10 +235,6 @@ defmodule ChesstrainerWeb.PageControllerTest do assert LazyHTML.attribute(LazyHTML.query(document, ".hero-actions a"), "href") == [play_path] - assert LazyHTML.attribute(LazyHTML.query(document, ".progress-banner a"), "href") == [ - play_path - ] - refute html =~ "/endgames//play" end @@ -258,10 +264,6 @@ defmodule ChesstrainerWeb.PageControllerTest do assert LazyHTML.attribute(LazyHTML.query(document, ".hero-actions a"), "href") == [ "/endgames" ] - - assert LazyHTML.attribute(LazyHTML.query(document, ".progress-banner a"), "href") == [ - "/endgames" - ] end test "unknown routes render the branded 404 page", %{conn: conn} do