refactor(server/db): delegate voice, dm, channels, admin to dbgen (D2)

voice: all reads (GetVoiceState, GetChannelVoiceStates, GetAllVoiceStates)
and writes (join/leave/mute/deafen/camera/screenshare/clear, capacity +
camera-limit atomic guards); CountChannelVoiceUsers stays raw (no query).
dm: OpenDM, CloseDM, IsDMParticipant, GetDMParticipantIDs; GetOrCreateDMChannel
(serializable tx), GetUserDMChannels (aggregate), GetDMRecipient stay raw.
channels: List/Get (shared channelFromFields mapper), Create/Update/Delete,
slow-mode/max-users setters, permission overrides get/list-for-role/upsert/
delete; ListChannelRoleOverrides + GetChannelTypes (variable IN) stay raw.
admin: UserCount, GetServerStats counts (PRAGMA stays raw), ListAllUsers,
UpdateUserRole, ForceLogoutUser, GetUserSessions, AdminUpdateChannel,
AdminDeleteChannel, LogAudit, GetAuditLog, GetSetting, SetSetting,
GetAllSettings, CountUsersWithoutTOTP; AdminCreateChannel + Backup stay raw.

Added b2i64 and strToNullPtr mapper helpers; retired the obsolete
scanChannel/nullableString. Behavior and public signatures unchanged.

Verified: go build ./...; go test ./db ./service ./permissions ./admin;
sqlc-verify.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UA17KPvqGBX3XbXYnMf1rA
This commit is contained in:
Claude
2026-07-19 15:43:46 +00:00
parent d769b8bdb6
commit c7e6702c8c
5 changed files with 299 additions and 485 deletions
+15 -32
View File
@@ -5,6 +5,8 @@ import (
"database/sql"
"errors"
"fmt"
"github.com/owncord/server/db/dbgen"
)
// ─── DM Models ──────────────────────────────────────────────────────────────
@@ -202,11 +204,10 @@ func (d *DB) GetUserDMChannels(userID int64) ([]DMChannelInfo, error) {
// OpenDM adds a DM channel to a user's open list (idempotent).
func (d *DB) OpenDM(userID, channelID int64) error {
_, err := d.sqlDB.Exec(
`INSERT OR IGNORE INTO dm_open_state (user_id, channel_id) VALUES (?, ?)`,
userID, channelID,
)
if err != nil {
if err := d.q.OpenDM(dbCtx(), dbgen.OpenDMParams{
UserID: userID,
ChannelID: channelID,
}); err != nil {
return fmt.Errorf("OpenDM: %w", err)
}
return nil
@@ -214,11 +215,10 @@ func (d *DB) OpenDM(userID, channelID int64) error {
// CloseDM removes a DM channel from a user's open list.
func (d *DB) CloseDM(userID, channelID int64) error {
_, err := d.sqlDB.Exec(
`DELETE FROM dm_open_state WHERE user_id = ? AND channel_id = ?`,
userID, channelID,
)
if err != nil {
if err := d.q.CloseDM(dbCtx(), dbgen.CloseDMParams{
UserID: userID,
ChannelID: channelID,
}); err != nil {
return fmt.Errorf("CloseDM: %w", err)
}
return nil
@@ -228,11 +228,10 @@ func (d *DB) CloseDM(userID, channelID int64) error {
// IsDMParticipant checks if a user is a participant in a DM channel.
func (d *DB) IsDMParticipant(userID, channelID int64) (bool, error) {
var id int64
err := d.sqlDB.QueryRow(
`SELECT user_id FROM dm_participants WHERE user_id = ? AND channel_id = ?`,
userID, channelID,
).Scan(&id)
_, err := d.q.IsDMParticipant(dbCtx(), dbgen.IsDMParticipantParams{
UserID: userID,
ChannelID: channelID,
})
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
@@ -244,26 +243,10 @@ func (d *DB) IsDMParticipant(userID, channelID int64) (bool, error) {
// GetDMParticipantIDs returns all participant user IDs for a DM channel.
func (d *DB) GetDMParticipantIDs(channelID int64) ([]int64, error) {
rows, err := d.sqlDB.Query(
`SELECT user_id FROM dm_participants WHERE channel_id = ?`,
channelID,
)
ids, err := d.q.GetDMParticipantIDs(dbCtx(), channelID)
if err != nil {
return nil, fmt.Errorf("GetDMParticipantIDs: %w", err)
}
defer rows.Close() //nolint:errcheck
var ids []int64
for rows.Next() {
var id int64
if scanErr := rows.Scan(&id); scanErr != nil {
return nil, fmt.Errorf("GetDMParticipantIDs scan: %w", scanErr)
}
ids = append(ids, id)
}
if rows.Err() != nil {
return nil, fmt.Errorf("GetDMParticipantIDs rows: %w", rows.Err())
}
return ids, nil
}