mirror of
https://github.com/versia-pub/versia-go.git
synced 2026-03-13 04:29:15 +01:00
refactor!: working WD-4 user discovery
This commit is contained in:
parent
cf0053312d
commit
61891d891a
91 changed files with 12768 additions and 5562 deletions
|
|
@ -36,7 +36,7 @@ func NewFollowRepositoryImpl(db *ent.Client, log logr.Logger, telemetry *unitel.
|
|||
}
|
||||
|
||||
func (i FollowRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entity.Follow, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.FollowRepositoryImpl.GetByID").
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/FollowRepositoryImpl.GetByID").
|
||||
AddAttribute("followID", id)
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
|
@ -58,7 +58,7 @@ func (i FollowRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entit
|
|||
}
|
||||
|
||||
func (i FollowRepositoryImpl) Follow(ctx context.Context, follower, followee *entity.User) (*entity.Follow, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.FollowRepositoryImpl.Follow").
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/FollowRepositoryImpl.Follow").
|
||||
AddAttribute("follower", follower.URI).
|
||||
AddAttribute("followee", followee.URI)
|
||||
defer s.End()
|
||||
|
|
@ -101,7 +101,7 @@ func (i FollowRepositoryImpl) Follow(ctx context.Context, follower, followee *en
|
|||
}
|
||||
|
||||
func (i FollowRepositoryImpl) Unfollow(ctx context.Context, follower, followee *entity.User) error {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.FollowRepositoryImpl.Unfollow").
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/FollowRepositoryImpl.Unfollow").
|
||||
AddAttribute("follower", follower.URI).
|
||||
AddAttribute("followee", followee.URI)
|
||||
defer s.End()
|
||||
|
|
@ -121,7 +121,7 @@ func (i FollowRepositoryImpl) Unfollow(ctx context.Context, follower, followee *
|
|||
}
|
||||
|
||||
func (i FollowRepositoryImpl) AcceptFollow(ctx context.Context, follower, followee *entity.User) error {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.FollowRepositoryImpl.AcceptFollow").
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/FollowRepositoryImpl.AcceptFollow").
|
||||
AddAttribute("follower", follower.URI).
|
||||
AddAttribute("followee", followee.URI)
|
||||
defer s.End()
|
||||
|
|
@ -141,7 +141,7 @@ func (i FollowRepositoryImpl) AcceptFollow(ctx context.Context, follower, follow
|
|||
}
|
||||
|
||||
func (i FollowRepositoryImpl) RejectFollow(ctx context.Context, follower, followee *entity.User) error {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.FollowRepositoryImpl.RejectFollow").
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/FollowRepositoryImpl.RejectFollow").
|
||||
AddAttribute("follower", follower.URI).
|
||||
AddAttribute("followee", followee.URI)
|
||||
defer s.End()
|
||||
|
|
|
|||
|
|
@ -0,0 +1,62 @@
|
|||
package repo_impls
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.devminer.xyz/devminer/unitel"
|
||||
"github.com/go-logr/logr"
|
||||
"github.com/lysand-org/versia-go/ent"
|
||||
"github.com/lysand-org/versia-go/ent/instancemetadata"
|
||||
"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"
|
||||
)
|
||||
|
||||
var _ repository.InstanceMetadataRepository = (*InstanceMetadataRepositoryImpl)(nil)
|
||||
|
||||
type InstanceMetadataRepositoryImpl struct {
|
||||
federationService service.FederationService
|
||||
|
||||
db *ent.Client
|
||||
log logr.Logger
|
||||
telemetry *unitel.Telemetry
|
||||
}
|
||||
|
||||
func NewInstanceMetadataRepositoryImpl(federationService service.FederationService, db *ent.Client, log logr.Logger, telemetry *unitel.Telemetry) repository.InstanceMetadataRepository {
|
||||
return &InstanceMetadataRepositoryImpl{
|
||||
federationService: federationService,
|
||||
|
||||
db: db,
|
||||
log: log,
|
||||
telemetry: telemetry,
|
||||
}
|
||||
}
|
||||
|
||||
func (i *InstanceMetadataRepositoryImpl) GetByHost(ctx context.Context, host string) (*entity.InstanceMetadata, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/InstanceMetadataRepositoryImpl.GetByHost").
|
||||
AddAttribute("host", host)
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
m, err := i.db.InstanceMetadata.Query().
|
||||
Where(instancemetadata.Host(host)).
|
||||
WithAdmins().
|
||||
WithModerators().
|
||||
Only(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return entity.NewInstanceMetadata(m)
|
||||
}
|
||||
|
||||
func (i *InstanceMetadataRepositoryImpl) ImportFromLysandByURI(ctx context.Context, uri *lysand.URL) (*entity.InstanceMetadata, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/InstanceMetadataRepositoryImpl.ImportFromLysandByURI").
|
||||
AddAttribute("uri", uri.String())
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
//i.federationService.
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
|
@ -15,32 +15,38 @@ type Factory[T any] func(db *ent.Client, log logr.Logger, telemetry *unitel.Tele
|
|||
var _ repository.Manager = (*ManagerImpl)(nil)
|
||||
|
||||
type ManagerImpl struct {
|
||||
users repository.UserRepository
|
||||
notes repository.NoteRepository
|
||||
follows repository.FollowRepository
|
||||
users repository.UserRepository
|
||||
notes repository.NoteRepository
|
||||
follows repository.FollowRepository
|
||||
instanceMetadata repository.InstanceMetadataRepository
|
||||
|
||||
uRFactory Factory[repository.UserRepository]
|
||||
nRFactory Factory[repository.NoteRepository]
|
||||
fRFactory Factory[repository.FollowRepository]
|
||||
uRFactory Factory[repository.UserRepository]
|
||||
nRFactory Factory[repository.NoteRepository]
|
||||
fRFactory Factory[repository.FollowRepository]
|
||||
imRFactory Factory[repository.InstanceMetadataRepository]
|
||||
|
||||
db *ent.Client
|
||||
log logr.Logger
|
||||
telemetry *unitel.Telemetry
|
||||
}
|
||||
|
||||
func NewManagerImpl(db *ent.Client, telemetry *unitel.Telemetry, log logr.Logger, userRepositoryFunc Factory[repository.UserRepository], noteRepositoryFunc Factory[repository.NoteRepository], followRepositoryFunc Factory[repository.FollowRepository]) *ManagerImpl {
|
||||
userRepository := userRepositoryFunc(db, log.WithName("users"), telemetry)
|
||||
noteRepository := noteRepositoryFunc(db, log.WithName("notes"), telemetry)
|
||||
followRepository := followRepositoryFunc(db, log.WithName("follows"), telemetry)
|
||||
|
||||
func NewManagerImpl(
|
||||
db *ent.Client, telemetry *unitel.Telemetry, log logr.Logger,
|
||||
userRepositoryFunc Factory[repository.UserRepository],
|
||||
noteRepositoryFunc Factory[repository.NoteRepository],
|
||||
followRepositoryFunc Factory[repository.FollowRepository],
|
||||
instanceMetadataRepositoryFunc Factory[repository.InstanceMetadataRepository],
|
||||
) *ManagerImpl {
|
||||
return &ManagerImpl{
|
||||
users: userRepository,
|
||||
notes: noteRepository,
|
||||
follows: followRepository,
|
||||
users: userRepositoryFunc(db, log.WithName("users"), telemetry),
|
||||
notes: noteRepositoryFunc(db, log.WithName("notes"), telemetry),
|
||||
follows: followRepositoryFunc(db, log.WithName("follows"), telemetry),
|
||||
instanceMetadata: instanceMetadataRepositoryFunc(db, log.WithName("instanceMetadata"), telemetry),
|
||||
|
||||
uRFactory: userRepositoryFunc,
|
||||
nRFactory: noteRepositoryFunc,
|
||||
fRFactory: followRepositoryFunc,
|
||||
uRFactory: userRepositoryFunc,
|
||||
nRFactory: noteRepositoryFunc,
|
||||
fRFactory: followRepositoryFunc,
|
||||
imRFactory: instanceMetadataRepositoryFunc,
|
||||
|
||||
db: db,
|
||||
log: log,
|
||||
|
|
@ -49,11 +55,17 @@ func NewManagerImpl(db *ent.Client, telemetry *unitel.Telemetry, log logr.Logger
|
|||
}
|
||||
|
||||
func (i *ManagerImpl) withDB(db *ent.Client) *ManagerImpl {
|
||||
return NewManagerImpl(db, i.telemetry, i.log, i.uRFactory, i.nRFactory, i.fRFactory)
|
||||
return NewManagerImpl(
|
||||
db, i.telemetry, i.log,
|
||||
i.uRFactory,
|
||||
i.nRFactory,
|
||||
i.fRFactory,
|
||||
i.imRFactory,
|
||||
)
|
||||
}
|
||||
|
||||
func (i *ManagerImpl) Atomic(ctx context.Context, fn func(ctx context.Context, tx repository.Manager) error) error {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.ManagerImpl.Atomic")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/ManagerImpl.Atomic")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
|
|
@ -88,3 +100,7 @@ func (i *ManagerImpl) Notes() repository.NoteRepository {
|
|||
func (i *ManagerImpl) Follows() repository.FollowRepository {
|
||||
return i.follows
|
||||
}
|
||||
|
||||
func (i *ManagerImpl) InstanceMetadata() repository.InstanceMetadataRepository {
|
||||
return i.instanceMetadata
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ func NewNoteRepositoryImpl(db *ent.Client, log logr.Logger, telemetry *unitel.Te
|
|||
}
|
||||
|
||||
func (i *NoteRepositoryImpl) NewNote(ctx context.Context, author *entity.User, content string, mentions []*entity.User) (*entity.Note, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.NoteRepositoryImpl.NewNote")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/NoteRepositoryImpl.NewNote")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
|
|
@ -63,7 +63,7 @@ func (i *NoteRepositoryImpl) NewNote(ctx context.Context, author *entity.User, c
|
|||
}
|
||||
|
||||
func (i *NoteRepositoryImpl) ImportLysandNote(ctx context.Context, lNote *lysand.Note) (*entity.Note, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.NoteRepositoryImpl.ImportLysandNote")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/NoteRepositoryImpl.ImportLysandNote")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
|
|
@ -91,7 +91,7 @@ func (i *NoteRepositoryImpl) ImportLysandNote(ctx context.Context, lNote *lysand
|
|||
}
|
||||
|
||||
func (i *NoteRepositoryImpl) GetByID(ctx context.Context, id uuid.UUID) (*entity.Note, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.NoteRepositoryImpl.LookupByIDOrUsername")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/NoteRepositoryImpl.LookupByIDOrUsername")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"crypto/ed25519"
|
||||
"errors"
|
||||
"github.com/lysand-org/versia-go/config"
|
||||
"github.com/lysand-org/versia-go/internal/repository"
|
||||
"github.com/lysand-org/versia-go/internal/service"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
|
|
@ -45,7 +46,7 @@ func NewUserRepositoryImpl(federationService service.FederationService, db *ent.
|
|||
}
|
||||
|
||||
func (i *UserRepositoryImpl) NewUser(ctx context.Context, username, password string, priv ed25519.PrivateKey, pub ed25519.PublicKey) (*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.UserRepositoryImpl.NewUser")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.NewUser")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
|
|
@ -62,8 +63,10 @@ func (i *UserRepositoryImpl) NewUser(ctx context.Context, username, password str
|
|||
SetURI(utils.UserAPIURL(uid).String()).
|
||||
SetUsername(username).
|
||||
SetPasswordHash(pwHash).
|
||||
SetPublicKey(pub).
|
||||
SetPrivateKey(priv).
|
||||
SetPublicKey(pub).
|
||||
SetPublicKeyAlgorithm("ed25519").
|
||||
SetPublicKeyActor(utils.UserAPIURL(uid).String()).
|
||||
SetInbox(utils.UserInboxAPIURL(uid).String()).
|
||||
SetOutbox(utils.UserOutboxAPIURL(uid).String()).
|
||||
SetFeatured(utils.UserFeaturedAPIURL(uid).String()).
|
||||
|
|
@ -82,7 +85,7 @@ func (i *UserRepositoryImpl) NewUser(ctx context.Context, username, password str
|
|||
}
|
||||
|
||||
func (i *UserRepositoryImpl) ImportLysandUserByURI(ctx context.Context, uri *lysand.URL) (*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.UserRepositoryImpl.ImportLysandUserByURI")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.ImportLysandUserByURI")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
|
|
@ -99,7 +102,9 @@ func (i *UserRepositoryImpl) ImportLysandUserByURI(ctx context.Context, uri *lys
|
|||
SetUsername(lUser.Username).
|
||||
SetNillableDisplayName(lUser.DisplayName).
|
||||
SetBiography(lUser.Bio.String()).
|
||||
SetPublicKey(lUser.PublicKey.PublicKey.ToStd()).
|
||||
SetPublicKey(lUser.PublicKey.RawKey).
|
||||
SetPublicKeyAlgorithm(lUser.PublicKey.Algorithm).
|
||||
SetPublicKeyActor(lUser.PublicKey.Actor.String()).
|
||||
SetIndexable(lUser.Indexable).
|
||||
SetFields(lUser.Fields).
|
||||
SetExtensions(lUser.Extensions).
|
||||
|
|
@ -127,11 +132,66 @@ func (i *UserRepositoryImpl) ImportLysandUserByURI(ctx context.Context, uri *lys
|
|||
return entity.NewUser(u)
|
||||
}
|
||||
|
||||
func (i *UserRepositoryImpl) Resolve(ctx context.Context, uri *lysand.URL) (*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.UserRepositoryImpl.Resolve")
|
||||
func (i *UserRepositoryImpl) Discover(ctx context.Context, domain, username string) (*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "svc_impls/UserServiceImpl.Search").
|
||||
AddAttribute("username", username).
|
||||
AddAttribute("domain", domain)
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
l := i.log.WithValues("domain", domain, "username", username)
|
||||
|
||||
// TODO: This *could* go wrong
|
||||
if domain != config.C.Host {
|
||||
l.V(2).Info("Discovering instance")
|
||||
|
||||
im, err := i.federationService.DiscoverInstance(ctx, domain)
|
||||
if err != nil {
|
||||
l.Error(err, "Failed to discover instance")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
l = l.WithValues("host", im.Host)
|
||||
|
||||
l.V(2).Info("Discovering user")
|
||||
|
||||
wf, err := i.federationService.DiscoverUser(ctx, im.Host, username)
|
||||
if err != nil {
|
||||
l.Error(err, "Failed to discover user")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
l.V(2).Info("Found remote user", "userURI", wf.URI)
|
||||
|
||||
u, err := i.Resolve(ctx, lysand.URLFromStd(wf.URI))
|
||||
if err != nil {
|
||||
l.Error(err, "Failed to resolve user")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
l.V(2).Info("Finding local user")
|
||||
|
||||
u, err := i.GetLocalByUsername(ctx, username)
|
||||
if err != nil {
|
||||
l.Error(err, "Failed to find local user", "username", username)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
l.V(2).Info("Found local user", "userURI", u.URI)
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (i *UserRepositoryImpl) Resolve(ctx context.Context, uri *lysand.URL) (*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.Resolve")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
l := i.log.WithValues("uri", uri)
|
||||
|
||||
u, err := i.LookupByURI(ctx, uri)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
@ -139,24 +199,24 @@ func (i *UserRepositoryImpl) Resolve(ctx context.Context, uri *lysand.URL) (*ent
|
|||
|
||||
// check if the user is already imported
|
||||
if u == nil {
|
||||
i.log.V(2).Info("User not found in DB", "uri", uri)
|
||||
l.V(2).Info("User not found in DB")
|
||||
|
||||
u, err := i.ImportLysandUserByURI(ctx, uri)
|
||||
if err != nil {
|
||||
i.log.Error(err, "Failed to import user", "uri", uri)
|
||||
l.Error(err, "Failed to import user")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
i.log.V(2).Info("User found in DB", "uri", uri)
|
||||
l.V(2).Info("User found in DB")
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (i *UserRepositoryImpl) ResolveMultiple(ctx context.Context, uris []lysand.URL) ([]*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.UserRepositoryImpl.ResolveMultiple")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.ResolveMultiple")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
|
|
@ -168,25 +228,27 @@ func (i *UserRepositoryImpl) ResolveMultiple(ctx context.Context, uris []lysand.
|
|||
// TODO: Refactor to use async imports using a work queue
|
||||
outer:
|
||||
for _, uri := range uris {
|
||||
l := i.log.WithValues("uri", uri)
|
||||
|
||||
// check if the user is already imported
|
||||
for _, u := range us {
|
||||
if uri.String() == u.URI.String() {
|
||||
i.log.V(2).Info("User found in DB", "uri", uri)
|
||||
l.V(2).Info("User found in DB")
|
||||
|
||||
continue outer
|
||||
}
|
||||
}
|
||||
|
||||
i.log.V(2).Info("User not found in DB", "uri", uri)
|
||||
l.V(2).Info("User not found in DB")
|
||||
|
||||
importedUser, err := i.ImportLysandUserByURI(ctx, &uri)
|
||||
if err != nil {
|
||||
i.log.Error(err, "Failed to import user", "uri", uri)
|
||||
l.Error(err, "Failed to import user")
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
i.log.V(2).Info("Imported user", "uri", uri)
|
||||
l.V(2).Info("Imported user")
|
||||
|
||||
us = append(us, importedUser)
|
||||
}
|
||||
|
|
@ -195,10 +257,12 @@ outer:
|
|||
}
|
||||
|
||||
func (i *UserRepositoryImpl) GetByID(ctx context.Context, uid uuid.UUID) (*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.UserRepositoryImpl.GetByID")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.GetByID")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
l := i.log.WithValues("id", uid)
|
||||
|
||||
u, err := i.db.User.Query().
|
||||
Where(user.IDEQ(uid)).
|
||||
WithAvatarImage().
|
||||
|
|
@ -206,25 +270,27 @@ func (i *UserRepositoryImpl) GetByID(ctx context.Context, uid uuid.UUID) (*entit
|
|||
Only(ctx)
|
||||
if err != nil {
|
||||
if !ent.IsNotFound(err) {
|
||||
i.log.Error(err, "Failed to query user", "id", uid)
|
||||
l.Error(err, "Failed to query user")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
i.log.V(2).Info("User not found in DB", "id", uid)
|
||||
l.V(2).Info("User not found in DB")
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
i.log.V(2).Info("User found in DB", "id", uid)
|
||||
l.V(2).Info("User found in DB")
|
||||
|
||||
return entity.NewUser(u)
|
||||
}
|
||||
|
||||
func (i *UserRepositoryImpl) GetLocalByID(ctx context.Context, uid uuid.UUID) (*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.UserRepositoryImpl.GetLocalByID")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.GetLocalByID")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
l := i.log.WithValues("id", uid)
|
||||
|
||||
u, err := i.db.User.Query().
|
||||
Where(user.And(user.ID(uid), user.IsRemote(false))).
|
||||
WithAvatarImage().
|
||||
|
|
@ -232,47 +298,77 @@ func (i *UserRepositoryImpl) GetLocalByID(ctx context.Context, uid uuid.UUID) (*
|
|||
Only(ctx)
|
||||
if err != nil {
|
||||
if !ent.IsNotFound(err) {
|
||||
i.log.Error(err, "Failed to query local user", "id", uid)
|
||||
l.Error(err, "Failed to query local user")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
i.log.V(2).Info("Local user not found in DB", "id", uid)
|
||||
l.V(2).Info("Local user not found in DB")
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
i.log.V(2).Info("Local user found in DB", "id", uid)
|
||||
l.V(2).Info("Local user found in DB", "uri", u.URI)
|
||||
|
||||
return entity.NewUser(u)
|
||||
}
|
||||
|
||||
func (i *UserRepositoryImpl) GetLocalByUsername(ctx context.Context, username string) (*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.GetLocalByUsername")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
l := i.log.WithValues("username", username)
|
||||
|
||||
u, err := i.db.User.Query().
|
||||
Where(user.And(user.Username(username), user.IsRemote(false))).
|
||||
WithAvatarImage().
|
||||
WithHeaderImage().
|
||||
Only(ctx)
|
||||
if err != nil {
|
||||
if !ent.IsNotFound(err) {
|
||||
l.Error(err, "Failed to query local user")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
l.V(2).Info("Local user not found in DB")
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
l.V(2).Info("Local user found in DB", "uri", u.URI)
|
||||
|
||||
return entity.NewUser(u)
|
||||
}
|
||||
|
||||
func (i *UserRepositoryImpl) LookupByURI(ctx context.Context, uri *lysand.URL) (*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.UserRepositoryImpl.LookupByURI")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.LookupByURI")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
l := i.log.WithValues("uri", uri)
|
||||
|
||||
// check if the user is already imported
|
||||
u, err := i.db.User.Query().
|
||||
Where(user.URI(uri.String())).
|
||||
Only(ctx)
|
||||
if err != nil {
|
||||
if !ent.IsNotFound(err) {
|
||||
i.log.Error(err, "Failed to query user", "uri", uri)
|
||||
l.Error(err, "Failed to query user")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
i.log.V(2).Info("User not found in DB", "uri", uri)
|
||||
l.V(2).Info("User not found in DB")
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
i.log.V(2).Info("User found in DB", "uri", uri)
|
||||
l.V(2).Info("User found in DB")
|
||||
|
||||
return entity.NewUser(u)
|
||||
}
|
||||
|
||||
func (i *UserRepositoryImpl) LookupByURIs(ctx context.Context, uris []lysand.URL) ([]*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.UserRepositoryImpl.LookupByURIs")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.LookupByURIs")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
|
|
@ -292,7 +388,7 @@ func (i *UserRepositoryImpl) LookupByURIs(ctx context.Context, uris []lysand.URL
|
|||
}
|
||||
|
||||
func (i *UserRepositoryImpl) LookupByIDOrUsername(ctx context.Context, idOrUsername string) (*entity.User, error) {
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repository/repo_impls.UserRepositoryImpl.LookupByIDOrUsername")
|
||||
s := i.telemetry.StartSpan(ctx, "function", "repo_impls/UserRepositoryImpl.LookupByIDOrUsername")
|
||||
defer s.End()
|
||||
ctx = s.Context()
|
||||
|
||||
|
|
@ -303,6 +399,8 @@ func (i *UserRepositoryImpl) LookupByIDOrUsername(ctx context.Context, idOrUsern
|
|||
preds = append(preds, user.UsernameEQ(idOrUsername))
|
||||
}
|
||||
|
||||
l := i.log.WithValues("idOrUsername", idOrUsername)
|
||||
|
||||
u, err := i.db.User.Query().
|
||||
Where(preds...).
|
||||
WithAvatarImage().
|
||||
|
|
@ -310,16 +408,16 @@ func (i *UserRepositoryImpl) LookupByIDOrUsername(ctx context.Context, idOrUsern
|
|||
Only(ctx)
|
||||
if err != nil {
|
||||
if !ent.IsNotFound(err) {
|
||||
i.log.Error(err, "Failed to query user", "idOrUsername", idOrUsername)
|
||||
l.Error(err, "Failed to query user")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
i.log.V(2).Info("User not found in DB", "idOrUsername", idOrUsername)
|
||||
l.V(2).Info("User not found in DB")
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
i.log.V(2).Info("User found in DB", "idOrUsername", idOrUsername, "id", u.ID)
|
||||
l.V(2).Info("User found in DB", "id", u.ID)
|
||||
|
||||
return entity.NewUser(u)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,6 +15,9 @@ type UserRepository interface {
|
|||
|
||||
GetByID(ctx context.Context, id uuid.UUID) (*entity.User, error)
|
||||
GetLocalByID(ctx context.Context, id uuid.UUID) (*entity.User, error)
|
||||
GetLocalByUsername(ctx context.Context, username string) (*entity.User, error)
|
||||
|
||||
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)
|
||||
|
|
@ -40,10 +43,16 @@ type NoteRepository interface {
|
|||
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)
|
||||
}
|
||||
|
||||
type Manager interface {
|
||||
Atomic(ctx context.Context, fn func(ctx context.Context, tx Manager) error) error
|
||||
|
||||
Users() UserRepository
|
||||
Notes() NoteRepository
|
||||
Follows() FollowRepository
|
||||
InstanceMetadata() InstanceMetadataRepository
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue