Files
OwnCord/Server/service/invite.go
T
Claude 5f1d6fc287 refactor(server): remove the store abstraction layer (D3)
Deletes Server/store (SQLiteStore, MemStore, the composed Store
interface) and collapses to a single sqlc-backed db package, executing
the prior audit's P4 "single data layer" direction (finding #6).

SQLiteStore was a pure pass-through to *db.DB, so consumers now depend
on narrow interfaces that *db.DB satisfies directly:

  - service.Store   (service/datastore.go, renamed from store/store.go)
  - ws.EventStore   (ws/eventstore.go)
  - plugin.PluginStore (plugin/pluginstore.go)

The event- and plugin-KV methods that lived in the store's SQLite
implementation move into the db package (db/event_queries.go,
db/plugin_queries.go), keeping their raw-SQL form.

Tests: the MemStore-based unit tests now run against a real in-memory
SQLite db opened per-test with migrations applied, via package-local
seed helpers. Fault-injection tests embed a real *db.DB and override the
single method under test, preserving error-path coverage. Full server
suite and sqlc-verify are green.

Docs: audit finding #6 and A-2026-07-06 marked resolved; decisions D3
updated; architecture server.md / data-model.md diagrams and prose
updated to the api -> service -> db layering.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01UA17KPvqGBX3XbXYnMf1rA
2026-07-19 16:33:58 +00:00

83 lines
2.2 KiB
Go

package service
import (
"context"
"fmt"
"time"
"github.com/owncord/server/db"
"github.com/owncord/server/telemetry"
)
// InviteService handles invite management.
type InviteService struct {
st Store
}
// NewInviteService creates an InviteService.
func NewInviteService(st Store) *InviteService {
return &InviteService{st: st}
}
// maxInviteExpiryHoursVal caps invite expiry to 30 days (H-4 hardening).
const maxInviteExpiryHoursVal = 720
// MaxInviteExpiryHours returns the maximum invite expiry in hours.
func MaxInviteExpiryHours() int { return maxInviteExpiryHoursVal }
// CreateInvite creates a new invite code with optional max uses and expiry.
func (s *InviteService) CreateInvite(ctx context.Context, createdBy int64, maxUses int, expiresInHours int) (*db.Invite, error) {
ctx, span := telemetry.GlobalTracer("service/invite").Start(ctx, "InviteService.CreateInvite",
telemetry.Int64("created_by", createdBy),
)
start := time.Now()
defer func() {
telemetry.TimeSince(ctx, telemetry.NewAppMetrics().ServiceCallDurationSec, start,
telemetry.String("method", "CreateInvite"))
span.End()
}()
// Cap expiry.
if expiresInHours > maxInviteExpiryHoursVal {
expiresInHours = maxInviteExpiryHoursVal
}
var expiresAt *time.Time
if expiresInHours > 0 {
t := time.Now().Add(time.Duration(expiresInHours) * time.Hour)
expiresAt = &t
}
code, err := s.st.CreateInvite(createdBy, maxUses, expiresAt)
if err != nil {
return nil, fmt.Errorf("%w: failed to create invite", ErrInternal)
}
invite, err := s.st.GetInvite(code)
if err != nil || invite == nil {
return nil, fmt.Errorf("%w: failed to retrieve invite", ErrInternal)
}
return invite, nil
}
// ListInvites returns all invites.
func (s *InviteService) ListInvites() ([]*db.Invite, error) {
invites, err := s.st.ListInvites()
if err != nil {
return nil, fmt.Errorf("%w: failed to list invites", ErrInternal)
}
return invites, nil
}
// RevokeInvite revokes an invite by code.
func (s *InviteService) RevokeInvite(code string) error {
invite, err := s.st.GetInvite(code)
if err != nil || invite == nil {
return fmt.Errorf("%w: invite not found", ErrNotFound)
}
if err := s.st.RevokeInvite(code); err != nil {
return fmt.Errorf("%w: failed to revoke invite", ErrInternal)
}
return nil
}