fix: resolve 6 medium issues from full go-review

- MED-1: Document soundboard channelID=0 server-wide permission intent
- MED-2: Document os.Exit(0) in update handler skipping deferred cleanup
- MED-3: Replace os.ReadFile/WriteFile with streaming io.Copy in backup
  restore to avoid loading entire DB into memory
- MED-4: Add GetAllVoiceStates bulk query, eliminating N+1 per-channel
  queries in collectAllVoiceStates
- MED-5: Wrap handlePatchSettings updates in a transaction for atomicity
- MED-6: Add rows.Err() check after scan loop in getReactionsBatch
- MED-9: Replace manual port-stripping in serverHost with net.SplitHostPort
  for correct IPv6 handling
This commit is contained in:
jevb
2026-03-19 04:04:53 +01:00
parent 65a8403a92
commit 13797e7075
8 changed files with 103 additions and 41 deletions
+3
View File
@@ -373,6 +373,9 @@ func (d *DB) getReactionsBatch(msgIDs []int64, requestingUserID int64) (map[int6
ri.Me = me != 0
result[msgID] = append(result[msgID], ri)
}
if rows.Err() != nil {
return nil, fmt.Errorf("getReactionsBatch rows: %w", rows.Err())
}
return result, nil
}
+33
View File
@@ -89,6 +89,39 @@ func (d *DB) GetChannelVoiceStates(channelID int64) ([]VoiceState, error) {
return states, nil
}
// GetAllVoiceStates returns voice states across all voice channels in a single
// query. Used at startup to build the ready payload without N+1 per-channel queries.
func (d *DB) GetAllVoiceStates() ([]VoiceState, error) {
rows, err := d.sqlDB.Query(
`SELECT vs.user_id, vs.channel_id, u.username,
vs.muted, vs.deafened, vs.speaking,
vs.camera, vs.screenshare
FROM voice_states vs
JOIN users u ON u.id = vs.user_id
ORDER BY vs.channel_id, vs.joined_at ASC`,
)
if err != nil {
return nil, fmt.Errorf("GetAllVoiceStates: %w", err)
}
defer rows.Close() //nolint:errcheck
var states []VoiceState
for rows.Next() {
vs, scanErr := scanVoiceStateRow(rows)
if scanErr != nil {
return nil, fmt.Errorf("GetAllVoiceStates scan: %w", scanErr)
}
states = append(states, vs)
}
if rows.Err() != nil {
return nil, fmt.Errorf("GetAllVoiceStates rows: %w", rows.Err())
}
if states == nil {
states = []VoiceState{}
}
return states, nil
}
// UpdateVoiceMute sets the muted field for the given user's voice state.
// It is safe to call when the user is not in any channel (no-op).
func (d *DB) UpdateVoiceMute(userID int64, muted bool) error {