chore: init

This commit is contained in:
DevMiner 2024-08-11 03:51:22 +02:00
commit 320715f3e7
174 changed files with 42083 additions and 0 deletions

34
ent/schema/attachment.go Normal file
View file

@ -0,0 +1,34 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
type Attachment struct{ ent.Schema }
func (Attachment) Fields() []ent.Field {
return []ent.Field{
field.String("description").MaxLen(384),
field.Bytes("sha256"),
field.Int("size"),
field.String("blurhash").Optional().Nillable(),
field.Int("height").Optional().Nillable(),
field.Int("width").Optional().Nillable(),
field.Int("fps").Optional().Nillable(),
field.String("mimeType"),
}
}
func (Attachment) Edges() []ent.Edge {
return []ent.Edge{
edge.To("author", User.Type).Unique().Required(),
}
}
func (Attachment) Mixin() []ent.Mixin {
return []ent.Mixin{LysandEntityMixin{}}
}

33
ent/schema/follow.go Normal file
View file

@ -0,0 +1,33 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)
type Follow struct{ ent.Schema }
func (Follow) Fields() []ent.Field {
return []ent.Field{
field.Enum("status").Values("pending", "accepted").Default("pending"),
}
}
func (Follow) Edges() []ent.Edge {
return []ent.Edge{
edge.To("follower", User.Type).Unique().Required(),
edge.To("followee", User.Type).Unique().Required(),
}
}
func (Follow) Indexes() []ent.Index {
return []ent.Index{
index.Edges("follower", "followee").Unique(),
}
}
func (Follow) Mixin() []ent.Mixin {
return []ent.Mixin{LysandEntityMixin{}}
}

19
ent/schema/image.go Normal file
View file

@ -0,0 +1,19 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/field"
)
type Image struct{ ent.Schema }
func (Image) Fields() []ent.Field {
return []ent.Field{
field.String("url"),
field.String("mimeType"),
}
}
func (Image) Edges() []ent.Edge {
return nil
}

View file

@ -0,0 +1,40 @@
package schema
import (
"net/url"
"time"
"entgo.io/ent"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/mixin"
"github.com/google/uuid"
"github.com/lysand-org/versia-go/pkg/lysand"
)
type LysandEntityMixin struct{ mixin.Schema }
var _ ent.Mixin = (*LysandEntityMixin)(nil)
func (LysandEntityMixin) Fields() []ent.Field {
return []ent.Field{
field.UUID("id", uuid.UUID{}).
Default(uuid.New).
Immutable(),
field.Bool("isRemote"),
field.String("uri").Validate(ValidateURI),
field.JSON("extensions", lysand.Extensions{}).Default(lysand.Extensions{}),
field.Time("created_at").
Immutable().
Default(time.Now),
field.Time("updated_at").
Default(time.Now).
UpdateDefault(time.Now),
}
}
func ValidateURI(s string) error {
_, err := url.Parse(s)
return err
}

32
ent/schema/note.go Normal file
View file

@ -0,0 +1,32 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
)
type Note struct{ ent.Schema }
func (Note) Fields() []ent.Field {
return []ent.Field{
field.String("subject").MaxLen(384).Optional().Nillable(),
field.String("content"),
field.Bool("isSensitive").Default(false),
field.Enum("visibility").Values("public", "unlisted", "followers", "direct").Default("public"),
}
}
func (Note) Edges() []ent.Edge {
return []ent.Edge{
edge.To("author", User.Type).Unique().Required(),
edge.To("mentions", User.Type),
edge.To("attachments", Attachment.Type),
}
}
func (Note) Mixin() []ent.Mixin {
return []ent.Mixin{LysandEntityMixin{}}
}

View file

@ -0,0 +1,43 @@
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"entgo.io/ent/schema/index"
)
type ServerMetadata struct{ ent.Schema }
func (ServerMetadata) Fields() []ent.Field {
return []ent.Field{
field.String("name").
NotEmpty(),
field.String("description").
Optional().
Nillable(),
field.String("version").
NotEmpty(),
field.Strings("supportedExtensions").
Default([]string{}),
}
}
func (ServerMetadata) Edges() []ent.Edge {
return []ent.Edge{
edge.To("follower", User.Type).Unique().Required(),
edge.To("followee", User.Type).Unique().Required(),
}
}
func (ServerMetadata) Indexes() []ent.Index {
return []ent.Index{
index.Edges("follower", "followee").Unique(),
}
}
func (ServerMetadata) Mixin() []ent.Mixin {
return []ent.Mixin{LysandEntityMixin{}}
}

65
ent/schema/user.go Normal file
View file

@ -0,0 +1,65 @@
package schema
import (
"crypto/ed25519"
"errors"
"regexp"
"entgo.io/ent"
"entgo.io/ent/schema/edge"
"entgo.io/ent/schema/field"
"github.com/lysand-org/versia-go/pkg/lysand"
)
var (
ErrUsernameInvalid = errors.New("username must match ^[a-z0-9_-]+$")
usernameRegex = regexp.MustCompile("^[a-z0-9_-]+$")
)
type User struct{ ent.Schema }
func (User) Fields() []ent.Field {
return []ent.Field{
field.String("username").Unique().MaxLen(32).Validate(ValidateUsername),
field.Bytes("passwordHash").Optional().Nillable(),
field.String("displayName").MaxLen(256).Optional().Nillable(),
field.String("biography").Optional().Nillable(),
field.Bytes("publicKey").GoType(ed25519.PublicKey([]byte{})),
field.Bytes("privateKey").GoType(ed25519.PrivateKey([]byte{})).Optional(),
field.Bool("indexable").Default(true),
field.Enum("privacyLevel").Values("public", "restricted", "private").Default("public"),
field.JSON("fields", []lysand.Field{}).Default([]lysand.Field{}),
field.String("inbox").Validate(ValidateURI),
// Collections
field.String("featured").Validate(ValidateURI),
field.String("followers").Validate(ValidateURI),
field.String("following").Validate(ValidateURI),
field.String("outbox").Validate(ValidateURI),
}
}
func (User) Edges() []ent.Edge {
return []ent.Edge{
edge.To("avatarImage", Image.Type).Unique(),
edge.To("headerImage", Image.Type).Unique(),
edge.From("authoredNotes", Note.Type).Ref("author"),
edge.From("mentionedNotes", Note.Type).Ref("mentions"),
}
}
func (User) Mixin() []ent.Mixin { return []ent.Mixin{LysandEntityMixin{}} }
func ValidateUsername(username string) error {
if !usernameRegex.MatchString(username) {
return ErrUsernameInvalid
}
return nil
}