refactor!: add missing fields and docs

This commit is contained in:
DevMiner 2024-08-22 23:03:38 +02:00
parent 61891d891a
commit 6e59386f60
73 changed files with 726 additions and 580 deletions

View file

@ -2,7 +2,8 @@ package api_schema
import (
"github.com/google/uuid"
"github.com/lysand-org/versia-go/pkg/lysand"
"github.com/lysand-org/versia-go/pkg/versia"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
)
type Note struct {
@ -12,7 +13,7 @@ type Note struct {
type FetchNoteResponse = APIResponse[Note]
type CreateNoteRequest struct {
Content string `json:"content" validate:"required,min=1,max=1024"`
Visibility lysand.PublicationVisibility `json:"visibility" validate:"required,oneof=public private direct"`
Mentions []lysand.URL `json:"mentions"`
Content string `json:"content" validate:"required,min=1,max=1024"`
Visibility versia.NoteVisibility `json:"visibility" validate:"required,oneof=public unlisted private direct"`
Mentions []versiautils.URL `json:"mentions"`
}

View file

@ -2,7 +2,7 @@ package api_schema
import (
"github.com/google/uuid"
"github.com/lysand-org/versia-go/pkg/lysand"
"github.com/lysand-org/versia-go/pkg/versia"
)
type User struct {
@ -10,7 +10,7 @@ type User struct {
Username string `json:"username"`
}
type LysandUser lysand.User
type LysandUser versia.User
type FetchUserResponse = APIResponse[User]
@ -23,3 +23,32 @@ type SearchUserRequest struct {
Username string `query:"username" validate:"required,username_regex,min=1,max=32"`
Domain *string `query:"domain" validate:"domain_regex"`
}
//var ErrInvalidUserMention = errors.New("invalid user mention")
//func (r *SearchUserRequest) UnmarshalJSON(raw []byte) error {
// var s string
// if err := json.Unmarshal(raw, &s); err != nil {
// return err
// }
//
// s = strings.TrimPrefix(s, "@")
// spl := strings.Split(s, "@")
//
// if len(spl) > 2 {
// return ErrInvalidUserMention
// }
//
// username := spl[0]
//
// var domain *string
// if len(spl) > 1 {
// domain = &spl[1]
// }
//
// *r = SearchUserRequest{
// Username: username,
// Domain: domain,
// }
//
// return nil
//}

View file

@ -2,15 +2,16 @@ package entity
import (
"github.com/lysand-org/versia-go/ent"
"github.com/lysand-org/versia-go/pkg/lysand"
"github.com/lysand-org/versia-go/pkg/versia"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
)
type Follow struct {
*ent.Follow
URI *lysand.URL
FollowerURI *lysand.URL
FolloweeURI *lysand.URL
URI *versiautils.URL
FollowerURI *versiautils.URL
FolloweeURI *versiautils.URL
}
func NewFollow(dbFollow *ent.Follow) (*Follow, error) {
@ -18,17 +19,17 @@ func NewFollow(dbFollow *ent.Follow) (*Follow, error) {
var err error
f.URI, err = lysand.ParseURL(dbFollow.URI)
f.URI, err = versiautils.ParseURL(dbFollow.URI)
if err != nil {
return nil, err
}
f.FollowerURI, err = lysand.ParseURL(dbFollow.Edges.Follower.URI)
f.FollowerURI, err = versiautils.ParseURL(dbFollow.Edges.Follower.URI)
if err != nil {
return nil, err
}
f.FolloweeURI, err = lysand.ParseURL(dbFollow.Edges.Followee.URI)
f.FolloweeURI, err = versiautils.ParseURL(dbFollow.Edges.Followee.URI)
if err != nil {
return nil, err
}
@ -36,12 +37,12 @@ func NewFollow(dbFollow *ent.Follow) (*Follow, error) {
return f, nil
}
func (f Follow) ToLysand() *lysand.Follow {
return &lysand.Follow{
Entity: lysand.Entity{
func (f Follow) ToLysand() *versia.Follow {
return &versia.Follow{
Entity: versia.Entity{
ID: f.ID,
URI: f.URI,
CreatedAt: lysand.TimeFromStd(f.CreatedAt),
CreatedAt: versiautils.Time(f.CreatedAt),
Extensions: f.Extensions,
},
Author: f.FollowerURI,
@ -49,12 +50,12 @@ func (f Follow) ToLysand() *lysand.Follow {
}
}
func (f Follow) ToLysandAccept() *lysand.FollowAccept {
return &lysand.FollowAccept{
Entity: lysand.Entity{
func (f Follow) ToLysandAccept() *versia.FollowAccept {
return &versia.FollowAccept{
Entity: versia.Entity{
ID: f.ID,
URI: f.URI,
CreatedAt: lysand.TimeFromStd(f.CreatedAt),
CreatedAt: versiautils.Time(f.CreatedAt),
Extensions: f.Extensions,
},
Author: f.FolloweeURI,
@ -62,12 +63,12 @@ func (f Follow) ToLysandAccept() *lysand.FollowAccept {
}
}
func (f Follow) ToLysandReject() *lysand.FollowReject {
return &lysand.FollowReject{
Entity: lysand.Entity{
func (f Follow) ToLysandReject() *versia.FollowReject {
return &versia.FollowReject{
Entity: versia.Entity{
ID: f.ID,
URI: f.URI,
CreatedAt: lysand.TimeFromStd(f.CreatedAt),
CreatedAt: versiautils.Time(f.CreatedAt),
Extensions: f.Extensions,
},
Author: f.FolloweeURI,

View file

@ -2,30 +2,31 @@ package entity
import (
"github.com/lysand-org/versia-go/ent"
"github.com/lysand-org/versia-go/pkg/lysand"
"github.com/lysand-org/versia-go/pkg/versia"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
)
type Note struct {
*ent.Note
URI *lysand.URL
Content lysand.TextContentTypeMap
URI *versiautils.URL
Content versiautils.TextContentTypeMap
Author *User
Mentions []User
MentionURIs []lysand.URL
MentionURIs []versiautils.URL
}
func NewNote(dbNote *ent.Note) (*Note, error) {
n := &Note{
Note: dbNote,
Content: lysand.TextContentTypeMap{
"text/plain": lysand.TextContent{Content: dbNote.Content},
Content: versiautils.TextContentTypeMap{
"text/plain": versiautils.TextContent{Content: dbNote.Content},
},
Mentions: make([]User, 0, len(dbNote.Edges.Mentions)),
MentionURIs: make([]lysand.URL, 0, len(dbNote.Edges.Mentions)),
MentionURIs: make([]versiautils.URL, 0, len(dbNote.Edges.Mentions)),
}
var err error
if n.URI, err = lysand.ParseURL(dbNote.URI); err != nil {
if n.URI, err = versiautils.ParseURL(dbNote.URI); err != nil {
return nil, err
}
if n.Author, err = NewUser(dbNote.Edges.Author); err != nil {
@ -45,12 +46,12 @@ func NewNote(dbNote *ent.Note) (*Note, error) {
return n, nil
}
func (n Note) ToLysand() lysand.Note {
return lysand.Note{
Entity: lysand.Entity{
func (n Note) ToLysand() versia.Note {
return versia.Note{
Entity: versia.Entity{
ID: n.ID,
URI: n.URI,
CreatedAt: lysand.TimeFromStd(n.CreatedAt),
CreatedAt: versiautils.Time(n.CreatedAt),
Extensions: n.Extensions,
},
Author: n.Author.URI,
@ -65,6 +66,6 @@ func (n Note) ToLysand() lysand.Note {
Mentions: n.MentionURIs,
Subject: n.Subject,
IsSensitive: &n.IsSensitive,
Visibility: lysand.PublicationVisibility(n.Visibility),
Visibility: versia.NoteVisibility(n.Visibility),
}
}

View file

@ -2,31 +2,32 @@ package entity
import (
"github.com/lysand-org/versia-go/ent"
"github.com/lysand-org/versia-go/pkg/lysand"
versiacrypto "github.com/lysand-org/versia-go/pkg/lysand/crypto"
"github.com/lysand-org/versia-go/pkg/versia"
versiacrypto "github.com/lysand-org/versia-go/pkg/versia/crypto"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
)
type InstanceMetadata struct {
*ent.InstanceMetadata
Moderators []User
ModeratorsCollection *lysand.URL
ModeratorsCollection *versiautils.URL
Admins []User
AdminsCollection *lysand.URL
AdminsCollection *versiautils.URL
SharedInbox *lysand.URL
SharedInbox *versiautils.URL
PublicKey *lysand.SPKIPublicKey
PublicKey *versiacrypto.SPKIPublicKey
Logo *lysand.ImageContentTypeMap
Banner *lysand.ImageContentTypeMap
Logo *versiautils.ImageContentTypeMap
Banner *versiautils.ImageContentTypeMap
}
func NewInstanceMetadata(dbData *ent.InstanceMetadata) (*InstanceMetadata, error) {
n := &InstanceMetadata{
InstanceMetadata: dbData,
PublicKey: &lysand.SPKIPublicKey{},
PublicKey: &versiacrypto.SPKIPublicKey{},
}
var err error
@ -34,16 +35,16 @@ func NewInstanceMetadata(dbData *ent.InstanceMetadata) (*InstanceMetadata, error
return nil, err
}
if n.SharedInbox, err = lysand.ParseURL(dbData.SharedInboxURI); err != nil {
if n.SharedInbox, err = versiautils.ParseURL(dbData.SharedInboxURI); err != nil {
return nil, err
}
if dbData.ModeratorsURI != nil {
if n.ModeratorsCollection, err = lysand.ParseURL(*dbData.ModeratorsURI); err != nil {
if n.ModeratorsCollection, err = versiautils.ParseURL(*dbData.ModeratorsURI); err != nil {
return nil, err
}
}
if dbData.AdminsURI != nil {
if n.AdminsCollection, err = lysand.ParseURL(*dbData.AdminsURI); err != nil {
if n.AdminsCollection, err = versiautils.ParseURL(*dbData.AdminsURI); err != nil {
return nil, err
}
}
@ -69,8 +70,8 @@ func NewInstanceMetadata(dbData *ent.InstanceMetadata) (*InstanceMetadata, error
return n, nil
}
func (m InstanceMetadata) ToLysand() lysand.InstanceMetadata {
return lysand.InstanceMetadata{
func (m InstanceMetadata) ToLysand() versia.InstanceMetadata {
return versia.InstanceMetadata{
Extensions: m.Extensions,
Name: m.Name,
Description: m.Description,
@ -80,15 +81,15 @@ func (m InstanceMetadata) ToLysand() lysand.InstanceMetadata {
Admins: m.AdminsCollection,
Logo: m.Logo,
Banner: m.Banner,
PublicKey: lysand.InstancePublicKey{
PublicKey: versia.InstancePublicKey{
Algorithm: m.PublicKeyAlgorithm,
Key: m.PublicKey,
},
Software: lysand.InstanceSoftware{
Software: versia.InstanceSoftware{
Name: m.SoftwareName,
Version: m.SoftwareVersion,
},
Compatibility: lysand.InstanceCompatibility{
Compatibility: versia.InstanceCompatibility{
Versions: m.SupportedVersions,
Extensions: m.SupportedExtensions,
},

View file

@ -2,36 +2,37 @@ package entity
import (
"github.com/lysand-org/versia-go/internal/helpers"
versiacrypto "github.com/lysand-org/versia-go/pkg/lysand/crypto"
"github.com/lysand-org/versia-go/pkg/versia"
versiacrypto "github.com/lysand-org/versia-go/pkg/versia/crypto"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
"net/url"
"github.com/lysand-org/versia-go/ent"
"github.com/lysand-org/versia-go/internal/utils"
"github.com/lysand-org/versia-go/pkg/lysand"
)
type User struct {
*ent.User
URI *lysand.URL
PKActorURI *lysand.URL
PublicKey *lysand.SPKIPublicKey
Inbox *lysand.URL
Outbox *lysand.URL
Featured *lysand.URL
Followers *lysand.URL
Following *lysand.URL
URI *versiautils.URL
PKActorURI *versiautils.URL
PublicKey *versiacrypto.SPKIPublicKey
Inbox *versiautils.URL
Outbox *versiautils.URL
Featured *versiautils.URL
Followers *versiautils.URL
Following *versiautils.URL
DisplayName string
LysandAvatar lysand.ImageContentTypeMap
LysandBiography lysand.TextContentTypeMap
Signer lysand.Signer
LysandAvatar versiautils.ImageContentTypeMap
LysandBiography versiautils.TextContentTypeMap
Signer versiacrypto.Signer
}
func NewUser(dbData *ent.User) (*User, error) {
u := &User{
User: dbData,
PublicKey: &lysand.SPKIPublicKey{
PublicKey: &versiacrypto.SPKIPublicKey{
Key: nil,
Algorithm: dbData.PublicKeyAlgorithm,
},
@ -50,29 +51,29 @@ func NewUser(dbData *ent.User) (*User, error) {
return nil, err
}
if u.URI, err = lysand.ParseURL(dbData.URI); err != nil {
if u.URI, err = versiautils.ParseURL(dbData.URI); err != nil {
return nil, err
}
if u.PKActorURI, err = lysand.ParseURL(dbData.PublicKeyActor); err != nil {
if u.PKActorURI, err = versiautils.ParseURL(dbData.PublicKeyActor); err != nil {
return nil, err
}
if u.Inbox, err = lysand.ParseURL(dbData.Inbox); err != nil {
if u.Inbox, err = versiautils.ParseURL(dbData.Inbox); err != nil {
return nil, err
}
if u.Outbox, err = lysand.ParseURL(dbData.Outbox); err != nil {
if u.Outbox, err = versiautils.ParseURL(dbData.Outbox); err != nil {
return nil, err
}
if u.Featured, err = lysand.ParseURL(dbData.Featured); err != nil {
if u.Featured, err = versiautils.ParseURL(dbData.Featured); err != nil {
return nil, err
}
if u.Followers, err = lysand.ParseURL(dbData.Followers); err != nil {
if u.Followers, err = versiautils.ParseURL(dbData.Followers); err != nil {
return nil, err
}
if u.Following, err = lysand.ParseURL(dbData.Following); err != nil {
if u.Following, err = versiautils.ParseURL(dbData.Following); err != nil {
return nil, err
}
u.Signer = lysand.Signer{
u.Signer = versiacrypto.Signer{
PrivateKey: dbData.PrivateKey,
UserURL: u.URI.ToStd(),
}
@ -80,12 +81,12 @@ func NewUser(dbData *ent.User) (*User, error) {
return u, nil
}
func (u User) ToLysand() *lysand.User {
return &lysand.User{
Entity: lysand.Entity{
func (u User) ToLysand() *versia.User {
return &versia.User{
Entity: versia.Entity{
ID: u.ID,
URI: u.URI,
CreatedAt: lysand.TimeFromStd(u.CreatedAt),
CreatedAt: versiautils.Time(u.CreatedAt),
Extensions: u.Extensions,
},
DisplayName: helpers.StringPtr(u.DisplayName),
@ -93,7 +94,7 @@ func (u User) ToLysand() *lysand.User {
Avatar: u.LysandAvatar,
Header: imageMap(u.Edges.HeaderImage),
Indexable: u.Indexable,
PublicKey: lysand.UserPublicKey{
PublicKey: versia.UserPublicKey{
Actor: u.PKActorURI,
Algorithm: u.PublicKeyAlgorithm,
Key: u.PublicKey,
@ -109,33 +110,33 @@ func (u User) ToLysand() *lysand.User {
}
}
func lysandAvatar(u *ent.User) lysand.ImageContentTypeMap {
func lysandAvatar(u *ent.User) versiautils.ImageContentTypeMap {
if avatar := imageMap(u.Edges.AvatarImage); avatar != nil {
return avatar
}
return lysand.ImageContentTypeMap{
"image/svg+xml": lysand.ImageContent{
return versiautils.ImageContentTypeMap{
"image/svg+xml": versiautils.ImageContent{
Content: utils.DefaultAvatarURL(u.ID),
},
}
}
func lysandBiography(u *ent.User) lysand.TextContentTypeMap {
func lysandBiography(u *ent.User) versiautils.TextContentTypeMap {
if u.Biography == nil {
return nil
}
// TODO: Render HTML
return lysand.TextContentTypeMap{
"text/html": lysand.TextContent{
return versiautils.TextContentTypeMap{
"text/html": versiautils.TextContent{
Content: *u.Biography,
},
}
}
func imageMap(i *ent.Image) lysand.ImageContentTypeMap {
func imageMap(i *ent.Image) versiautils.ImageContentTypeMap {
if i == nil {
return nil
}
@ -145,9 +146,9 @@ func imageMap(i *ent.Image) lysand.ImageContentTypeMap {
return nil
}
return lysand.ImageContentTypeMap{
return versiautils.ImageContentTypeMap{
i.MimeType: {
Content: (*lysand.URL)(u),
Content: (*versiautils.URL)(u),
},
}
}

View file

@ -5,11 +5,11 @@ import (
"errors"
"fmt"
"github.com/lysand-org/versia-go/internal/validators/val_impls"
"github.com/lysand-org/versia-go/pkg/versia"
"github.com/gofiber/fiber/v2"
"github.com/google/uuid"
"github.com/lysand-org/versia-go/internal/api_schema"
"github.com/lysand-org/versia-go/pkg/lysand"
)
func (i *Handler) LysandInbox(c *fiber.Ctx) error {
@ -29,13 +29,13 @@ func (i *Handler) LysandInbox(c *fiber.Ctx) error {
return api_schema.ErrBadRequest(nil)
}
obj, err := lysand.ParseInboxObject(raw)
obj, err := versia.ParseInboxObject(raw)
if err != nil {
i.log.Error(err, "Failed to parse inbox object")
if errors.Is(err, lysand.ErrUnknownType{}) {
if errors.Is(err, versia.UnknownEntityTypeError{}) {
return api_schema.ErrNotFound(map[string]any{
"error": fmt.Sprintf("Unknown object type: %s", err.(lysand.ErrUnknownType).Type),
"error": fmt.Sprintf("Unknown object type: %s", err.(versia.UnknownEntityTypeError).Type),
})
}

View file

@ -9,7 +9,7 @@ import (
"github.com/lysand-org/versia-go/internal/entity"
"github.com/lysand-org/versia-go/internal/repository"
"github.com/lysand-org/versia-go/internal/service"
"github.com/lysand-org/versia-go/pkg/lysand"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
)
var _ repository.InstanceMetadataRepository = (*InstanceMetadataRepositoryImpl)(nil)
@ -50,7 +50,7 @@ func (i *InstanceMetadataRepositoryImpl) GetByHost(ctx context.Context, host str
return entity.NewInstanceMetadata(m)
}
func (i *InstanceMetadataRepositoryImpl) ImportFromLysandByURI(ctx context.Context, uri *lysand.URL) (*entity.InstanceMetadata, error) {
func (i *InstanceMetadataRepositoryImpl) ImportFromLysandByURI(ctx context.Context, uri *versiautils.URL) (*entity.InstanceMetadata, error) {
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/InstanceMetadataRepositoryImpl.ImportFromLysandByURI").
AddAttribute("uri", uri.String())
defer s.End()

View file

@ -3,6 +3,7 @@ package repo_impls
import (
"context"
"github.com/lysand-org/versia-go/internal/repository"
"github.com/lysand-org/versia-go/pkg/versia"
"git.devminer.xyz/devminer/unitel"
"github.com/go-logr/logr"
@ -11,7 +12,6 @@ import (
"github.com/lysand-org/versia-go/ent/note"
"github.com/lysand-org/versia-go/internal/entity"
"github.com/lysand-org/versia-go/internal/utils"
"github.com/lysand-org/versia-go/pkg/lysand"
)
var _ repository.NoteRepository = (*NoteRepositoryImpl)(nil)
@ -62,7 +62,7 @@ func (i *NoteRepositoryImpl) NewNote(ctx context.Context, author *entity.User, c
return entity.NewNote(n)
}
func (i *NoteRepositoryImpl) ImportLysandNote(ctx context.Context, lNote *lysand.Note) (*entity.Note, error) {
func (i *NoteRepositoryImpl) ImportLysandNote(ctx context.Context, lNote *versia.Note) (*entity.Note, error) {
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/NoteRepositoryImpl.ImportLysandNote")
defer s.End()
ctx = s.Context()

View file

@ -7,6 +7,7 @@ import (
"github.com/lysand-org/versia-go/config"
"github.com/lysand-org/versia-go/internal/repository"
"github.com/lysand-org/versia-go/internal/service"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
"golang.org/x/crypto/bcrypt"
"git.devminer.xyz/devminer/unitel"
@ -17,7 +18,6 @@ import (
"github.com/lysand-org/versia-go/ent/user"
"github.com/lysand-org/versia-go/internal/entity"
"github.com/lysand-org/versia-go/internal/utils"
"github.com/lysand-org/versia-go/pkg/lysand"
)
const bcryptCost = 12
@ -84,7 +84,7 @@ func (i *UserRepositoryImpl) NewUser(ctx context.Context, username, password str
return entity.NewUser(u)
}
func (i *UserRepositoryImpl) ImportLysandUserByURI(ctx context.Context, uri *lysand.URL) (*entity.User, error) {
func (i *UserRepositoryImpl) ImportLysandUserByURI(ctx context.Context, uri *versiautils.URL) (*entity.User, error) {
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.ImportLysandUserByURI")
defer s.End()
ctx = s.Context()
@ -163,7 +163,7 @@ func (i *UserRepositoryImpl) Discover(ctx context.Context, domain, username stri
l.V(2).Info("Found remote user", "userURI", wf.URI)
u, err := i.Resolve(ctx, lysand.URLFromStd(wf.URI))
u, err := i.Resolve(ctx, versiautils.URLFromStd(wf.URI))
if err != nil {
l.Error(err, "Failed to resolve user")
return nil, err
@ -185,7 +185,7 @@ func (i *UserRepositoryImpl) Discover(ctx context.Context, domain, username stri
return u, nil
}
func (i *UserRepositoryImpl) Resolve(ctx context.Context, uri *lysand.URL) (*entity.User, error) {
func (i *UserRepositoryImpl) Resolve(ctx context.Context, uri *versiautils.URL) (*entity.User, error) {
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.Resolve")
defer s.End()
ctx = s.Context()
@ -215,7 +215,7 @@ func (i *UserRepositoryImpl) Resolve(ctx context.Context, uri *lysand.URL) (*ent
return u, nil
}
func (i *UserRepositoryImpl) ResolveMultiple(ctx context.Context, uris []lysand.URL) ([]*entity.User, error) {
func (i *UserRepositoryImpl) ResolveMultiple(ctx context.Context, uris []versiautils.URL) ([]*entity.User, error) {
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.ResolveMultiple")
defer s.End()
ctx = s.Context()
@ -340,7 +340,7 @@ func (i *UserRepositoryImpl) GetLocalByUsername(ctx context.Context, username st
return entity.NewUser(u)
}
func (i *UserRepositoryImpl) LookupByURI(ctx context.Context, uri *lysand.URL) (*entity.User, error) {
func (i *UserRepositoryImpl) LookupByURI(ctx context.Context, uri *versiautils.URL) (*entity.User, error) {
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.LookupByURI")
defer s.End()
ctx = s.Context()
@ -367,7 +367,7 @@ func (i *UserRepositoryImpl) LookupByURI(ctx context.Context, uri *lysand.URL) (
return entity.NewUser(u)
}
func (i *UserRepositoryImpl) LookupByURIs(ctx context.Context, uris []lysand.URL) ([]*entity.User, error) {
func (i *UserRepositoryImpl) LookupByURIs(ctx context.Context, uris []versiautils.URL) ([]*entity.User, error) {
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.LookupByURIs")
defer s.End()
ctx = s.Context()

View file

@ -3,15 +3,16 @@ package repository
import (
"context"
"crypto/ed25519"
"github.com/lysand-org/versia-go/pkg/versia"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
"github.com/google/uuid"
"github.com/lysand-org/versia-go/internal/entity"
"github.com/lysand-org/versia-go/pkg/lysand"
)
type UserRepository interface {
NewUser(ctx context.Context, username, password string, privateKey ed25519.PrivateKey, publicKey ed25519.PublicKey) (*entity.User, error)
ImportLysandUserByURI(ctx context.Context, uri *lysand.URL) (*entity.User, error)
ImportLysandUserByURI(ctx context.Context, uri *versiautils.URL) (*entity.User, error)
GetByID(ctx context.Context, id uuid.UUID) (*entity.User, error)
GetLocalByID(ctx context.Context, id uuid.UUID) (*entity.User, error)
@ -19,11 +20,11 @@ type UserRepository interface {
Discover(ctx context.Context, host, username string) (*entity.User, error)
Resolve(ctx context.Context, uri *lysand.URL) (*entity.User, error)
ResolveMultiple(ctx context.Context, uris []lysand.URL) ([]*entity.User, error)
Resolve(ctx context.Context, uri *versiautils.URL) (*entity.User, error)
ResolveMultiple(ctx context.Context, uris []versiautils.URL) ([]*entity.User, error)
LookupByURI(ctx context.Context, uri *lysand.URL) (*entity.User, error)
LookupByURIs(ctx context.Context, uris []lysand.URL) ([]*entity.User, error)
LookupByURI(ctx context.Context, uri *versiautils.URL) (*entity.User, error)
LookupByURIs(ctx context.Context, uris []versiautils.URL) ([]*entity.User, error)
LookupByIDOrUsername(ctx context.Context, idOrUsername string) (*entity.User, error)
}
@ -38,14 +39,14 @@ type FollowRepository interface {
type NoteRepository interface {
NewNote(ctx context.Context, author *entity.User, content string, mentions []*entity.User) (*entity.Note, error)
ImportLysandNote(ctx context.Context, lNote *lysand.Note) (*entity.Note, error)
ImportLysandNote(ctx context.Context, lNote *versia.Note) (*entity.Note, error)
GetByID(ctx context.Context, idOrUsername uuid.UUID) (*entity.Note, error)
}
type InstanceMetadataRepository interface {
GetByHost(ctx context.Context, host string) (*entity.InstanceMetadata, error)
ImportFromLysandByURI(ctx context.Context, uri *lysand.URL) (*entity.InstanceMetadata, error)
ImportFromLysandByURI(ctx context.Context, uri *versiautils.URL) (*entity.InstanceMetadata, error)
}
type Manager interface {

View file

@ -4,11 +4,13 @@ import (
"context"
"github.com/gofiber/fiber/v2"
"github.com/lysand-org/versia-go/internal/repository"
"github.com/lysand-org/versia-go/pkg/versia"
versiacrypto "github.com/lysand-org/versia-go/pkg/versia/crypto"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
"github.com/google/uuid"
"github.com/lysand-org/versia-go/internal/api_schema"
"github.com/lysand-org/versia-go/internal/entity"
"github.com/lysand-org/versia-go/pkg/lysand"
"github.com/lysand-org/versia-go/pkg/webfinger"
)
@ -26,10 +28,10 @@ type UserService interface {
type FederationService interface {
SendToInbox(ctx context.Context, author *entity.User, target *entity.User, object any) ([]byte, error)
GetUser(ctx context.Context, uri *lysand.URL) (*lysand.User, error)
GetUser(ctx context.Context, uri *versiautils.URL) (*versia.User, error)
DiscoverUser(ctx context.Context, baseURL, username string) (*webfinger.User, error)
DiscoverInstance(ctx context.Context, baseURL string) (*lysand.InstanceMetadata, error)
DiscoverInstance(ctx context.Context, baseURL string) (*versia.InstanceMetadata, error)
}
type InboxService interface {
@ -40,14 +42,14 @@ type NoteService interface {
CreateNote(ctx context.Context, req api_schema.CreateNoteRequest) (*entity.Note, error)
GetNote(ctx context.Context, id uuid.UUID) (*entity.Note, error)
ImportLysandNote(ctx context.Context, lNote *lysand.Note) (*entity.Note, error)
ImportLysandNote(ctx context.Context, lNote *versia.Note) (*entity.Note, error)
}
type FollowService interface {
NewFollow(ctx context.Context, follower, followee *entity.User) (*entity.Follow, error)
GetFollow(ctx context.Context, id uuid.UUID) (*entity.Follow, error)
ImportLysandFollow(ctx context.Context, lFollow *lysand.Follow) (*entity.Follow, error)
ImportLysandFollow(ctx context.Context, lFollow *versia.Follow) (*entity.Follow, error)
}
type InstanceMetadataService interface {
@ -59,5 +61,5 @@ type TaskService interface {
}
type RequestSigner interface {
Sign(c *fiber.Ctx, signer lysand.Signer, body any) error
Sign(c *fiber.Ctx, signer versiacrypto.Signer, body any) error
}

View file

@ -5,6 +5,7 @@ import (
"github.com/google/uuid"
"github.com/lysand-org/versia-go/internal/repository"
"github.com/lysand-org/versia-go/internal/service"
"github.com/lysand-org/versia-go/pkg/versia"
"git.devminer.xyz/devminer/unitel"
"github.com/go-logr/logr"
@ -12,7 +13,6 @@ import (
"github.com/lysand-org/versia-go/ent/user"
"github.com/lysand-org/versia-go/internal/api_schema"
"github.com/lysand-org/versia-go/internal/entity"
"github.com/lysand-org/versia-go/pkg/lysand"
)
var _ service.InboxService = (*InboxServiceImpl)(nil)
@ -63,22 +63,17 @@ func (i InboxServiceImpl) Handle(ctx context.Context, obj any, userId uuid.UUID)
// TODO: Implement more types
switch o := obj.(type) {
case lysand.Note:
case versia.Note:
i.log.Info("Received note", "note", o)
if err := i.handleNote(ctx, o, u); err != nil {
i.log.Error(err, "Failed to handle note", "note", o)
return err
}
case lysand.Patch:
i.log.Info("Received patch", "patch", o)
case lysand.Follow:
case versia.Follow:
if err := i.handleFollow(ctx, o, u); err != nil {
i.log.Error(err, "Failed to handle follow", "follow", o)
return err
}
case lysand.Undo:
i.log.Info("Received undo", "undo", o)
default:
i.log.Info("Unimplemented object type", "object", obj)
return api_schema.ErrNotImplemented(nil)
@ -88,7 +83,7 @@ func (i InboxServiceImpl) Handle(ctx context.Context, obj any, userId uuid.UUID)
})
}
func (i InboxServiceImpl) handleFollow(ctx context.Context, o lysand.Follow, u *entity.User) error {
func (i InboxServiceImpl) handleFollow(ctx context.Context, o versia.Follow, u *entity.User) error {
s := i.telemetry.StartSpan(ctx, "function", "svc_impls/InboxServiceImpl.handleFollow")
defer s.End()
ctx = s.Context()
@ -130,7 +125,7 @@ func (i InboxServiceImpl) handleFollow(ctx context.Context, o lysand.Follow, u *
return nil
}
func (i InboxServiceImpl) handleNote(ctx context.Context, o lysand.Note, u *entity.User) error {
func (i InboxServiceImpl) handleNote(ctx context.Context, o versia.Note, u *entity.User) error {
s := i.telemetry.StartSpan(ctx, "function", "svc_impls/InboxServiceImpl.handleNote")
defer s.End()
ctx = s.Context()

View file

@ -12,9 +12,10 @@ import (
"github.com/go-logr/logr"
"github.com/lysand-org/versia-go/internal/entity"
"github.com/lysand-org/versia-go/internal/service"
"github.com/lysand-org/versia-go/pkg/lysand"
versiacrypto "github.com/lysand-org/versia-go/pkg/lysand/crypto"
"github.com/lysand-org/versia-go/pkg/protoretry"
"github.com/lysand-org/versia-go/pkg/versia"
versiacrypto "github.com/lysand-org/versia-go/pkg/versia/crypto"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
"github.com/lysand-org/versia-go/pkg/webfinger"
"net/http"
"net/url"
@ -29,14 +30,14 @@ var (
type FederationServiceImpl struct {
httpC *protoretry.Client
federationClient *lysand.FederationClient
federationClient *versia.FederationClient
telemetry *unitel.Telemetry
log logr.Logger
}
func NewFederationServiceImpl(httpClient *http.Client, federationClient *lysand.FederationClient, telemetry *unitel.Telemetry, log logr.Logger) *FederationServiceImpl {
func NewFederationServiceImpl(httpClient *http.Client, federationClient *versia.FederationClient, telemetry *unitel.Telemetry, log logr.Logger) *FederationServiceImpl {
return &FederationServiceImpl{
httpC: protoretry.New(httpClient),
federationClient: federationClient,
@ -45,7 +46,7 @@ func NewFederationServiceImpl(httpClient *http.Client, federationClient *lysand.
}
}
func (i *FederationServiceImpl) GetUser(ctx context.Context, uri *lysand.URL) (*lysand.User, error) {
func (i *FederationServiceImpl) GetUser(ctx context.Context, uri *versiautils.URL) (*versia.User, error) {
s := i.telemetry.StartSpan(ctx, "function", "svc_impls/FederationServiceImpl.GetUser").
AddAttribute("userURI", uri.String())
defer s.End()
@ -57,19 +58,19 @@ func (i *FederationServiceImpl) GetUser(ctx context.Context, uri *lysand.URL) (*
return nil, err
}
u := &lysand.User{}
u := &versia.User{}
if err := json.Unmarshal(body, u); err != nil {
s.SetSimpleStatus(unitel.Error, err.Error())
return nil, err
}
fedHeaders, err := lysand.ExtractFederationHeaders(resp.Header)
fedHeaders, err := versiacrypto.ExtractFederationHeaders(resp.Header)
if err != nil {
s.SetSimpleStatus(unitel.Error, err.Error())
return nil, err
}
v := lysand.Verifier{PublicKey: u.PublicKey.Key.Key}
v := versiacrypto.Verifier{PublicKey: u.PublicKey.Key.Key}
if !v.Verify("GET", uri.ToStd(), body, fedHeaders) {
s.SetSimpleStatus(unitel.Error, ErrSignatureValidationFailed.Error())
i.log.V(1).Error(ErrSignatureValidationFailed, "signature validation failed", "user", u.URI.String())
@ -100,7 +101,7 @@ func (i *FederationServiceImpl) DiscoverUser(ctx context.Context, baseURL, usern
return wf, nil
}
func (i *FederationServiceImpl) DiscoverInstance(ctx context.Context, baseURL string) (*lysand.InstanceMetadata, error) {
func (i *FederationServiceImpl) DiscoverInstance(ctx context.Context, baseURL string) (*versia.InstanceMetadata, error) {
s := i.telemetry.StartSpan(ctx, "function", "svc_impls/FederationServiceImpl.DiscoverInstance").
AddAttribute("baseURL", baseURL)
defer s.End()
@ -112,10 +113,10 @@ func (i *FederationServiceImpl) DiscoverInstance(ctx context.Context, baseURL st
return nil, err
} else if resp.StatusCode >= http.StatusBadRequest {
s.SetSimpleStatus(unitel.Error, fmt.Sprintf("unexpected response code: %d", resp.StatusCode))
return nil, &lysand.ResponseError{StatusCode: resp.StatusCode, URL: resp.Request.URL}
return nil, &versia.ResponseError{StatusCode: resp.StatusCode, URL: resp.Request.URL}
}
var metadata lysand.InstanceMetadata
var metadata versia.InstanceMetadata
if err := json.Unmarshal(body, &metadata); err != nil {
s.SetSimpleStatus(unitel.Error, err.Error())
return nil, err
@ -150,7 +151,7 @@ func (i *FederationServiceImpl) SendToInbox(ctx context.Context, author *entity.
return nil, err
}
sigData := lysand.NewSignatureData("POST", base64.StdEncoding.EncodeToString(nonce), uri, versiacrypto.SHA256(body))
sigData := versiacrypto.NewSignatureData("POST", base64.StdEncoding.EncodeToString(nonce), uri, versiacrypto.SHA256(body))
sig := author.Signer.Sign(*sigData)
req, err := http.NewRequestWithContext(ctx, "POST", uri.String(), bytes.NewReader(body))

View file

@ -4,12 +4,12 @@ import (
"context"
"github.com/lysand-org/versia-go/internal/repository"
"github.com/lysand-org/versia-go/internal/service"
"github.com/lysand-org/versia-go/pkg/versia"
"git.devminer.xyz/devminer/unitel"
"github.com/go-logr/logr"
"github.com/google/uuid"
"github.com/lysand-org/versia-go/internal/entity"
"github.com/lysand-org/versia-go/pkg/lysand"
)
var _ service.FollowService = (*FollowServiceImpl)(nil)
@ -69,7 +69,7 @@ func (i FollowServiceImpl) GetFollow(ctx context.Context, id uuid.UUID) (*entity
return f, nil
}
func (i FollowServiceImpl) ImportLysandFollow(ctx context.Context, lFollow *lysand.Follow) (*entity.Follow, error) {
func (i FollowServiceImpl) ImportLysandFollow(ctx context.Context, lFollow *versia.Follow) (*entity.Follow, error) {
s := i.telemetry.StartSpan(ctx, "function", "svc_impls/FollowServiceImpl.ImportLysandFollow").
AddAttribute("uri", lFollow.URI.String())
defer s.End()

View file

@ -4,6 +4,7 @@ import (
"context"
"github.com/lysand-org/versia-go/internal/repository"
"github.com/lysand-org/versia-go/internal/service"
"github.com/lysand-org/versia-go/pkg/versia"
"slices"
"git.devminer.xyz/devminer/unitel"
@ -12,7 +13,6 @@ import (
"github.com/lysand-org/versia-go/internal/api_schema"
"github.com/lysand-org/versia-go/internal/entity"
"github.com/lysand-org/versia-go/internal/tasks"
"github.com/lysand-org/versia-go/pkg/lysand"
)
var _ service.NoteService = (*NoteServiceImpl)(nil)
@ -89,7 +89,7 @@ func (i NoteServiceImpl) GetNote(ctx context.Context, id uuid.UUID) (*entity.Not
return i.repositories.Notes().GetByID(ctx, id)
}
func (i NoteServiceImpl) ImportLysandNote(ctx context.Context, lNote *lysand.Note) (*entity.Note, error) {
func (i NoteServiceImpl) ImportLysandNote(ctx context.Context, lNote *versia.Note) (*entity.Note, error) {
s := i.telemetry.StartSpan(ctx, "function", "svc_impls/NoteServiceImpl.ImportLysandNote")
defer s.End()
ctx = s.Context()

View file

@ -8,8 +8,7 @@ import (
"github.com/go-logr/logr"
"github.com/gofiber/fiber/v2"
"github.com/lysand-org/versia-go/internal/service"
"github.com/lysand-org/versia-go/pkg/lysand"
versiacrypto "github.com/lysand-org/versia-go/pkg/lysand/crypto"
versiacrypto "github.com/lysand-org/versia-go/pkg/versia/crypto"
"net/url"
)
@ -28,7 +27,7 @@ func NewRequestSignerImpl(telemetry *unitel.Telemetry, log logr.Logger) *Request
}
}
func (i *RequestSignerImpl) Sign(c *fiber.Ctx, signer lysand.Signer, body any) error {
func (i *RequestSignerImpl) Sign(c *fiber.Ctx, signer versiacrypto.Signer, body any) error {
s := i.telemetry.StartSpan(c.UserContext(), "function", "svc_impls/RequestSignerImpl.Sign")
defer s.End()
@ -50,7 +49,7 @@ func (i *RequestSignerImpl) Sign(c *fiber.Ctx, signer lysand.Signer, body any) e
digest := versiacrypto.SHA256(j)
d := lysand.NewSignatureData(c.Method(), nonce, uri, digest)
d := versiacrypto.NewSignatureData(c.Method(), nonce, uri, digest)
signed := signer.Sign(*d)
for k, v := range signed.Headers() {

View file

@ -2,11 +2,10 @@ package utils
import (
"fmt"
"net/url"
"github.com/google/uuid"
"github.com/lysand-org/versia-go/config"
"github.com/lysand-org/versia-go/pkg/lysand"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
"net/url"
)
var dicebearURL = &url.URL{
@ -15,81 +14,81 @@ var dicebearURL = &url.URL{
Path: "9.x/adventurer/svg",
}
func UserAPIURL(uuid uuid.UUID) *lysand.URL {
func UserAPIURL(uuid uuid.UUID) *versiautils.URL {
newPath := &url.URL{Path: fmt.Sprintf("/api/users/%s/", uuid.String())}
return lysand.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
return versiautils.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
}
func DefaultAvatarURL(uuid uuid.UUID) *lysand.URL {
func DefaultAvatarURL(uuid uuid.UUID) *versiautils.URL {
u := &url.URL{}
q := u.Query()
q.Set("seed", uuid.String())
u.RawQuery = q.Encode()
return lysand.URLFromStd(dicebearURL.ResolveReference(u))
return versiautils.URLFromStd(dicebearURL.ResolveReference(u))
}
func UserInboxAPIURL(uuid uuid.UUID) *lysand.URL {
func UserInboxAPIURL(uuid uuid.UUID) *versiautils.URL {
newPath := &url.URL{Path: "./inbox"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserOutboxAPIURL(uuid uuid.UUID) *lysand.URL {
func UserOutboxAPIURL(uuid uuid.UUID) *versiautils.URL {
newPath := &url.URL{Path: "./outbox"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserFollowersAPIURL(uuid uuid.UUID) *lysand.URL {
func UserFollowersAPIURL(uuid uuid.UUID) *versiautils.URL {
newPath := &url.URL{Path: "./followers"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserFollowingAPIURL(uuid uuid.UUID) *lysand.URL {
func UserFollowingAPIURL(uuid uuid.UUID) *versiautils.URL {
newPath := &url.URL{Path: "./following"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserFeaturedAPIURL(uuid uuid.UUID) *lysand.URL {
func UserFeaturedAPIURL(uuid uuid.UUID) *versiautils.URL {
newPath := &url.URL{Path: "./featured"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserLikesAPIURL(uuid uuid.UUID) *lysand.URL {
func UserLikesAPIURL(uuid uuid.UUID) *versiautils.URL {
newPath := &url.URL{Path: "./likes"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func UserDislikesAPIURL(uuid uuid.UUID) *lysand.URL {
func UserDislikesAPIURL(uuid uuid.UUID) *versiautils.URL {
newPath := &url.URL{Path: "./dislikes"}
return UserAPIURL(uuid).ResolveReference(newPath)
}
func FollowAPIURL(uuid uuid.UUID) *lysand.URL {
func FollowAPIURL(uuid uuid.UUID) *versiautils.URL {
newPath := &url.URL{Path: fmt.Sprintf("/api/follows/%s/", uuid.String())}
return lysand.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
return versiautils.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
}
func NoteAPIURL(uuid uuid.UUID) *lysand.URL {
func NoteAPIURL(uuid uuid.UUID) *versiautils.URL {
newPath := &url.URL{Path: fmt.Sprintf("/api/notes/%s/", uuid.String())}
return lysand.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
return versiautils.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
}
func InstanceMetadataAPIURL() *lysand.URL {
func InstanceMetadataAPIURL() *versiautils.URL {
newPath := &url.URL{Path: "/.well-known/versia/"}
return lysand.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
return versiautils.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
}
func InstanceMetadataAdminsAPIURL() *lysand.URL {
func InstanceMetadataAdminsAPIURL() *versiautils.URL {
newPath := &url.URL{Path: "/.well-known/versia/admins/"}
return lysand.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
return versiautils.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
}
func InstanceMetadataModeratorsAPIURL() *lysand.URL {
func InstanceMetadataModeratorsAPIURL() *versiautils.URL {
newPath := &url.URL{Path: "/.well-known/versia/moderators/"}
return lysand.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
return versiautils.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
}
func SharedInboxAPIURL() *lysand.URL {
func SharedInboxAPIURL() *versiautils.URL {
newPath := &url.URL{Path: "/api/inbox/"}
return lysand.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
return versiautils.URLFromStd(config.C.PublicAddress.ResolveReference(newPath))
}

View file

@ -9,7 +9,8 @@ import (
"github.com/lysand-org/versia-go/internal/repository"
"github.com/lysand-org/versia-go/internal/utils"
"github.com/lysand-org/versia-go/internal/validators"
"github.com/lysand-org/versia-go/pkg/lysand"
versiacrypto "github.com/lysand-org/versia-go/pkg/versia/crypto"
versiautils "github.com/lysand-org/versia-go/pkg/versia/utils"
"net/http"
)
@ -42,13 +43,13 @@ func (i RequestValidatorImpl) Validate(ctx context.Context, r *http.Request) err
r = r.WithContext(ctx)
fedHeaders, err := lysand.ExtractFederationHeaders(r.Header)
fedHeaders, err := versiacrypto.ExtractFederationHeaders(r.Header)
if err != nil {
return err
}
// TODO: Fetch user from database instead of using the URI
user, err := i.repositories.Users().Resolve(ctx, lysand.URLFromStd(fedHeaders.SignedBy))
user, err := i.repositories.Users().Resolve(ctx, versiautils.URLFromStd(fedHeaders.SignedBy))
if err != nil {
return err
}
@ -58,7 +59,7 @@ func (i RequestValidatorImpl) Validate(ctx context.Context, r *http.Request) err
return err
}
if !(lysand.Verifier{PublicKey: user.PublicKey.Key}).Verify(r.Method, r.URL, body, fedHeaders) {
if !(versiacrypto.Verifier{PublicKey: user.PublicKey.Key}).Verify(r.Method, r.URL, body, fedHeaders) {
i.log.WithCallDepth(1).Info("signature verification failed", "user", user.URI, "url", r.URL.Path)
s.CaptureError(ErrInvalidSignature)