mirror of
https://github.com/J3vb/OwnCord.git
synced 2026-09-03 03:50:00 +03:00
Wire the sqlc-generated dbgen package into db.DB so it stops being dead
code (audit A-2026-07-05) and becomes the real, CI-verified query layer.
db.DB now holds a *dbgen.Queries (initialized in Open via dbgen.New).
Query method bodies delegate to it; sqlc owns the SQL text and parameter
binding (make sqlc-verify), while db keeps its stable public API and
domain model types so no caller in api/admin/ws/service changes. The
migration is incremental — a method either delegates to d.q.* or still
runs raw SQL — so both layers are correct during the transition.
Converted domains (now load-bearing through sqlc):
- blocks: BlockUser, UnblockUser, IsBlocked, IsEitherBlocked,
ListBlockedUsers (added the query to blocks.sql + regenerated).
Empty ListBlockedUsers now returns []int64{} instead of nil, matching
the MemStore backend — a latent inconsistency fixed, not a regression.
- lockouts: UpsertLockout, LoadActiveLockouts, CleanupExpiredLockouts,
DeleteLockout (RFC3339 time formatting/parsing kept in the wrappers).
- roles: GetRoleByID, ListRoles, GetRoleForUser via a shared roleFromGen
mapper (int64 position/is_default -> int/bool). GetUserWithRole stays
raw for now.
Remaining domains stay on raw SQL and are tracked in
docs/plans/sqlc-adoption.md; store/ event+plugin SQL is intentionally
excluded (that layer is removed in D3). Decisions doc + audit closure
updated (A-2026-07-05 -> in progress).
Verified: go build ./...; go test -race ./db ./service ./auth ./ws (api
green non-race, race run matches CI's -timeout 20m); make sqlc-verify and
protocol-verify pass with the regenerated output committed.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UA17KPvqGBX3XbXYnMf1rA
78 lines
2.0 KiB
Go
78 lines
2.0 KiB
Go
package db
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"database/sql"
|
|
|
|
"github.com/owncord/server/db/dbgen"
|
|
)
|
|
|
|
// BlockUser adds a block from blocker to blocked. Idempotent — re-blocking
|
|
// a user that is already blocked is a no-op (INSERT OR IGNORE).
|
|
func (d *DB) BlockUser(blockerID, blockedID int64) error {
|
|
if err := d.q.BlockUser(dbCtx(), dbgen.BlockUserParams{
|
|
BlockerID: blockerID,
|
|
BlockedID: blockedID,
|
|
}); err != nil {
|
|
return fmt.Errorf("BlockUser: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// UnblockUser removes a block. Idempotent — unblocking a non-blocked user is
|
|
// a no-op.
|
|
func (d *DB) UnblockUser(blockerID, blockedID int64) error {
|
|
if err := d.q.UnblockUser(dbCtx(), dbgen.UnblockUserParams{
|
|
BlockerID: blockerID,
|
|
BlockedID: blockedID,
|
|
}); err != nil {
|
|
return fmt.Errorf("UnblockUser: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// IsBlocked returns true if blockerID has blocked blockedID.
|
|
func (d *DB) IsBlocked(blockerID, blockedID int64) (bool, error) {
|
|
_, err := d.q.IsBlocked(dbCtx(), dbgen.IsBlockedParams{
|
|
BlockerID: blockerID,
|
|
BlockedID: blockedID,
|
|
})
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, fmt.Errorf("IsBlocked: %w", err)
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// IsEitherBlocked returns true if either user has blocked the other.
|
|
// Used for DM authorization — if either party has blocked the other,
|
|
// messaging is denied.
|
|
func (d *DB) IsEitherBlocked(userA, userB int64) (bool, error) {
|
|
_, err := d.q.IsEitherBlocked(dbCtx(), dbgen.IsEitherBlockedParams{
|
|
BlockerID: userA,
|
|
BlockedID: userB,
|
|
BlockerID_2: userB,
|
|
BlockedID_2: userA,
|
|
})
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return false, nil
|
|
}
|
|
if err != nil {
|
|
return false, fmt.Errorf("IsEitherBlocked: %w", err)
|
|
}
|
|
return true, nil
|
|
}
|
|
|
|
// ListBlockedUsers returns the IDs of all users blocked by the given user.
|
|
func (d *DB) ListBlockedUsers(blockerID int64) ([]int64, error) {
|
|
ids, err := d.q.ListBlockedUsers(dbCtx(), blockerID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("ListBlockedUsers: %w", err)
|
|
}
|
|
return ids, nil
|
|
}
|