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

@ -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() {