Files
m8sh/database/inteface.go
T

199 lines
5.9 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")
ErrInvalid = errors.New("invalid input")
ErrUnavailable = errors.New("service temporarily unavailable")
)
type DB interface {
Users() Users
Subscriptions() Subscriptions
Files() Files
Emails() Emails
Messages() Messages
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, targetEmail string, sig []byte) error
Unsubscribe(user, targetEmail string) error
OfUser(user string) ([]Subscription, error)
ToUser(targetEmail string) ([]Subscription, error)
UpdateReference(user, domain string, delta int) error
ListReferences(user string) ([]SubscriptionReference, error)
}
type Subscription struct {
Email string `json:"email"`
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 Emails interface {
Put(user string, letter Letter) (id uuid.UUID, err error)
Get(user string, id uuid.UUID) (Letter, error)
List(user string, limit, offset int) ([]Letter, error)
MarkSeen(user string, id uuid.UUID) error
Delete(user string, id uuid.UUID) error
}
type Letter struct {
ID uuid.UUID `json:"id" gob:"-"`
From string `json:"from" gob:"1"`
Subject string `json:"subject" gob:"2"`
Date time.Time `json:"date" gob:"-"`
Body []byte `json:"body" gob:"-"`
Seen bool `json:"seen" gob:"3"`
Attachments []Attachment `json:"attachments" gob:"4"`
}
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 Messages interface {
Put(msg Message) (id uuid.UUID, err error)
Get(id uuid.UUID) (Message, error)
List(user string, limit, offset int) ([]Message, error)
Delete(id uuid.UUID) error
}
type Message struct {
ID uuid.UUID `json:"id" gob:"-"`
From string `json:"from" gob:"1"`
To string `json:"to" gob:"2"`
Group []string `json:"group" gob:"3"`
Date time.Time `json:"date" gob:"-"`
Payload []byte `json:"payload" gob:"4"`
Signature []byte `json:"signature" gob:"5"`
}
type Posts interface {
Create(post Post) (id uuid.UUID, err error)
Get(id uuid.UUID) (Post, error)
List(user string, limit, offset int) ([]Post, error)
Update(id uuid.UUID, post Post) error
Delete(id uuid.UUID) error
}
type Post struct {
ID uuid.UUID `json:"id" gob:"-"`
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(typ string, id uuid.UUID, emojis []string) error
Allowed(typ string, id uuid.UUID) ([]string, error)
AddExternal(typ string, id uuid.UUID, domain, user, emoji string, sig []byte) error
RemoveExternal(typ string, id uuid.UUID, domain, user, emoji string) error
ToExternal(typ string, id uuid.UUID, domain string) ([]Reaction, error)
UpdateReference(typ string, id uuid.UUID, domain, emoji string, delta int) error
ListReactions(typ string, id uuid.UUID) ([]ReactionCounter, error)
}
type ReactionCounter struct {
Emoji string `json:"emoji"`
Count int `json:"count"`
}
type Reaction struct {
User string `json:"user"`
Emoji string `json:"emoji"`
Signature []byte `json:"signature"`
}
type Comments interface {
Enable(typ string, id uuid.UUID) error
Disable(typ string, id uuid.UUID) error
AddExternal(comment Comment) (uuid.UUID, error)
RemoveExternal(typ, user string, commentID, targetID uuid.UUID) error
UpdateReference(typ string, id uuid.UUID, lastAt time.Time, domain string, delta int) error
ListReferences(typ string, id uuid.UUID, limit, offset int) ([]CommentReference, error)
}
type Comment struct {
ID uuid.UUID `json:"id" gob:"-"`
Target CommentTarget `json:"target" gob:"1"`
Author string `json:"author" gob:"-"`
Domain string `json:"domain" gob:"2"`
Body string `json:"body" gob:"3"`
Date time.Time `json:"date" gob:"-"`
References []CommentReference `json:"referencess" gob:"-"`
Signature []byte `json:"signature" gob:"4"`
}
type CommentTarget struct {
Type string `json:"type" gob:"-"`
ID uuid.UUID `json:"id" gob:"-"`
}
type CommentReference struct {
Domain string `json:"domain"`
Count int `json:"count"`
LastAt time.Time `json:"lastAt"`
}
// Next modules to support:
//
// Music() Music
// Reels() Reels
// Videos() Videos
// Streams() Streams
// Wallet() Wallet
// Ads() Ads