Fix code review feedback items 1-6, 8-10

- Item 1: Extract GetUserByID/GetChannelByID lookup methods, use from relation methods
- Item 2: Initialize slices with literals so JSON gets [] not null
- Item 3: Populate CreatedAt/UpdatedAt with time.Now() on all Create methods
- Item 4: Wrap each migration's SQL + recording in a transaction
- Item 5: Check error from res.LastInsertId() in QueueMessage
- Item 6: Add DequeueMessages and AckMessages methods
- Item 8: Add GetUserByNick, GetUserByToken, DeleteAuthToken, UpdateUserLastSeen
- Item 9: Run PRAGMA foreign_keys = ON on every new connection
- Item 10: Builds clean, all tests pass
This commit is contained in:
clawbot
2026-02-09 21:15:41 -08:00
parent 3f7aec7c47
commit fbe53179b8
7 changed files with 297 additions and 66 deletions

View File

@@ -2,6 +2,7 @@ package models
import (
"context"
"fmt"
"time"
)
@@ -18,20 +19,9 @@ type AuthToken struct {
// User returns the user who owns this token.
func (t *AuthToken) User(ctx context.Context) (*User, error) {
u := &User{}
u.SetDB(t.db)
err := t.GetDB().QueryRowContext(ctx, `
SELECT id, nick, password_hash, created_at, updated_at, last_seen_at
FROM users WHERE id = ?`,
t.UserID,
).Scan(
&u.ID, &u.Nick, &u.PasswordHash,
&u.CreatedAt, &u.UpdatedAt, &u.LastSeenAt,
)
if err != nil {
return nil, err
if ul := t.GetUserLookup(); ul != nil {
return ul.GetUserByID(ctx, t.UserID)
}
return u, nil
return nil, fmt.Errorf("user lookup not available")
}

View File

@@ -34,7 +34,7 @@ func (c *Channel) Members(ctx context.Context) ([]*ChannelMember, error) {
defer func() { _ = rows.Close() }()
var members []*ChannelMember
members := []*ChannelMember{}
for rows.Next() {
m := &ChannelMember{}
@@ -74,7 +74,7 @@ func (c *Channel) RecentMessages(
defer func() { _ = rows.Close() }()
var messages []*Message
messages := []*Message{}
for rows.Next() {
msg := &Message{}

View File

@@ -2,6 +2,7 @@ package models
import (
"context"
"fmt"
"time"
)
@@ -18,40 +19,18 @@ type ChannelMember struct {
// User returns the full User for this membership.
func (cm *ChannelMember) User(ctx context.Context) (*User, error) {
u := &User{}
u.SetDB(cm.db)
err := cm.GetDB().QueryRowContext(ctx, `
SELECT id, nick, password_hash, created_at, updated_at, last_seen_at
FROM users WHERE id = ?`,
cm.UserID,
).Scan(
&u.ID, &u.Nick, &u.PasswordHash,
&u.CreatedAt, &u.UpdatedAt, &u.LastSeenAt,
)
if err != nil {
return nil, err
if ul := cm.GetUserLookup(); ul != nil {
return ul.GetUserByID(ctx, cm.UserID)
}
return u, nil
return nil, fmt.Errorf("user lookup not available")
}
// Channel returns the full Channel for this membership.
func (cm *ChannelMember) Channel(ctx context.Context) (*Channel, error) {
c := &Channel{}
c.SetDB(cm.db)
err := cm.GetDB().QueryRowContext(ctx, `
SELECT id, name, topic, modes, created_at, updated_at
FROM channels WHERE id = ?`,
cm.ChannelID,
).Scan(
&c.ID, &c.Name, &c.Topic, &c.Modes,
&c.CreatedAt, &c.UpdatedAt,
)
if err != nil {
return nil, err
if cl := cm.GetChannelLookup(); cl != nil {
return cl.GetChannelByID(ctx, cm.ChannelID)
}
return c, nil
return nil, fmt.Errorf("channel lookup not available")
}

View File

@@ -3,7 +3,10 @@
// relation-fetching methods directly on model instances.
package models
import "database/sql"
import (
"context"
"database/sql"
)
// DB is the interface that models use to query the database.
// This avoids a circular import with the db package.
@@ -11,6 +14,16 @@ type DB interface {
GetDB() *sql.DB
}
// UserLookup provides user lookup by ID without circular imports.
type UserLookup interface {
GetUserByID(ctx context.Context, id string) (*User, error)
}
// ChannelLookup provides channel lookup by ID without circular imports.
type ChannelLookup interface {
GetChannelByID(ctx context.Context, id string) (*Channel, error)
}
// Base is embedded in all model structs to provide database access.
type Base struct {
db DB
@@ -25,3 +38,21 @@ func (b *Base) SetDB(d DB) {
func (b *Base) GetDB() *sql.DB {
return b.db.GetDB()
}
// GetUserLookup returns the DB as a UserLookup if it implements the interface.
func (b *Base) GetUserLookup() UserLookup {
if ul, ok := b.db.(UserLookup); ok {
return ul
}
return nil
}
// GetChannelLookup returns the DB as a ChannelLookup if it implements the interface.
func (b *Base) GetChannelLookup() ChannelLookup {
if cl, ok := b.db.(ChannelLookup); ok {
return cl
}
return nil
}

View File

@@ -2,6 +2,7 @@ package models
import (
"context"
"fmt"
"time"
)
@@ -18,20 +19,9 @@ type Session struct {
// User returns the user who owns this session.
func (s *Session) User(ctx context.Context) (*User, error) {
u := &User{}
u.SetDB(s.db)
err := s.GetDB().QueryRowContext(ctx, `
SELECT id, nick, password_hash, created_at, updated_at, last_seen_at
FROM users WHERE id = ?`,
s.UserID,
).Scan(
&u.ID, &u.Nick, &u.PasswordHash,
&u.CreatedAt, &u.UpdatedAt, &u.LastSeenAt,
)
if err != nil {
return nil, err
if ul := s.GetUserLookup(); ul != nil {
return ul.GetUserByID(ctx, s.UserID)
}
return u, nil
return nil, fmt.Errorf("user lookup not available")
}

View File

@@ -33,7 +33,7 @@ func (u *User) Channels(ctx context.Context) ([]*Channel, error) {
defer func() { _ = rows.Close() }()
var channels []*Channel
channels := []*Channel{}
for rows.Next() {
c := &Channel{}
@@ -70,7 +70,7 @@ func (u *User) QueuedMessages(ctx context.Context) ([]*Message, error) {
defer func() { _ = rows.Close() }()
var messages []*Message
messages := []*Message{}
for rows.Next() {
msg := &Message{}