121 lines
2.9 KiB
Go
121 lines
2.9 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type UserLookup int
|
|
|
|
const (
|
|
UserLookupByID UserLookup = iota
|
|
UserLookupByBindCode
|
|
UserLookupByContactCode
|
|
)
|
|
|
|
const DatabaseSource = "database"
|
|
|
|
type Database interface {
|
|
Users() Users
|
|
Chats() Chats
|
|
Facts() Facts
|
|
Contacts() Contacts
|
|
Notifications() Notifications
|
|
Actions() Actions
|
|
Close() error
|
|
}
|
|
|
|
type Users interface {
|
|
Get(ctx context.Context, id uuid.UUID, lookup UserLookup) (*User, error)
|
|
Create(ctx context.Context, user *User) error
|
|
Update(ctx context.Context, user *User) error
|
|
Delete(ctx context.Context, id uuid.UUID) error
|
|
}
|
|
|
|
type Chats interface {
|
|
Attach(ctx context.Context, userID uuid.UUID, platform, identifier string) error
|
|
Detach(ctx context.Context, userID uuid.UUID, platform string) error
|
|
GetUserID(ctx context.Context, platform, identifier string) (uuid.UUID, error)
|
|
List(ctx context.Context, userID uuid.UUID) ([]Chat, error)
|
|
}
|
|
|
|
type Facts interface {
|
|
Add(ctx context.Context, userID uuid.UUID, value string) error
|
|
List(ctx context.Context, userID uuid.UUID) ([]Fact, error)
|
|
Delete(ctx context.Context, userID uuid.UUID, value string) error
|
|
}
|
|
|
|
type Contacts interface {
|
|
Add(ctx context.Context, contact *Contact) error
|
|
List(ctx context.Context, ownerID uuid.UUID) ([]Contact, error)
|
|
Delete(ctx context.Context, ownerID uuid.UUID, targetID uuid.UUID) error
|
|
}
|
|
|
|
type Notifications interface {
|
|
Push(ctx context.Context, n *Notification) error
|
|
Pop(ctx context.Context, limit int) ([]Notification, error)
|
|
List(ctx context.Context, userID uuid.UUID) ([]Notification, error)
|
|
ListOutgoing(ctx context.Context, initiatorID uuid.UUID) ([]Notification, error)
|
|
Delete(ctx context.Context, id uuid.UUID) error
|
|
}
|
|
|
|
type Actions interface {
|
|
Log(ctx context.Context, userID uuid.UUID, typ, content string) error
|
|
Recent(ctx context.Context, userID uuid.UUID, limit int) ([]Action, error)
|
|
}
|
|
|
|
type User struct {
|
|
ID uuid.UUID
|
|
Language string
|
|
Timezone string
|
|
PreferredChat string
|
|
LLMCount int
|
|
SearchCount int
|
|
NotificationCount int
|
|
CountUpdatedAt time.Time
|
|
BindCode uuid.UUID
|
|
ContactCode uuid.UUID
|
|
Role string
|
|
}
|
|
|
|
type Chat struct {
|
|
UserID uuid.UUID
|
|
Platform string // "telegram", "email", "whatsapp"
|
|
Identifier string // @username, email, phone
|
|
}
|
|
|
|
type Fact struct {
|
|
UserID uuid.UUID
|
|
Value string
|
|
}
|
|
|
|
type Contact struct {
|
|
OwnerID uuid.UUID // User who owns this contact
|
|
TargetID uuid.UUID // Target user ID
|
|
Name string // "mom", "brother", "Lena"
|
|
}
|
|
|
|
type Notification struct {
|
|
ID uuid.UUID
|
|
UserID uuid.UUID
|
|
InitiatorID uuid.UUID
|
|
ScheduledAt time.Time
|
|
Content string
|
|
RepeatOn string
|
|
}
|
|
|
|
type Action struct {
|
|
UserID uuid.UUID
|
|
ExecutedAt time.Time
|
|
Type string
|
|
Content string
|
|
}
|
|
|
|
var (
|
|
ErrNotFound = errors.New("entity not found")
|
|
ErrAlreadyExists = errors.New("entity already exists")
|
|
)
|