Add public codes for player URLs and admin endgame search

Player-facing endgame links now use a 10-character Crockford base32 public code instead of the database UUID; admin routes keep the UUID. Existing rows are backfilled by migration, and the admin Show page displays and copies the code.

The admin endgame index gains its own search box: exact database id, exact public code, or a FEN / FEN fragment. A single match opens the endgame; multiple matches list below; nothing found says so.

Closes #44
Closes #45
This commit is contained in:
2026-11-03 22:46:38 +01:00
parent 160e8629b7
commit 49736d186c
18 changed files with 544 additions and 64 deletions
@@ -0,0 +1,47 @@
defmodule Chesstrainer.Repo.Migrations.AddPublicCodeToEndgames do
use Ecto.Migration
@alphabet ~c"0123456789abcdefghjkmnpqrstvwxyz"
@length 10
def up do
alter table(:endgames) do
add :public_code, :string
end
flush()
repo().query!("SELECT id FROM endgames").rows
|> Enum.reduce(MapSet.new(), fn [id], used ->
code = Stream.repeatedly(&generate_code/0) |> Enum.find(&(not MapSet.member?(used, &1)))
repo().query!("UPDATE endgames SET public_code = $1 WHERE id = $2", [code, id])
MapSet.put(used, code)
end)
alter table(:endgames) do
modify :public_code, :string, null: false
end
create unique_index(:endgames, [:public_code])
end
def down do
drop unique_index(:endgames, [:public_code])
alter table(:endgames) do
remove :public_code
end
end
# Inlined rather than calling the schema, so the migration stays frozen even
# if the app's generator changes. `rem(byte, 32)` is unbiased: 256 = 8 * 32.
defp generate_code do
@length
|> :crypto.strong_rand_bytes()
|> :binary.bin_to_list()
|> Enum.map(fn byte -> Enum.at(@alphabet, rem(byte, length(@alphabet))) end)
|> List.to_string()
end
end