diff --git a/Dockerfile.collector b/Dockerfile.collector index e0eb3c47..631d5a93 100644 --- a/Dockerfile.collector +++ b/Dockerfile.collector @@ -1,4 +1,4 @@ -FROM golang:latest AS builder-go +FROM golang:1.26.0 AS builder-go WORKDIR /workspace diff --git a/cmd/core/server/handlers/private/accounts.go b/cmd/core/server/handlers/private/accounts.go index 16008f6b..b322175c 100644 --- a/cmd/core/server/handlers/private/accounts.go +++ b/cmd/core/server/handlers/private/accounts.go @@ -36,7 +36,7 @@ func LoadAccountsHandler(client core.Client) http.HandlerFunc { return } - w.Write([]byte(fmt.Sprintf("working on %d accounts", len(accounts)-len(existing)))) + w.Write(fmt.Appendf(nil, "working on %d accounts", len(accounts)-len(existing))) log.Debug().Int("count", len(accounts)-len(existing)).Msg("importing accounts") go func(accounts []string, existing []models.Account) { @@ -64,10 +64,7 @@ func LoadAccountsHandler(client core.Client) http.HandlerFunc { sem := semaphore.NewWeighted(5) for realm, accounts := range accountsByRealm { for i := 0; i < len(accounts); i += batchSize { - end := i + batchSize - if end > len(accounts) { - end = len(accounts) - } + end := min(i+batchSize, len(accounts)) wg.Add(1) go func(accounts []string, realm types.Realm) { diff --git a/cmd/discord/commands/builder/command.go b/cmd/discord/commands/builder/command.go index 4a41915f..e1038eae 100644 --- a/cmd/discord/commands/builder/command.go +++ b/cmd/discord/commands/builder/command.go @@ -87,7 +87,7 @@ func (c Builder) Build() Command { NameLocalizations: &nameLocalized, DescriptionLocalizations: &descLocalized, Options: options, - DMPermission: common.Pointer(!c.guildOnly), + DMPermission: new(!c.guildOnly), Type: discordgo.ChatApplicationCommand, IntegrationTypes: c.integrationTypes, Contexts: c.interactionContexts, diff --git a/cmd/discord/commands/options.go b/cmd/discord/commands/options.go index e2c02453..f085333e 100644 --- a/cmd/discord/commands/options.go +++ b/cmd/discord/commands/options.go @@ -89,8 +89,8 @@ func GetDefaultStatsOptions(data []*discordgo.ApplicationCommandInteractionDataO var options StatsOptions options.TankSearch, _ = common.GetOption[string](data, "tank") - if strings.HasPrefix(options.TankSearch, "valid#vehicle#") { - options.TankID = strings.TrimPrefix(options.TankSearch, "valid#vehicle#") + if after, ok := strings.CutPrefix(options.TankSearch, "valid#vehicle#"); ok { + options.TankID = after } tier, ok := common.GetOption[float64](data, "tier") @@ -99,8 +99,8 @@ func GetDefaultStatsOptions(data []*discordgo.ApplicationCommandInteractionDataO } options.NicknameSearch, _ = common.GetOption[string](data, "nickname") - if strings.HasPrefix(options.NicknameSearch, "valid#account#") { - data := strings.Split(strings.TrimPrefix(options.NicknameSearch, "valid#account#"), "#") + if after, ok0 := strings.CutPrefix(options.NicknameSearch, "valid#account#"); ok0 { + data := strings.Split(after, "#") if len(data) == 2 { options.Realm = types.Realm(data[1]) options.AccountID = data[0] diff --git a/cmd/discord/commands/public/career.go b/cmd/discord/commands/public/career.go index 0e9a3cd0..59c3abec 100644 --- a/cmd/discord/commands/public/career.go +++ b/cmd/discord/commands/public/career.go @@ -18,7 +18,6 @@ import ( "github.com/cufee/aftermath/internal/permissions" stats "github.com/cufee/aftermath/internal/stats/client/common" "github.com/cufee/aftermath/internal/stats/fetch/v1" - "github.com/cufee/aftermath/internal/utils" ) var ( @@ -51,7 +50,7 @@ func careerCommandHandler(ctx common.Context) error { case options.UserID != "": // mentioned another user, check if the user has an account linked mentionedUser, _ := ctx.Core().Database().GetUserByID(ctx.Ctx(), options.UserID, database.WithConnections(), database.WithSubscriptions(), database.WithContent()) - defaultAccount, hasDefaultAccount := mentionedUser.Connection(models.ConnectionTypeWargaming, nil, utils.Pointer(true)) + defaultAccount, hasDefaultAccount := mentionedUser.Connection(models.ConnectionTypeWargaming, nil, new(true)) if !hasDefaultAccount { return ctx.Reply().IsError(common.UserError).Send("stats_error_connection_not_found_vague") } @@ -98,7 +97,7 @@ func careerCommandHandler(ctx common.Context) error { accountID = fmt.Sprint(accounts[0].ID) default: - defaultAccount, hasDefaultAccount := ctx.User().Connection(models.ConnectionTypeWargaming, nil, utils.Pointer(true)) + defaultAccount, hasDefaultAccount := ctx.User().Connection(models.ConnectionTypeWargaming, nil, new(true)) if !hasDefaultAccount { return ctx.Reply().Send("command_career_help_message") } diff --git a/cmd/discord/commands/public/my.go b/cmd/discord/commands/public/my.go index c0ae4007..12de5850 100644 --- a/cmd/discord/commands/public/my.go +++ b/cmd/discord/commands/public/my.go @@ -18,7 +18,6 @@ import ( "github.com/cufee/aftermath/internal/permissions" stats "github.com/cufee/aftermath/internal/stats/client/common" "github.com/cufee/aftermath/internal/stats/fetch/v1" - "github.com/cufee/aftermath/internal/utils" "github.com/pkg/errors" ) @@ -85,7 +84,7 @@ func init() { if len(parts) == 4 && parts[0] == "valid" { accountID = parts[1] } else { - defaultAccount, hasDefaultAccount := ctx.User().Connection(models.ConnectionTypeWargaming, nil, utils.Pointer(true)) + defaultAccount, hasDefaultAccount := ctx.User().Connection(models.ConnectionTypeWargaming, nil, new(true)) if !hasDefaultAccount { return ctx.Reply().IsError(common.UserError).Send("my_error_no_account_linked") } diff --git a/cmd/discord/commands/public/session.go b/cmd/discord/commands/public/session.go index 310f8ceb..bbb125a6 100644 --- a/cmd/discord/commands/public/session.go +++ b/cmd/discord/commands/public/session.go @@ -17,7 +17,6 @@ import ( "github.com/cufee/aftermath/internal/permissions" stats "github.com/cufee/aftermath/internal/stats/client/common" "github.com/cufee/aftermath/internal/stats/fetch/v1" - "github.com/cufee/aftermath/internal/utils" "github.com/pkg/errors" ) @@ -51,7 +50,7 @@ func init() { case options.UserID != "": // mentioned another user, check if the user has an account linked mentionedUser, _ := ctx.Core().Database().GetUserByID(ctx.Ctx(), options.UserID, database.WithConnections(), database.WithSubscriptions(), database.WithContent()) - defaultAccount, hasDefaultAccount := mentionedUser.Connection(models.ConnectionTypeWargaming, nil, utils.Pointer(true)) + defaultAccount, hasDefaultAccount := mentionedUser.Connection(models.ConnectionTypeWargaming, nil, new(true)) if !hasDefaultAccount { return ctx.Reply().IsError(common.UserError).Send("stats_error_connection_not_found_vague") } @@ -98,7 +97,7 @@ func init() { accountID = fmt.Sprint(accounts[0].ID) default: - defaultAccount, hasDefaultAccount := ctx.User().Connection(models.ConnectionTypeWargaming, nil, utils.Pointer(true)) + defaultAccount, hasDefaultAccount := ctx.User().Connection(models.ConnectionTypeWargaming, nil, new(true)) if !hasDefaultAccount { return ctx.Reply().Send("command_session_help_message") } diff --git a/cmd/discord/commands/public/widget.go b/cmd/discord/commands/public/widget.go index fbad1492..3719972c 100644 --- a/cmd/discord/commands/public/widget.go +++ b/cmd/discord/commands/public/widget.go @@ -11,7 +11,6 @@ import ( "github.com/cufee/aftermath/internal/constants" "github.com/cufee/aftermath/internal/database/models" "github.com/cufee/aftermath/internal/permissions" - "github.com/cufee/aftermath/internal/utils" ) func init() { @@ -32,7 +31,7 @@ func init() { if len(parts) == 4 && parts[0] == "valid" { accountID = parts[1] } else { - defaultAccount, hasDefaultAccount := ctx.User().Connection(models.ConnectionTypeWargaming, nil, utils.Pointer(true)) + defaultAccount, hasDefaultAccount := ctx.User().Connection(models.ConnectionTypeWargaming, nil, new(true)) if !hasDefaultAccount { return ctx.Reply().Format("commands_widget_message_fmt", constants.FrontendURL+"/widget/").Send() } diff --git a/cmd/discord/common/pointer.go b/cmd/discord/common/pointer.go deleted file mode 100644 index 7e5389b3..00000000 --- a/cmd/discord/common/pointer.go +++ /dev/null @@ -1,5 +0,0 @@ -package common - -func Pointer[T any](d T) *T { - return &d -} diff --git a/cmd/discord/common/reply.go b/cmd/discord/common/reply.go index 05174799..47c859af 100644 --- a/cmd/discord/common/reply.go +++ b/cmd/discord/common/reply.go @@ -3,6 +3,7 @@ package common import ( "context" "fmt" + "maps" "strings" "time" @@ -79,9 +80,7 @@ func (r Reply) Metadata() map[string]any { func (r Reply) WithMeta(data map[string]any) Reply { meta := r.Metadata() - for key, value := range data { - meta[key] = value - } + maps.Copy(meta, data) return r } diff --git a/cmd/discord/gateway/gateway.go b/cmd/discord/gateway/gateway.go index 4d0fe0ae..7dbb1260 100644 --- a/cmd/discord/gateway/gateway.go +++ b/cmd/discord/gateway/gateway.go @@ -24,7 +24,7 @@ type Client interface { Connect() error Disconnect() error - Handler(fn interface{}) + Handler(fn any) SetStatus(status status, text string, emoji *discordgo.Emoji) error } @@ -72,7 +72,7 @@ func (c *gatewayClient) Rest() *rest.Client { return c.rest } -func (c *gatewayClient) Handler(fn interface{}) { +func (c *gatewayClient) Handler(fn any) { c.manager.AddHandler(fn) } diff --git a/cmd/discord/router/handler.go b/cmd/discord/router/handler.go index 25792234..2b7f20b1 100644 --- a/cmd/discord/router/handler.go +++ b/cmd/discord/router/handler.go @@ -187,7 +187,7 @@ func (r *router) handleInteraction(ctx context.Context, interaction discordgo.In func sendPingReply(w http.ResponseWriter) { w.WriteHeader(http.StatusOK) - _, err := w.Write([]byte(fmt.Sprintf(`{"type": %d}`, discordgo.InteractionPing))) + _, err := w.Write(fmt.Appendf(nil, `{"type": %d}`, discordgo.InteractionPing)) if err != nil { log.Err(err).Msg("failed to reply to a discord PING") http.Error(w, "Internal Server Error", http.StatusInternalServerError) diff --git a/cmd/frontend/handler/context.go b/cmd/frontend/handler/context.go index 02d3b27e..49b95c88 100644 --- a/cmd/frontend/handler/context.go +++ b/cmd/frontend/handler/context.go @@ -209,7 +209,7 @@ func (ctx *Context) Error(format string, args ...any) error { } func (ctx *Context) String(format string, args ...any) error { - _, err := ctx.w.Write([]byte(fmt.Sprintf(format, args...))) + _, err := ctx.w.Write(fmt.Appendf(nil, format, args...)) return err } @@ -217,7 +217,7 @@ func (ctx *Context) JSON(data any) error { ctx.w.Header().Set("Content-Type", "application/json") err := json.NewEncoder(ctx.w).Encode(data) if err != nil { - ctx.w.Write([]byte(fmt.Sprintf(`{"error":"%s"}`, err.Error()))) + ctx.w.Write(fmt.Appendf(nil, `{"error":"%s"}`, err.Error())) } return nil } diff --git a/cmd/frontend/logic/scripts.go b/cmd/frontend/logic/scripts.go index c4bd28a0..50058a61 100644 --- a/cmd/frontend/logic/scripts.go +++ b/cmd/frontend/logic/scripts.go @@ -12,7 +12,7 @@ import ( "github.com/tdewolff/minify/v2/js" ) -func EmbedScript(script templ.ComponentScript, params ...interface{}) templ.Component { +func EmbedScript(script templ.ComponentScript, params ...any) templ.Component { return templ.ComponentFunc(func(ctx context.Context, w io.Writer) (err error) { if _, err = io.WriteString(w, `