Better tracing

Not done yet, still need to add them to just about every other function
This commit is contained in:
Melody Becker 2024-11-06 16:57:44 +01:00
parent 83f47d17be
commit 529d106351
13 changed files with 148 additions and 22 deletions

View file

@ -13,6 +13,7 @@ import (
"github.com/mstarongithub/passkey"
"github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/linstrom/ap"
"gitlab.com/mstarongitlab/linstrom/util"
"gorm.io/gorm"
)
@ -42,8 +43,8 @@ type Account struct {
Follows []string `gorm:"serializer:json"` // List of account ids this account follows
Followers []string `gorm:"serializer:json"` // List of account ids that follow this account
Icon string // ID of a media file used as icon
Background string // ID of a media file used as background image
Banner string // ID of a media file used as banner
Background *string // ID of a media file used as background image
Banner *string // ID of a media file used as banner
Indexable bool // Whether this account can be found by crawlers
PublicKey []byte // The public key of the account
// Whether this account restricts following
@ -102,7 +103,7 @@ type RemoteAccountLinks struct {
// Find an account in the db using a given full handle (@max@example.com)
// Returns an account and nil if an account is found, otherwise nil and the error
func (s *Storage) FindAccountByFullHandle(handle string) (*Account, error) {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
log.Debug().Str("account-handle", handle).Msg("Looking for account by handle")
log.Debug().Str("account-handle", handle).Msg("Checking if there's a cache hit")
@ -155,7 +156,7 @@ func (s *Storage) FindAccountByFullHandle(handle string) (*Account, error) {
// Find an account given a specific ID
func (s *Storage) FindAccountById(id string) (*Account, error) {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
log.Debug().Str("account-id", id).Msg("Looking for account by id")
log.Debug().Str("account-id", id).Msg("First trying to hit cache")
acc, err := s.cacheAccIdToData(id)
@ -173,10 +174,11 @@ func (s *Storage) FindAccountById(id string) (*Account, error) {
if res.Error != nil {
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
log.Warn().Str("account-id", id).Msg("Account not found")
return nil, ErrEntryNotFound
} else {
log.Error().Err(res.Error).Str("account-id", id).Msg("Failed to look for account")
return nil, res.Error
}
return nil, res.Error
}
log.Info().Str("account-id", id).Msg("Found account in db, also adding to cache")
if err = s.cache.Set(cacheUserIdToAccPrefix+id, acc); err != nil {
@ -186,7 +188,7 @@ func (s *Storage) FindAccountById(id string) (*Account, error) {
}
func (s *Storage) FindLocalAccountByUsername(username string) (*Account, error) {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
log.Debug().Str("account-username", username).Msg("Looking for local account")
log.Debug().Str("account-username", username).Msg("Checking cache first")
@ -237,7 +239,7 @@ func (s *Storage) FindLocalAccountByUsername(username string) (*Account, error)
}
func (s *Storage) FindAccountByPasskeyId(pkeyId []byte) (*Account, error) {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
log.Debug().Bytes("account-passkey-id", pkeyId).Msg("Looking for account")
log.Debug().Bytes("account-passkey-id", pkeyId).Msg("Checking cache first")
@ -288,6 +290,7 @@ func (s *Storage) FindAccountByPasskeyId(pkeyId []byte) (*Account, error) {
// Update a given account in storage and cache
func (s *Storage) UpdateAccount(acc *Account) error {
defer util.Untrace(util.Trace(&log.Logger))
// If the account is nil or doesn't have an id, error out
if acc == nil || acc.ID == "" {
return ErrInvalidData
@ -304,7 +307,7 @@ func (s *Storage) UpdateAccount(acc *Account) error {
// Create a new empty account for future use
func (s *Storage) NewEmptyAccount() (*Account, error) {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
log.Debug().Msg("Creating new empty account")
acc := Account{}
// Generate the 64 bit id for passkey and webauthn stuff
@ -322,6 +325,10 @@ func (s *Storage) NewEmptyAccount() (*Account, error) {
if err != nil {
return nil, fmt.Errorf("failed to generate account role for new account: %w", err)
}
accountRole.IsUserRole = true
if err = s.UpdateRole(accountRole); err != nil {
return nil, fmt.Errorf("failed to generate account role for new account: %w", err)
}
acc.WebAuthnId = data
acc.Followers = []string{}
@ -332,6 +339,7 @@ func (s *Storage) NewEmptyAccount() (*Account, error) {
acc.IdentifiesAs = []Being{}
acc.PasskeyCredentials = []webauthn.Credential{}
acc.Roles = []string{DefaultUserRole.Name, accountRole.Name}
acc.Icon = "placeholder"
log.Debug().Any("account", &acc).Msg("Saving new account in db")
res := s.db.Save(&acc)
if res.Error != nil {
@ -346,6 +354,7 @@ func (s *Storage) NewEmptyAccount() (*Account, error) {
// The handle in this case is only the part before the domain (example: @bob@example.com would have a handle of bob)
// It also sets up a bunch of values that tend to be obvious for local accounts
func (s *Storage) NewLocalAccount(handle string) (*Account, error) {
defer util.Untrace(util.Trace(&log.Logger))
log.Trace().Caller().Send()
log.Debug().Str("account-handle", handle).Msg("Creating new local account")
acc, err := s.NewEmptyAccount()
@ -382,31 +391,36 @@ func (s *Storage) NewLocalAccount(handle string) (*Account, error) {
return acc, nil
}
func (s *Storage) DeleteAccount(accId string) error {
// TODO: Implement me
panic("Not implemented")
}
// ---- Section WebAuthn.User
// Implements the webauthn.User interface for interaction with passkeys
func (a *Account) WebAuthnID() []byte {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
return a.WebAuthnId
}
func (u *Account) WebAuthnName() string {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
return u.Username
}
func (u *Account) WebAuthnDisplayName() string {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
return u.DisplayName
}
func (u *Account) WebAuthnCredentials() []webauthn.Credential {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
return u.PasskeyCredentials
}
func (u *Account) WebAuthnIcon() string {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
return ""
}
@ -414,7 +428,7 @@ func (u *Account) WebAuthnIcon() string {
// Implements the passkey.User interface
func (u *Account) PutCredential(new webauthn.Credential) {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
u.PasskeyCredentials = append(u.PasskeyCredentials, new)
}
@ -422,7 +436,7 @@ func (u *Account) PutCredential(new webauthn.Credential) {
// Implements the passkey.UserStore interface
func (s *Storage) GetOrCreateUser(userID string) passkey.User {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
log.Debug().
Str("account-handle", userID).
Msg("Looking for or creating account for passkey stuff")
@ -445,7 +459,7 @@ func (s *Storage) GetOrCreateUser(userID string) passkey.User {
}
func (s *Storage) GetUserByWebAuthnId(id []byte) passkey.User {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
log.Debug().Bytes("webauthn-id", id).Msg("Looking for account with webauthn id")
acc := Account{}
res := s.db.Where(Account{WebAuthnId: id}).First(&acc)
@ -461,7 +475,7 @@ func (s *Storage) GetUserByWebAuthnId(id []byte) passkey.User {
}
func (s *Storage) SaveUser(rawUser passkey.User) {
log.Trace().Caller().Send()
defer util.Untrace(util.Trace(&log.Logger))
user, ok := rawUser.(*Account)
if !ok {
log.Error().Any("raw-user", rawUser).Msg("Failed to cast raw user to db account")