Files
superbrain/internal/models/models.go
T
Andrew S Erwin 1f38c4d56c
release gate / check (push) Failing after 7m7s
Add tenancy phase 1: implicit tenants, plan entitlements, quota counters
Foundation for the priced plans (PLAN_PRICING_TIERS.md): every user now
belongs to exactly one tenant, and single-tenant deployments (self-hosted,
core) use an implicit 'tenant-default' row so no code path special-cases
'no tenant'. Plans are per-tenant DATA, never code forks — the open-source
promise depends on the free version being the same binary.

Schema (migration 012 + schema.sql, converge- and atomicity-verified):
Tenant, TenantMember (1:1 per user in phase 1), TenantEntitlement
(per-tenant overrides), QuotaUsage (maintained counters), User.tenantId.
The backfill adopts every existing user into the implicit tenant
(ADMIN -> owner, others -> member). The User.tenantId FK is re-added by
name under a guard so converge pass 2 (CASCADE-drop + chain re-run) stays
clean.

internal/entitlements: the shipped plan catalog (free/standard/premium/
core/selfhosted: storage, brain links, seats, notification channels,
connectors, AI mode), tier -> default-plan derivation, override merging
that fails CLOSED (unknown plan resolves to free limits; malformed
override rows are ignored so a bad row can never widen access), and
WithinQuota boundary logic.

internal/db/tenants.go: EnsureDefault reconciles the implicit tenant's
plan with TIER but only ever touches auto plans (core/selfhosted) — sold
plans are billing data, not deployment config. AdoptOrphans is the runtime
self-heal twin of the migration backfill (restored dumps); main runs
Bootstrap once at startup, best-effort. QuotaAdd moves counters under an
advisory lock and refuses growth past the plan limit with ErrQuotaExceeded;
counters floor at 0.

Wiring: createUserTx attaches tenant + membership atomically for EVERY
creation path (signup, OIDC provisioning, seed CLI); the identity
projection carries tenantId, so /api/me and the bridge response expose it.
models.User gains TenantID.

No behavior changes for existing installs: default limits are declarative
until gate points land (connectors, brain links, notifications, storage
metering are later steps). API.md documents tenantId and the password-
reset endpoints; AGENTS.md records the invariant that features gate on
PLAN, never on TIER directly.

Tests: entitlements unit tests (catalog, fail-closed resolution, override
normalization, quota boundaries) and integration tests (attach-on-create
across all three paths with owner/member roles, bootstrap + plan
reconcile including 'sold plans never overwritten', limits resolution,
quota consume/refuse/release/floor).
2026-10-26 13:21:12 -05:00

198 lines
4.3 KiB
Go

package models
// KanbanStatus is the legacy enum. Porting guide 2.1.
type KanbanStatus string
const (
Inbox KanbanStatus = "INBOX"
Todo KanbanStatus = "TODO"
InProgress KanbanStatus = "IN_PROGRESS"
Done KanbanStatus = "DONE"
Archived KanbanStatus = "ARCHIVED"
)
// User. Porting guide 2.2.
type User struct {
ID string
Name *string
Email *string
EmailVerified *string
Image *string
Password *string
Role string // USER | ADMIN
TokenVersion int // embedded in cookie JWTs; bumping revokes them
TenantID *string
CreatedAt string
UpdatedAt string
}
// Account, Session, VerificationToken are auth-library tables. Porting guide 2.3.
// Session and VerificationToken are unused (sessions are JWT-based).
// Account is only needed if keeping OIDC.
type Account struct {
ID string
UserID string
Type string
Provider string
ProviderAccountID string
RefreshToken *string
AccessToken *string
ExpiresAt *int
TokenType *string
Scope *string
IDToken *string
SessionState *string
}
// Bookmark is the core object: one saved URL. Porting guide 2.4.
type Bookmark struct {
ID string
URL string
Title *string
Description *string
Favicon *string
OgImage *string
OgTitle *string
OgDescription *string
Notes *string
DueDate *string
AlertAt *string
AlertSent bool
KanbanStatus KanbanStatus
KanbanColumnID *string
ImapWatchEnabled bool
ImapQuery *string
IsPublic bool
OwnerID string
ContactID *string
CreatedAt string
UpdatedAt string
}
// Tag. Porting guide 2.5.
type Tag struct {
ID string
Name string
Color string
OwnerID string
CreatedAt string
UpdatedAt string
}
// BookmarkTag join. Porting guide 2.6.
type BookmarkTag struct {
BookmarkID string
TagID string
}
// BookmarkShare. Porting guide 2.7.
type BookmarkShare struct {
BookmarkID string
UserID string
CreatedAt string
}
// TagShare. Porting guide 2.8.
type TagShare struct {
TagID string
UserID string
CreatedAt string
}
// Contact, synced from Nextcloud CardDAV. Porting guide 2.9.
type Contact struct {
ID string
UID string
DisplayName string
Email *string
Phone *string
CardDavURL *string
UserID string
CreatedAt string
UpdatedAt string
}
// ImapConfig, one per user; password is encrypted. Porting guide 2.10.
type ImapConfig struct {
ID string
Host string
Port int
TLS bool
Username string
Password string // encrypted
Folder string
UserID string
}
// KanbanColumn, user-editable board column. Porting guide 2.11.
type KanbanColumn struct {
ID string
Label string
Color string
Position int
Key *string // INBOX/TODO/IN_PROGRESS/DONE for defaults, null for custom
UserID string
CreatedAt string
}
// Page, a Markdown workspace. Porting guide 2.12.
type Page struct {
ID string
Title string
Content string
Icon *string
Pinned bool
Position int
OwnerID string
CreatedAt string
UpdatedAt string
}
// PageBookmark links a bookmark onto a page. Porting guide 2.13.
type PageBookmark struct {
PageID string
BookmarkID string
Position int
CreatedAt string
}
// SignalSource, a watcher attached to a bookmark. Porting guide 2.14.
type SignalSource struct {
ID string
Type string
Config string // JSON, default "{}"
Enabled bool
LastCheckedAt *string
LastStatus *string
BookmarkID string
OwnerID string
CreatedAt string
UpdatedAt string
}
// Signal, one outside event about a bookmark. Porting guide 2.15.
type Signal struct {
ID string
SourceType string
Title string
Summary *string
URL *string
ExternalID *string
OccurredAt string
Read bool
SourceID *string
BookmarkID string
OwnerID string
CreatedAt string
}
// SharedDate. Porting guide 2.16.
type SharedDate struct {
ID string
Date string
Note *string
CreatorID string
RecipientID string
CreatedAt string
}