Files
2026-06-06 19:20:38 +03:00

197 lines
5.8 KiB
Go

package database
import (
"errors"
"io"
"time"
"github.com/google/uuid"
)
var (
ErrNotFound = errors.New("resource not found")
ErrAlreadyExists = errors.New("resource already exists")
)
type DB interface {
Users() Users
Subscriptions() Subscriptions
Files() Files
Email() Email
Chat() Chat
Posts() Posts
Reactions() Reactions
// Comments() Comments
Close() error
}
type Users interface {
Create(user User) error
Get(name string) (User, error)
Update(name string, user User) error
Delete(name string) error
CheckExists(name string) bool
}
type User struct {
Name string `json:"name" gob:"1"`
DisplayName string `json:"displayName" gob:"2"`
Bio string `json:"bio" gob:"3"`
PublicKey []byte `json:"publicKey" gob:"4"`
Avatar uuid.UUID `json:"avatar" gob:"5"`
Subscribers uint64 `json:"subscribers" gob:"-"`
}
type Subscriptions interface {
Subscribe(user, target, domain string, sig []byte) error
Unsubscribe(user, target, domain string) error
OfDomestic(user string, limit, offset int) ([]SubscriptionToForeign, error)
ToForeign(target, domain string, limit, offset int) ([]SubscriptionOfDomestic, error)
UpdateReference(owner, domain string, delta int) error
ListReferences(owner string) ([]SubscriptionReference, error)
}
type SubscriptionToForeign struct {
Target string `json:"target"`
Domain string `json:"domain"`
Signature []byte `json:"signature"`
}
type SubscriptionOfDomestic struct {
User string `json:"user"`
Signature []byte `json:"signature"`
}
type SubscriptionReference struct {
Domain string `json:"domain"`
Count uint64 `json:"count"`
}
type Files interface {
Put(user string, r io.Reader) (uuid.UUID, error)
Get(user string, id uuid.UUID, w io.Writer) error
Delete(user string, id uuid.UUID) error
Size(user string, id uuid.UUID) (int64, error)
}
type Email interface {
Put(user string, letter Letter) error
Get(user, correspondent string, date time.Time) (Letter, error)
List(user string, limit int, before time.Time) ([]Letter, error)
MarkSeen(user, correspondent string, date time.Time) error
Delete(user, correspondent string, date time.Time) error
}
type Letter struct {
From string `json:"from" gob:"1"`
To string `json:"to" gob:"2"`
Subject string `json:"subject" gob:"3"`
Date time.Time `json:"date" gob:"4"`
Body []byte `json:"body" gob:"-"`
Seen bool `json:"seen" gob:"5"`
Attachments []Attachment `json:"attachments" gob:"6"`
}
type Attachment struct {
ID uuid.UUID `json:"id" gob:"1"`
Filename string `json:"filename" gob:"2"`
Size int64 `json:"size" gob:"3"`
MimeType string `json:"mimeType" gob:"4"`
Body uuid.UUID `json:"body" gob:"5"`
}
type Chat interface {
Put(user string, msg Message) error
Dialogues(user string, limit int, after time.Time) ([]Dialogue, error)
Messages(user, contact string, limit int, after time.Time) ([]Message, error)
MarkSeen(user, contact string, date time.Time) error
Delete(user, contact string, date time.Time) error
}
type Dialogue struct {
Contact string `json:"contact"`
LastDate time.Time `json:"lastDate"`
LastContent []byte `json:"lastContent"`
UnreadCount int `json:"unreadCount"`
}
type Message struct {
From string `json:"from" gob:"1"`
To string `json:"to" gob:"2"`
Group []string `json:"group" gob:"3"`
Date time.Time `json:"date" gob:"-"`
Content []byte `json:"content" gob:"5"`
Attachments []Attachment `json:"attachments" gob:"-"`
Seen bool `json:"seen" gob:"4"`
Signature []byte `json:"signature" gob:"6"`
}
type Posts interface {
Create(post Post) error
Get(author string, date time.Time) (Post, error)
List(author string, limit, offset int) ([]Post, error)
Update(post Post) error
Delete(author string, date time.Time) error
}
type Post struct {
Author string `json:"author" gob:"1"`
Title string `json:"title" gob:"2"`
Body string `json:"body" gob:"-"`
Date time.Time `json:"date" gob:"-"`
Tags []string `json:"tags" gob:"3"`
Attachment []Attachment `json:"attachment" gob:"4"`
Signature []byte `json:"signature" gob:"6"`
}
type Reactions interface {
SetAllowed(author string, date time.Time, emojis []string) error
GetAllowed(author string, date time.Time) ([]string, error)
Add(user, author, domain string, date time.Time, emoji string, sig []byte) error
Remove(user, author, domain string, date time.Time, emoji string) error
OfUser(user string, limit, offset int) ([]ReactionToForeign, error)
ToContent(author, domain string, date time.Time, limit, offset int) ([]ReactionOfDomestic, error)
UpdateReference(owner string, date time.Time, domain, emoji string, delta int) error
ListReactions(owner string, date time.Time) ([]ReactionCounter, error)
}
type ReactionToForeign struct {
Domain string `json:"domain"`
Author string `json:"author"`
Date time.Time `json:"date"`
Emoji string `json:"emoji"`
}
type ReactionOfDomestic struct {
User string `json:"user"`
Emoji string `json:"emoji"`
Signature []byte `json:"signature"`
}
type ReactionCounter struct {
Emoji string `json:"emoji"`
Count uint64 `json:"count"`
}
type Comments interface {
Enable(author string, date time.Time) error
Disable(author string, date time.Time) error
}
type Comment struct {
Body string `json:"body" gob:"1"`
Signature []byte `json:"signature" gob:"2"`
}
// Next modules to support:
//
// Music() Music
// Reels() Reels
// Videos() Videos
// Streams() Streams
// Wallet() Wallet
// Ads() Ads