Add complete database schema and ORM models

Schema (002_tables.sql):
- users: accounts with nick, password hash, timestamps
- auth_tokens: per-device tokens with expiry, linked to users
- channels: chat rooms with topic and mode flags
- channel_members: membership with per-user modes (+o, +v)
- messages: channel/DM history with structured JSON meta
- message_queue: per-user pending delivery queue
- sessions: server-held session state with idle timeout
- server_links: federation peer configuration

Models (internal/models/):
- All models embed Base for database access
- Relation methods on models: User.Channels(), User.QueuedMessages(),
  Channel.Members(), Channel.RecentMessages(), ChannelMember.User(),
  ChannelMember.Channel(), AuthToken.User(), Session.User()
- IDs are UUIDs (TEXT), not auto-increment integers
- JSON tags use camelCase per lint rules

All tables verified: migrations apply cleanly, 0 lint issues.
This commit is contained in:
clawbot
2026-02-09 14:54:35 -08:00
parent 03cbc3cd1a
commit b21508cecc
11 changed files with 451 additions and 23 deletions

View File

@@ -0,0 +1,57 @@
package models
import (
"context"
"time"
)
// ChannelMember represents a user's membership in a channel.
type ChannelMember struct {
Base
ChannelID string `json:"channelId"`
UserID string `json:"userId"`
Modes string `json:"modes"`
JoinedAt time.Time `json:"joinedAt"`
Nick string `json:"nick"` // denormalized from users table
}
// 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
}
return u, nil
}
// 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
}
return c, nil
}