Skip to content

Commit

Permalink
refactor(server): rename stuff from Lysand to Versia
Browse files Browse the repository at this point in the history
Big diff energy
  • Loading branch information
TheDevMinerTV committed Aug 27, 2024
1 parent 47f993b commit 5d141fb
Show file tree
Hide file tree
Showing 29 changed files with 102 additions and 97 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ VERSIA_PORT=8080
#VERSIA_TLS_CERT=

VERSIA_INSTANCE_ADDRESS=http://localhost:8080
VERSIA_INSTANCE_NAME=lysand-test
VERSIA_INSTANCE_NAME=My Versia
VERSIA_INSTANCE_DESCRIPTION=Versia-Go Instance

NATS_URI=nats://localhost:4222
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@ go run .
- [ ] Follows
- [ ] API
- [x] Automatic follows for public users
- [ ] Unfollows (scheduled for Lysand Working Draft 4)
- [ ] Unfollows (scheduled for Versia Working Draft 4)
- [ ] API
- [ ] Users
- [ ] API
- [x] Create user
- [ ] Lysand API
- [ ] Versia API
- [x] Get user (from local)
- [x] Webfinger
- [ ] User discovery
- [ ] Inbox handling
- [ ] Federated notes
- [ ] Federated unfollows
Expand Down
21 changes: 3 additions & 18 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,15 @@ services:

versia-1:
<<: *versia-default
hostname: lysand-test.i.devminer.xyz
hostname: versia.localhost
volumes:
- type: bind
source: ./1.db
target: /app/test.db
environment:
VERSIA_PORT: 8080
VERSIA_INSTANCE_ADDRESS: https://lysand-test.i.devminer.xyz:8080
VERSIA_INSTANCE_ADDRESS: http://versia.localhost:8080
NATS_URI: nats://nats:4222
NATS_STREAM_NAME: versia-go-1
NATS_STREAM_NAME: versia-go
ports:
- "8080:8080"

versia-2:
<<: *versia-default
hostname: lysand-test-2.i.devminer.xyz
volumes:
- type: bind
source: ./2.db
target: /app/test.db
environment:
VERSIA_PORT: 8081
VERSIA_INSTANCE_ADDRESS: https://lysand-test-2.i.devminer.xyz:8081
NATS_URI: nats://nats:4222
NATS_STREAM_NAME: versia-go-2
ports:
- "8081:8081"
37 changes: 17 additions & 20 deletions internal/entity/follow.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,66 @@ import (
type Follow struct {
*ent.Follow

URI *versiautils.URL
FollowerURI *versiautils.URL
FolloweeURI *versiautils.URL
URI *versiautils.URL
Follower *User
Followee *User
}

func NewFollow(dbFollow *ent.Follow) (*Follow, error) {
f := &Follow{Follow: dbFollow}
func NewFollow(dbData *ent.Follow) (*Follow, error) {
f := &Follow{Follow: dbData}

var err error

f.URI, err = versiautils.ParseURL(dbFollow.URI)
if err != nil {
if f.URI, err = versiautils.ParseURL(dbData.URI); err != nil {
return nil, err
}

f.FollowerURI, err = versiautils.ParseURL(dbFollow.Edges.Follower.URI)
if err != nil {
if f.Follower, err = NewUser(dbData.Edges.Follower); err != nil {
return nil, err
}

f.FolloweeURI, err = versiautils.ParseURL(dbFollow.Edges.Followee.URI)
if err != nil {
if f.Followee, err = NewUser(dbData.Edges.Followee); err != nil {
return nil, err
}

return f, nil
}

func (f Follow) ToLysand() *versia.Follow {
func (f Follow) ToVersia() *versia.Follow {
return &versia.Follow{
Entity: versia.Entity{
ID: f.ID,
URI: f.URI,
CreatedAt: versiautils.Time(f.CreatedAt),
Extensions: f.Extensions,
},
Author: f.FollowerURI,
Followee: f.FolloweeURI,
Author: f.Follower.URI,
Followee: f.Followee.URI,
}
}

func (f Follow) ToLysandAccept() *versia.FollowAccept {
func (f Follow) ToVersiaAccept() *versia.FollowAccept {
return &versia.FollowAccept{
Entity: versia.Entity{
ID: f.ID,
URI: f.URI,
CreatedAt: versiautils.Time(f.CreatedAt),
Extensions: f.Extensions,
},
Author: f.FolloweeURI,
Follower: f.FollowerURI,
Author: f.Followee.URI,
Follower: f.Follower.URI,
}
}

func (f Follow) ToLysandReject() *versia.FollowReject {
func (f Follow) ToVersiaReject() *versia.FollowReject {
return &versia.FollowReject{
Entity: versia.Entity{
ID: f.ID,
URI: f.URI,
CreatedAt: versiautils.Time(f.CreatedAt),
Extensions: f.Extensions,
},
Author: f.FolloweeURI,
Follower: f.FollowerURI,
Author: f.Followee.URI,
Follower: f.Follower.URI,
}
}
2 changes: 1 addition & 1 deletion internal/entity/note.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func NewNote(dbNote *ent.Note) (*Note, error) {
return n, nil
}

func (n Note) ToLysand() versia.Note {
func (n Note) ToVersia() versia.Note {
return versia.Note{
Entity: versia.Entity{
ID: n.ID,
Expand Down
2 changes: 1 addition & 1 deletion internal/entity/server_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func NewInstanceMetadata(dbData *ent.InstanceMetadata) (*InstanceMetadata, error
return n, nil
}

func (m InstanceMetadata) ToLysand() versia.InstanceMetadata {
func (m InstanceMetadata) ToVersia() versia.InstanceMetadata {
return versia.InstanceMetadata{
CreatedAt: versiautils.TimeFromStd(m.CreatedAt),
Extensions: m.Extensions,
Expand Down
22 changes: 11 additions & 11 deletions internal/entity/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ type User struct {
Followers *versiautils.URL
Following *versiautils.URL

DisplayName string
LysandAvatar versiautils.ImageContentTypeMap
LysandBiography versiautils.TextContentTypeMap
Signer versiacrypto.Signer
DisplayName string
Avatar versiautils.ImageContentTypeMap
Biography versiautils.TextContentTypeMap
Signer versiacrypto.Signer
}

func NewUser(dbData *ent.User) (*User, error) {
Expand All @@ -38,8 +38,8 @@ func NewUser(dbData *ent.User) (*User, error) {
},
DisplayName: dbData.Username,

LysandAvatar: lysandAvatar(dbData),
LysandBiography: lysandBiography(dbData),
Avatar: userAvatar(dbData),
Biography: userBiography(dbData),
}

if dbData.DisplayName != nil {
Expand Down Expand Up @@ -81,7 +81,7 @@ func NewUser(dbData *ent.User) (*User, error) {
return u, nil
}

func (u User) ToLysand() *versia.User {
func (u User) ToVersia() *versia.User {
return &versia.User{
Entity: versia.Entity{
ID: u.ID,
Expand All @@ -91,15 +91,15 @@ func (u User) ToLysand() *versia.User {
},
DisplayName: helpers.StringPtr(u.DisplayName),
Username: u.Username,
Avatar: u.LysandAvatar,
Avatar: u.Avatar,
Header: imageMap(u.Edges.HeaderImage),
Indexable: u.Indexable,
PublicKey: versia.UserPublicKey{
Actor: u.PKActorURI,
Algorithm: u.PublicKeyAlgorithm,
Key: u.PublicKey,
},
Bio: u.LysandBiography,
Bio: u.Biography,
Fields: u.Fields,

Inbox: u.Inbox,
Expand All @@ -110,7 +110,7 @@ func (u User) ToLysand() *versia.User {
}
}

func lysandAvatar(u *ent.User) versiautils.ImageContentTypeMap {
func userAvatar(u *ent.User) versiautils.ImageContentTypeMap {
if avatar := imageMap(u.Edges.AvatarImage); avatar != nil {
return avatar
}
Expand All @@ -122,7 +122,7 @@ func lysandAvatar(u *ent.User) versiautils.ImageContentTypeMap {
}
}

func lysandBiography(u *ent.User) versiautils.TextContentTypeMap {
func userBiography(u *ent.User) versiautils.TextContentTypeMap {
if u.Biography == nil {
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/follow_handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func New(followService service.FollowService, federationService service.Federati
}

func (i *Handler) Register(r fiber.Router) {
r.Get("/api/follows/:id", i.GetLysandFollow)
r.Get("/api/follows/:id", i.GetVersiaFollow)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/lysand-org/versia-go/internal/api_schema"
)

func (i *Handler) GetLysandFollow(c *fiber.Ctx) error {
func (i *Handler) GetVersiaFollow(c *fiber.Ctx) error {
parsedRequestedFollowID, err := uuid.Parse(c.Params("id"))
if err != nil {
return api_schema.ErrBadRequest(map[string]any{"reason": "Invalid follow ID"})
Expand All @@ -24,5 +24,5 @@ func (i *Handler) GetLysandFollow(c *fiber.Ctx) error {
})
}

return c.JSON(f.ToLysand())
return c.JSON(f.ToVersia())
}
6 changes: 3 additions & 3 deletions internal/handlers/meta_handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ func New(instanceMetadataService service.InstanceMetadataService, log logr.Logge
}

func (i *Handler) Register(r fiber.Router) {
r.Get("/.well-known/versia", i.GetLysandInstanceMetadata)
r.Get("/.well-known/versia/admins", i.GetLysandInstanceMetadata)
r.Get("/.well-known/versia/moderators", i.GetLysandInstanceMetadata)
r.Get("/.well-known/versia", i.GetVersiaInstanceMetadata)
r.Get("/.well-known/versia/admins", i.GetVersiaInstanceMetadata)
r.Get("/.well-known/versia/moderators", i.GetVersiaInstanceMetadata)

// Webfinger host meta spec
r.Get("/.well-known/host-meta", i.GetHostMeta)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import (
"github.com/gofiber/fiber/v2"
)

func (i *Handler) GetLysandInstanceMetadata(c *fiber.Ctx) error {
func (i *Handler) GetVersiaInstanceMetadata(c *fiber.Ctx) error {
m, err := i.instanceMetadataService.Ours(c.UserContext())
if err != nil {
return err
}

return c.JSON(m.ToLysand())
// TODO: Sign with the instance private key
return c.JSON(m.ToVersia())
}
17 changes: 14 additions & 3 deletions internal/handlers/note_handler/app_note_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,26 @@ func (i *Handler) GetNote(c *fiber.Ctx) error {
})
}

u, err := i.noteService.GetNote(c.UserContext(), parsedRequestedNoteID)
n, err := i.noteService.GetNote(c.UserContext(), parsedRequestedNoteID)
if err != nil {
i.log.Error(err, "Failed to query note", "id", parsedRequestedNoteID)

return api_schema.ErrInternalServerError(map[string]any{"reason": "Failed to query note"})
}
if u == nil {
if n == nil {
return api_schema.ErrNotFound(nil)
}

return c.JSON(u.ToLysand())
if !n.Author.IsRemote {
// For local authors we can also sign the note
if err := i.requestSigner.SignAndSend(c, n.Author.Signer, n.ToVersia()); err != nil {
i.log.Error(err, "Failed to sign response body", "id", parsedRequestedNoteID)

return api_schema.ErrInternalServerError(map[string]any{
"reason": "failed to sign response body",
})
}
}

return c.JSON(n.ToVersia())
}
4 changes: 3 additions & 1 deletion internal/handlers/note_handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ import (
type Handler struct {
noteService service.NoteService
bodyValidator validators.BodyValidator
requestSigner service.RequestSigner

hostMeta webfinger.HostMeta

log logr.Logger
}

func New(noteService service.NoteService, bodyValidator validators.BodyValidator, log logr.Logger) *Handler {
func New(noteService service.NoteService, bodyValidator validators.BodyValidator, requestSigner service.RequestSigner, log logr.Logger) *Handler {
return &Handler{
noteService: noteService,
bodyValidator: bodyValidator,
requestSigner: requestSigner,

hostMeta: webfinger.NewHostMeta(config.C.PublicAddress),

Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/user_handler/app_user_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,5 @@ func (i *Handler) SearchUser(c *fiber.Ctx) error {
return api_schema.ErrNotFound(nil)
}

return c.JSON((*api_schema.VersiaUser)(u.ToLysand()))
return c.JSON((*api_schema.VersiaUser)(u.ToVersia()))
}
4 changes: 2 additions & 2 deletions internal/handlers/user_handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ func (i *Handler) Register(r fiber.Router) {
r.Get("/api/app/users/:id", i.GetUser)
r.Post("/api/app/users/", i.CreateUser)

r.Get("/api/users/:id", i.GetLysandUser)
r.Post("/api/users/:id/inbox", i.LysandInbox)
r.Get("/api/users/:id", i.GetVersiaUser)
r.Post("/api/users/:id/inbox", i.HandleVersiaInbox)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/lysand-org/versia-go/internal/api_schema"
)

func (i *Handler) LysandInbox(c *fiber.Ctx) error {
func (i *Handler) HandleVersiaInbox(c *fiber.Ctx) error {
if err := i.requestValidator.ValidateFiberCtx(c.UserContext(), c); err != nil {
if errors.Is(err, val_impls.ErrInvalidSignature) {
i.log.Error(err, "Invalid signature")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/lysand-org/versia-go/internal/api_schema"
)

func (i *Handler) GetLysandUser(c *fiber.Ctx) error {
func (i *Handler) GetVersiaUser(c *fiber.Ctx) error {
parsedRequestedUserID, err := uuid.Parse(c.Params("id"))
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{
Expand All @@ -27,5 +27,13 @@ func (i *Handler) GetLysandUser(c *fiber.Ctx) error {
return api_schema.ErrNotFound(map[string]any{"id": parsedRequestedUserID})
}

return i.requestSigner.Sign(c, u.Signer, u.ToLysand())
if err := i.requestSigner.SignAndSend(c, u.Signer, u.ToVersia()); err != nil {
i.log.Error(err, "Failed to sign response body", "id", parsedRequestedUserID)

return api_schema.ErrInternalServerError(map[string]any{
"reason": "failed to sign response body",
})
}

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func (i *InstanceMetadataRepositoryImpl) GetByHost(ctx context.Context, host str
return entity.NewInstanceMetadata(m)
}

func (i *InstanceMetadataRepositoryImpl) ImportFromLysandByURI(ctx context.Context, uri *versiautils.URL) (*entity.InstanceMetadata, error) {
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/InstanceMetadataRepositoryImpl.ImportFromLysandByURI").
func (i *InstanceMetadataRepositoryImpl) ImportFromVersiaByURI(ctx context.Context, uri *versiautils.URL) (*entity.InstanceMetadata, error) {
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/InstanceMetadataRepositoryImpl.ImportFromVersiaByURI").
AddAttribute("uri", uri.String())
defer s.End()
ctx = s.Context()
Expand Down
Loading

0 comments on commit 5d141fb

Please sign in to comment.