fix: resolve nlreturn, modernize, perfsprint, wsl_v5, and partial err113 lint issues

This commit is contained in:
clawbot
2026-02-20 02:59:15 -08:00
parent c65c9bbe5a
commit c1040ff69d
11 changed files with 189 additions and 44 deletions

View File

@@ -34,6 +34,7 @@ func (s *Handlers) requireAuth(w http.ResponseWriter, r *http.Request) (string,
uid, nick, err := s.authUser(r)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "unauthorized"}, http.StatusUnauthorized)
return "", "", false
}
@@ -52,6 +53,7 @@ func (s *Handlers) HandleCreateSession() http.HandlerFunc {
type request struct {
Nick string `json:"nick"`
}
type response struct {
ID string `json:"id"`
Nick string `json:"nick"`
@@ -62,12 +64,14 @@ func (s *Handlers) HandleCreateSession() http.HandlerFunc {
var req request
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
s.respondJSON(w, r, map[string]string{"error": "invalid request"}, http.StatusBadRequest)
return
}
req.Nick = strings.TrimSpace(req.Nick)
if req.Nick == "" || len(req.Nick) > 32 {
s.respondJSON(w, r, map[string]string{"error": "nick must be 1-32 characters"}, http.StatusBadRequest)
return
}
@@ -77,6 +81,7 @@ func (s *Handlers) HandleCreateSession() http.HandlerFunc {
if err != nil {
if strings.Contains(err.Error(), "UNIQUE") {
s.respondJSON(w, r, map[string]string{"error": "nick already taken"}, http.StatusConflict)
return
}
@@ -162,6 +167,7 @@ func (s *Handlers) HandleChannelMembers() http.HandlerFunc {
"SELECT id FROM channels WHERE name = ?", name).Scan(&chID)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
return
}
@@ -205,10 +211,10 @@ func (s *Handlers) HandleGetMessages() http.HandlerFunc {
// The "command" field dispatches to the appropriate logic.
func (s *Handlers) HandleSendCommand() http.HandlerFunc {
type request struct {
Command string `json:"command"`
To string `json:"to"`
Params []string `json:"params,omitempty"`
Body interface{} `json:"body,omitempty"`
Command string `json:"command"`
To string `json:"to"`
Params []string `json:"params,omitempty"`
Body any `json:"body,omitempty"`
}
return func(w http.ResponseWriter, r *http.Request) {
@@ -218,8 +224,10 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
}
var req request
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
err := json.NewDecoder(r.Body).Decode(&req)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "invalid request"}, http.StatusBadRequest)
return
}
@@ -229,7 +237,7 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
// Helper to extract body as string lines.
bodyLines := func() []string {
switch v := req.Body.(type) {
case []interface{}:
case []any:
lines := make([]string, 0, len(v))
for _, item := range v {
@@ -267,6 +275,7 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
default:
_ = nick // suppress unused warning
s.respondJSON(w, r, map[string]string{"error": "unknown command: " + req.Command}, http.StatusBadRequest)
}
}
@@ -275,11 +284,13 @@ func (s *Handlers) HandleSendCommand() http.HandlerFunc {
func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, nick, to string, lines []string) {
if to == "" {
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
return
}
if len(lines) == 0 {
s.respondJSON(w, r, map[string]string{"error": "body required"}, http.StatusBadRequest)
return
}
@@ -293,6 +304,7 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
"SELECT id FROM channels WHERE name = ?", to).Scan(&chID)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
return
}
@@ -310,6 +322,7 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
targetUser, err := s.params.Database.GetUserByNick(r.Context(), to)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "user not found"}, http.StatusNotFound)
return
}
@@ -328,6 +341,7 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
func (s *Handlers) handleJoin(w http.ResponseWriter, r *http.Request, uid, to string) {
if to == "" {
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
return
}
@@ -357,6 +371,7 @@ func (s *Handlers) handleJoin(w http.ResponseWriter, r *http.Request, uid, to st
func (s *Handlers) handlePart(w http.ResponseWriter, r *http.Request, uid, to string) {
if to == "" {
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
return
}
@@ -371,6 +386,7 @@ func (s *Handlers) handlePart(w http.ResponseWriter, r *http.Request, uid, to st
"SELECT id FROM channels WHERE name = ?", channel).Scan(&chID)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
return
}
@@ -387,18 +403,22 @@ func (s *Handlers) handlePart(w http.ResponseWriter, r *http.Request, uid, to st
func (s *Handlers) handleNick(w http.ResponseWriter, r *http.Request, uid string, lines []string) {
if len(lines) == 0 {
s.respondJSON(w, r, map[string]string{"error": "body required (new nick)"}, http.StatusBadRequest)
return
}
newNick := strings.TrimSpace(lines[0])
if newNick == "" || len(newNick) > 32 {
s.respondJSON(w, r, map[string]string{"error": "nick must be 1-32 characters"}, http.StatusBadRequest)
return
}
if err := s.params.Database.ChangeNick(r.Context(), uid, newNick); err != nil {
err := s.params.Database.ChangeNick(r.Context(), uid, newNick)
if err != nil {
if strings.Contains(err.Error(), "UNIQUE") {
s.respondJSON(w, r, map[string]string{"error": "nick already in use"}, http.StatusConflict)
return
}
@@ -414,11 +434,13 @@ func (s *Handlers) handleNick(w http.ResponseWriter, r *http.Request, uid string
func (s *Handlers) handleTopic(w http.ResponseWriter, r *http.Request, uid, to string, lines []string) {
if to == "" {
s.respondJSON(w, r, map[string]string{"error": "to field required"}, http.StatusBadRequest)
return
}
if len(lines) == 0 {
s.respondJSON(w, r, map[string]string{"error": "body required (topic text)"}, http.StatusBadRequest)
return
}
@@ -429,7 +451,8 @@ func (s *Handlers) handleTopic(w http.ResponseWriter, r *http.Request, uid, to s
channel = "#" + channel
}
if err := s.params.Database.SetTopic(r.Context(), channel, uid, topic); err != nil {
err := s.params.Database.SetTopic(r.Context(), channel, uid, topic)
if err != nil {
s.log.Error("set topic failed", "error", err)
s.respondJSON(w, r, map[string]string{"error": "internal error"}, http.StatusInternalServerError)
@@ -450,6 +473,7 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
target := r.URL.Query().Get("target")
if target == "" {
s.respondJSON(w, r, map[string]string{"error": "target required"}, http.StatusBadRequest)
return
}
@@ -468,6 +492,7 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
"SELECT id FROM channels WHERE name = ?", target).Scan(&chID)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "channel not found"}, http.StatusNotFound)
return
}
@@ -485,6 +510,7 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
targetUser, err := s.params.Database.GetUserByNick(r.Context(), target)
if err != nil {
s.respondJSON(w, r, map[string]string{"error": "user not found"}, http.StatusNotFound)
return
}