player profile added
This commit is contained in:
@@ -906,6 +906,12 @@
|
||||
.border-green {
|
||||
border-color: var(--green);
|
||||
}
|
||||
.border-green\/30 {
|
||||
border-color: var(--green);
|
||||
@supports (color: color-mix(in lab, red, red)) {
|
||||
border-color: color-mix(in oklab, var(--green) 30%, transparent);
|
||||
}
|
||||
}
|
||||
.border-overlay0 {
|
||||
border-color: var(--overlay0);
|
||||
}
|
||||
@@ -1002,6 +1008,12 @@
|
||||
.bg-green {
|
||||
background-color: var(--green);
|
||||
}
|
||||
.bg-green\/10 {
|
||||
background-color: var(--green);
|
||||
@supports (color: color-mix(in lab, red, red)) {
|
||||
background-color: color-mix(in oklab, var(--green) 10%, transparent);
|
||||
}
|
||||
}
|
||||
.bg-green\/20 {
|
||||
background-color: var(--green);
|
||||
@supports (color: color-mix(in lab, red, red)) {
|
||||
@@ -1371,6 +1383,9 @@
|
||||
.italic {
|
||||
font-style: italic;
|
||||
}
|
||||
.underline {
|
||||
text-decoration-line: underline;
|
||||
}
|
||||
.placeholder-subtext0 {
|
||||
&::placeholder {
|
||||
color: var(--subtext0);
|
||||
|
||||
104
internal/handlers/player_link_slapid.go
Normal file
104
internal/handlers/player_link_slapid.go
Normal file
@@ -0,0 +1,104 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
"git.haelnorr.com/h/oslstats/internal/notify"
|
||||
"git.haelnorr.com/h/oslstats/internal/throw"
|
||||
playersview "git.haelnorr.com/h/oslstats/internal/view/playersview"
|
||||
"git.haelnorr.com/h/oslstats/pkg/slapshotapi"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// LinkPlayerSlapID handles the HTMX POST request to link a player's Slapshot ID
|
||||
// via their Discord Steam connection. Only the player's owner can trigger this.
|
||||
func LinkPlayerSlapID(
|
||||
s *hws.Server,
|
||||
conn *db.DB,
|
||||
slapAPI *slapshotapi.SlapAPI,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
playerIDStr := r.PathValue("player_id")
|
||||
|
||||
playerID, err := strconv.Atoi(playerIDStr)
|
||||
if err != nil {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return
|
||||
}
|
||||
|
||||
var player *db.Player
|
||||
|
||||
if ok := conn.WithNotifyTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
user := db.CurrentUser(ctx)
|
||||
if user == nil {
|
||||
throw.Unauthorized(s, w, r, "You must be logged in", errors.New("user not authenticated"))
|
||||
return false, nil
|
||||
}
|
||||
|
||||
var err error
|
||||
player, err = db.GetPlayer(ctx, tx, playerID)
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return false, nil
|
||||
}
|
||||
return false, errors.Wrap(err, "db.GetPlayer")
|
||||
}
|
||||
|
||||
// Verify the current user owns this player
|
||||
if player.UserID == nil || *player.UserID != user.ID {
|
||||
throw.ForbiddenSecurity(s, w, r, "You can only link your own player", errors.New("user does not own player"))
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Player already has a SlapID
|
||||
if player.SlapID != nil {
|
||||
notify.Info(s, w, r, "Already Linked", "Your Slapshot ID is already linked", nil)
|
||||
return false, nil
|
||||
}
|
||||
|
||||
// Get the user's discord token to look up steam connection
|
||||
discordToken, err := user.GetDiscordToken(ctx, tx)
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
notify.Warn(s, w, r, "Link Failed", "Discord token not found. Please log out and log back in.", nil)
|
||||
return false, nil
|
||||
}
|
||||
return false, errors.Wrap(err, "user.GetDiscordToken")
|
||||
}
|
||||
|
||||
audit := db.NewAudit(r.RemoteAddr, r.UserAgent(), user)
|
||||
err = ConnectSlapID(ctx, tx, user, discordToken.Convert(), slapAPI, audit)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "ConnectSlapID")
|
||||
}
|
||||
|
||||
// Re-fetch the player to check if SlapID was set
|
||||
player, err = db.GetPlayer(ctx, tx, playerID)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "db.GetPlayer")
|
||||
}
|
||||
|
||||
if player.SlapID == nil {
|
||||
// ConnectSlapID returned nil (silent failure) - no steam or no slapID
|
||||
notify.Warn(s, w, r, "Link Failed",
|
||||
"Could not find your Slapshot ID. Make sure your Steam account is connected to Discord and you have played Slapshot: Rebound.",
|
||||
nil)
|
||||
} else {
|
||||
notify.Success(s, w, r, "Success", "Your Slapshot ID has been linked!", nil)
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// Re-render the slap ID section with updated state
|
||||
renderSafely(playersview.SlapIDSection(player, true), s, r, w)
|
||||
})
|
||||
}
|
||||
84
internal/handlers/player_view.go
Normal file
84
internal/handlers/player_view.go
Normal file
@@ -0,0 +1,84 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"git.haelnorr.com/h/golib/hws"
|
||||
"git.haelnorr.com/h/oslstats/internal/db"
|
||||
"git.haelnorr.com/h/oslstats/internal/throw"
|
||||
playersview "git.haelnorr.com/h/oslstats/internal/view/playersview"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/uptrace/bun"
|
||||
)
|
||||
|
||||
// ProfileRedirect redirects the authenticated user to their own player page.
|
||||
func ProfileRedirect(
|
||||
s *hws.Server,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
user := db.CurrentUser(r.Context())
|
||||
if user == nil {
|
||||
throw.Unauthorized(s, w, r, "You must be logged in to view your profile", errors.New("user not authenticated"))
|
||||
return
|
||||
}
|
||||
if user.Player == nil {
|
||||
throw.InternalServiceError(s, w, r, "Player profile not found", errors.New("user has no linked player"))
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, fmt.Sprintf("/players/%d", user.Player.ID), http.StatusSeeOther)
|
||||
})
|
||||
}
|
||||
|
||||
// PlayerView renders the player profile page.
|
||||
// If the player has no SlapID and the viewer is the player's owner, show the link prompt.
|
||||
// If the player has no SlapID and the viewer is not the owner, show 404.
|
||||
func PlayerView(
|
||||
s *hws.Server,
|
||||
conn *db.DB,
|
||||
) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
playerIDStr := r.PathValue("player_id")
|
||||
|
||||
playerID, err := strconv.Atoi(playerIDStr)
|
||||
if err != nil {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return
|
||||
}
|
||||
|
||||
var player *db.Player
|
||||
var isOwner bool
|
||||
|
||||
if ok := conn.WithReadTx(s, w, r, func(ctx context.Context, tx bun.Tx) (bool, error) {
|
||||
var err error
|
||||
player, err = db.GetPlayer(ctx, tx, playerID)
|
||||
if err != nil {
|
||||
if db.IsBadRequest(err) {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return false, nil
|
||||
}
|
||||
return false, errors.Wrap(err, "db.GetPlayer")
|
||||
}
|
||||
|
||||
// Check if the current user owns this player
|
||||
user := db.CurrentUser(ctx)
|
||||
if user != nil && player.UserID != nil && *player.UserID == user.ID {
|
||||
isOwner = true
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}); !ok {
|
||||
return
|
||||
}
|
||||
|
||||
// If player has no SlapID and viewer is not the owner, show 404
|
||||
if player.SlapID == nil && !isOwner {
|
||||
throw.NotFound(s, w, r, r.URL.Path)
|
||||
return
|
||||
}
|
||||
|
||||
renderSafely(playersview.PlayerPage(player, isOwner), s, r, w)
|
||||
})
|
||||
}
|
||||
@@ -73,7 +73,7 @@ func Register(
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "registerUser")
|
||||
}
|
||||
err = connectSlapID(ctx, tx, user, details.Token, slapAPI, audit)
|
||||
err = ConnectSlapID(ctx, tx, user, details.Token, slapAPI, audit)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "connectSlapID")
|
||||
}
|
||||
@@ -123,11 +123,11 @@ func registerUser(ctx context.Context, tx bun.Tx,
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func connectSlapID(ctx context.Context, tx bun.Tx, user *db.User,
|
||||
// ConnectSlapID attempts to link a player's Slapshot ID via their Discord Steam connection.
|
||||
// If fails due to no steam connection or no slapID, fails silently and returns nil.
|
||||
func ConnectSlapID(ctx context.Context, tx bun.Tx, user *db.User,
|
||||
token *discord.Token, slapAPI *slapshotapi.SlapAPI, audit *db.AuditMeta,
|
||||
) error {
|
||||
// Attempt to setup their player/slapID from steam connection
|
||||
// If fails due to no steam connection or no slapID, fail silently and proceed with registration
|
||||
session, err := discord.NewOAuthSession(token)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "discord.NewOAuthSession")
|
||||
|
||||
@@ -64,6 +64,11 @@ func addRoutes(
|
||||
Methods: []hws.Method{hws.MethodGET, hws.MethodPOST},
|
||||
Handler: auth.LoginReq(handlers.Logout(s, auth, conn, discordAPI)),
|
||||
},
|
||||
{
|
||||
Path: "/profile",
|
||||
Method: hws.MethodGET,
|
||||
Handler: auth.LoginReq(handlers.ProfileRedirect(s)),
|
||||
},
|
||||
}
|
||||
|
||||
seasonRoutes := []hws.Route{
|
||||
@@ -295,6 +300,19 @@ func addRoutes(
|
||||
},
|
||||
}
|
||||
|
||||
playerRoutes := []hws.Route{
|
||||
{
|
||||
Path: "/players/{player_id}",
|
||||
Method: hws.MethodGET,
|
||||
Handler: handlers.PlayerView(s, conn),
|
||||
},
|
||||
{
|
||||
Path: "/players/{player_id}/link-slapid",
|
||||
Method: hws.MethodPOST,
|
||||
Handler: auth.LoginReq(handlers.LinkPlayerSlapID(s, conn, slapAPI)),
|
||||
},
|
||||
}
|
||||
|
||||
teamRoutes := []hws.Route{
|
||||
{
|
||||
Path: "/teams",
|
||||
@@ -468,6 +486,7 @@ func addRoutes(
|
||||
routes = append(routes, leagueRoutes...)
|
||||
routes = append(routes, fixturesRoutes...)
|
||||
routes = append(routes, teamRoutes...)
|
||||
routes = append(routes, playerRoutes...)
|
||||
|
||||
// Register the routes with the server
|
||||
err := s.AddRoutes(routes...)
|
||||
|
||||
38
internal/view/playersview/player_page.templ
Normal file
38
internal/view/playersview/player_page.templ
Normal file
@@ -0,0 +1,38 @@
|
||||
package playersview
|
||||
|
||||
import "git.haelnorr.com/h/oslstats/internal/db"
|
||||
import "git.haelnorr.com/h/oslstats/internal/view/baseview"
|
||||
import "fmt"
|
||||
|
||||
templ PlayerPage(player *db.Player, isOwner bool) {
|
||||
@baseview.Layout(player.DisplayName() + " - Player Profile") {
|
||||
<div class="max-w-screen-xl mx-auto px-4 py-8">
|
||||
<div class="bg-mantle border border-surface1 rounded-lg overflow-hidden">
|
||||
<!-- Header -->
|
||||
<div class="bg-surface0 border-b border-surface1 px-6 py-8">
|
||||
<div class="flex flex-col sm:flex-row justify-between items-start sm:items-center gap-4">
|
||||
<div>
|
||||
<h1 class="text-4xl font-bold text-text">{ player.DisplayName() }</h1>
|
||||
<div class="flex items-center gap-2 mt-2 flex-wrap">
|
||||
if player.SlapID != nil {
|
||||
<span class="px-2 py-1 bg-mantle rounded text-subtext0 font-mono text-sm">
|
||||
Slapshot ID: { fmt.Sprintf("%d", *player.SlapID) }
|
||||
</span>
|
||||
}
|
||||
if isOwner {
|
||||
<span class="px-2 py-0.5 bg-blue/20 text-blue rounded text-xs font-medium">
|
||||
Your Profile
|
||||
</span>
|
||||
}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- Content -->
|
||||
<div class="p-6">
|
||||
@SlapIDSection(player, isOwner)
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
77
internal/view/playersview/slap_id_section.templ
Normal file
77
internal/view/playersview/slap_id_section.templ
Normal file
@@ -0,0 +1,77 @@
|
||||
package playersview
|
||||
|
||||
import "git.haelnorr.com/h/oslstats/internal/db"
|
||||
import "fmt"
|
||||
|
||||
templ SlapIDSection(player *db.Player, isOwner bool) {
|
||||
<div id="slap-id-section">
|
||||
if player.SlapID == nil && isOwner {
|
||||
@slapIDLinkPrompt(player)
|
||||
} else if player.SlapID != nil {
|
||||
@slapIDLinked(player)
|
||||
}
|
||||
</div>
|
||||
}
|
||||
|
||||
templ slapIDLinkPrompt(player *db.Player) {
|
||||
<div class="bg-yellow/10 border border-yellow/30 rounded-lg p-6">
|
||||
<div class="flex items-start gap-4">
|
||||
<svg class="w-6 h-6 text-yellow shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 16.5c-.77.833.192 2.5 1.732 2.5z"
|
||||
></path>
|
||||
</svg>
|
||||
<div class="flex-1">
|
||||
<h3 class="text-lg font-semibold text-yellow mb-2">Slapshot ID Not Linked</h3>
|
||||
<p class="text-subtext0 mb-4">
|
||||
Your Slapshot ID is not linked. Please link your Steam account to your Discord account, then click the button below to connect your Slapshot ID.
|
||||
</p>
|
||||
<p class="text-subtext1 text-sm mb-4">
|
||||
Need help linking Steam to Discord?
|
||||
<a
|
||||
href="https://support.discord.com/hc/en-us/articles/32330173689623-Account-Connections-on-Discord-FAQ#h_01JVZBVNC1HYWX4BTPFN9B4B1V"
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
class="text-blue hover:text-blue/80 underline transition"
|
||||
>
|
||||
Follow this guide
|
||||
</a>
|
||||
</p>
|
||||
<button
|
||||
hx-post={ fmt.Sprintf("/players/%d/link-slapid", player.ID) }
|
||||
hx-target="#slap-id-section"
|
||||
hx-swap="outerHTML"
|
||||
class="px-4 py-2 bg-green hover:bg-green/75 text-mantle rounded font-medium transition hover:cursor-pointer"
|
||||
>
|
||||
Link Slapshot ID
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
|
||||
templ slapIDLinked(player *db.Player) {
|
||||
<div class="bg-green/10 border border-green/30 rounded-lg p-4">
|
||||
<div class="flex items-center gap-3">
|
||||
<svg class="w-5 h-5 text-green shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
|
||||
<path
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
stroke-width="2"
|
||||
d="M5 13l4 4L19 7"
|
||||
></path>
|
||||
</svg>
|
||||
<span class="text-text">
|
||||
Slapshot ID linked:
|
||||
<span class="font-mono text-subtext0">
|
||||
if player.SlapID != nil {
|
||||
{ fmt.Sprintf("%d", *player.SlapID) }
|
||||
}
|
||||
</span>
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
Reference in New Issue
Block a user