package web
import (
"context"
"sourcecraft.dev/bigbes/sr-ht-core/auth"
"sourcecraft.dev/bigbes/sr-ht-dolt/authn"
"sourcecraft.dev/bigbes/sr-ht-dolt/browse"
"sourcecraft.dev/bigbes/sr-ht-dolt/core"
"sourcecraft.dev/bigbes/sr-ht-dolt/db"
)
// This file holds the production adapters that satisfy the small interfaces in
// deps.go over the real db, browse and core-go layers. Tests do not use these;
// they inject their own fakes. Each adapter is a zero-value struct with a
// compile-time interface assertion so a signature drift in a committed package
// breaks the build here rather than at Phase-3 wiring time.
// DBAdapter satisfies RepoStore over the request-scoped db.Store. It holds no
// connection: every method builds a Store from the *sql.DB the core-go database
// middleware installed in ctx (db.FromContext), so one value serves all
// requests and leaks nothing.
type DBAdapter struct{}
var _ RepoStore = DBAdapter{}
func (DBAdapter) CreateRepo(ctx context.Context, r *core.Repo) (*core.Repo, error) {
return db.FromContext(ctx).CreateRepo(ctx, r)
}
func (DBAdapter) GetRepoByOwnerAndName(ctx context.Context, owner, name string) (*core.Repo, error) {
return db.FromContext(ctx).GetRepoByOwnerAndName(ctx, owner, name)
}
func (DBAdapter) ListReposByOwner(ctx context.Context, owner string, viewer *core.Caller) ([]*core.Repo, error) {
return db.FromContext(ctx).ListReposByOwner(ctx, owner, viewer)
}
func (DBAdapter) ListReposForDashboard(ctx context.Context, userID int) ([]*core.Repo, error) {
return db.FromContext(ctx).ListReposForDashboard(ctx, userID)
}
func (DBAdapter) UpdateRepo(ctx context.Context, id int, description string, visibility core.Visibility) error {
return db.FromContext(ctx).UpdateRepo(ctx, id, description, visibility)
}
func (DBAdapter) DeleteRepo(ctx context.Context, id int) error {
return db.FromContext(ctx).DeleteRepo(ctx, id)
}
func (DBAdapter) EffectiveAccess(ctx context.Context, userID, repoID int) (*core.AccessMode, error) {
return db.FromContext(ctx).EffectiveAccess(ctx, userID, repoID)
}
func (DBAdapter) ListACL(ctx context.Context, repoID int) ([]*db.ACLEntry, error) {
return db.FromContext(ctx).ListACL(ctx, repoID)
}
func (DBAdapter) UpsertACL(ctx context.Context, repoID, userID int, mode core.AccessMode) error {
return db.FromContext(ctx).UpsertACL(ctx, repoID, userID, mode)
}
func (DBAdapter) DeleteACL(ctx context.Context, repoID, userID int) error {
return db.FromContext(ctx).DeleteACL(ctx, repoID, userID)
}
func (DBAdapter) InsertKey(ctx context.Context, userID int, kid string, pubkey []byte, comment string) (*db.DoltKey, error) {
return db.FromContext(ctx).InsertKey(ctx, userID, kid, pubkey, comment)
}
func (DBAdapter) ListKeysByUser(ctx context.Context, userID int) ([]*db.DoltKey, error) {
return db.FromContext(ctx).ListKeysByUser(ctx, userID)
}
func (DBAdapter) DeleteKey(ctx context.Context, id, userID int) error {
return db.FromContext(ctx).DeleteKey(ctx, id, userID)
}
// BrowseAdapter satisfies BrowseOpener over browse.Open. The returned *browse.DB
// already implements every BrowseSession method plus Close.
type BrowseAdapter struct{}
var _ BrowseOpener = BrowseAdapter{}
func (BrowseAdapter) Open(ctx context.Context, diskPath string) (BrowseSession, error) {
dbh, err := browse.Open(ctx, diskPath)
if err != nil {
return nil, err
}
return dbh, nil
}
// MetaUserResolver satisfies UserResolver via core-go's auth.LookupUser, which
// mirrors the meta.sr.ht profile into the local user table and yields the
// account's UserID. Used to resolve an ACL grantee by username.
type MetaUserResolver struct{}
var _ UserResolver = MetaUserResolver{}
func (MetaUserResolver) LookupUser(ctx context.Context, username string) (*core.Caller, error) {
var ac auth.AuthContext
if err := auth.LookupUser(ctx, username, &ac); err != nil {
return nil, err
}
return authn.AsCoreCaller(&ac), nil
}