fix: resolve nestif issues by extracting helper methods
This commit is contained in:
@@ -303,12 +303,24 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
|
||||
content := strings.Join(lines, "\n")
|
||||
|
||||
if strings.HasPrefix(to, "#") {
|
||||
// Channel message.
|
||||
s.sendChannelMessage(w, r, uid, nick, to, content)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// DM.
|
||||
s.sendDirectMessage(w, r, uid, nick, to, content)
|
||||
}
|
||||
|
||||
func (s *Handlers) sendChannelMessage(
|
||||
w http.ResponseWriter, r *http.Request,
|
||||
uid, nick, channel, content string,
|
||||
) {
|
||||
var chID string
|
||||
|
||||
//nolint:gosec // G701: parameterized query, not injection
|
||||
err := s.params.Database.GetDB().QueryRowContext(r.Context(),
|
||||
"SELECT id FROM channels WHERE name = ?", to).Scan(&chID)
|
||||
"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)
|
||||
|
||||
@@ -324,8 +336,12 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
|
||||
}
|
||||
|
||||
s.respondJSON(w, r, map[string]any{"id": msgID, "status": "sent"}, http.StatusCreated)
|
||||
} else {
|
||||
// DM.
|
||||
}
|
||||
|
||||
func (s *Handlers) sendDirectMessage(
|
||||
w http.ResponseWriter, r *http.Request,
|
||||
uid, nick, to, content string,
|
||||
) {
|
||||
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)
|
||||
@@ -342,7 +358,6 @@ func (s *Handlers) handlePrivmsg(w http.ResponseWriter, r *http.Request, uid, ni
|
||||
}
|
||||
|
||||
s.respondJSON(w, r, map[string]any{"id": msgID, "status": "sent"}, http.StatusCreated)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Handlers) handleJoin(w http.ResponseWriter, r *http.Request, uid, to string) {
|
||||
@@ -495,12 +510,24 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
|
||||
}
|
||||
|
||||
if strings.HasPrefix(target, "#") {
|
||||
// Channel history — look up channel by name to get its ID for target matching.
|
||||
s.getChannelHistory(w, r, target, beforeTS, limit)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
s.getDMHistory(w, r, uid, target, beforeTS, limit)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Handlers) getChannelHistory(
|
||||
w http.ResponseWriter, r *http.Request,
|
||||
channel, beforeTS string, limit int,
|
||||
) {
|
||||
var chID string
|
||||
|
||||
//nolint:gosec // G701: parameterized query, not injection
|
||||
err := s.params.Database.GetDB().QueryRowContext(r.Context(),
|
||||
"SELECT id FROM channels WHERE name = ?", target).Scan(&chID)
|
||||
"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)
|
||||
|
||||
@@ -516,8 +543,12 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
|
||||
}
|
||||
|
||||
s.respondJSON(w, r, msgs, http.StatusOK)
|
||||
} else {
|
||||
// DM history.
|
||||
}
|
||||
|
||||
func (s *Handlers) getDMHistory(
|
||||
w http.ResponseWriter, r *http.Request,
|
||||
uid, target, beforeTS string, limit int,
|
||||
) {
|
||||
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)
|
||||
@@ -534,8 +565,6 @@ func (s *Handlers) HandleGetHistory() http.HandlerFunc {
|
||||
}
|
||||
|
||||
s.respondJSON(w, r, msgs, http.StatusOK)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// HandleServerInfo returns server metadata (MOTD, name).
|
||||
|
||||
Reference in New Issue
Block a user