Better tracing
Not done yet, still need to add them to just about every other function
This commit is contained in:
parent
83f47d17be
commit
529d106351
13 changed files with 148 additions and 22 deletions
|
@ -5,6 +5,8 @@ import (
|
|||
"strings"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
)
|
||||
|
||||
// various prefixes for accessing items in the cache (since it's a simple key-value store)
|
||||
|
@ -24,6 +26,7 @@ var errCacheNotFound = errors.New("not found in cache")
|
|||
// err contains an error describing why an account's id couldn't be found
|
||||
// The most common one should be errCacheNotFound
|
||||
func (s *Storage) cacheHandleToAccUid(handle string) (accId *string, err error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
// Where to put the data (in case it's found)
|
||||
var target string
|
||||
found, err := s.cache.Get(cacheUserHandleToIdPrefix+strings.TrimLeft(handle, "@"), &target)
|
||||
|
@ -45,6 +48,7 @@ func (s *Storage) cacheHandleToAccUid(handle string) (accId *string, err error)
|
|||
// err contains an error describing why an account's id couldn't be found
|
||||
// The most common one should be errCacheNotFound
|
||||
func (s *Storage) cacheLocalUsernameToAccUid(username string) (accId *string, err error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
// Where to put the data (in case it's found)
|
||||
var target string
|
||||
found, err := s.cache.Get(cacheLocalUsernameToIdPrefix+username, &target)
|
||||
|
@ -62,6 +66,7 @@ func (s *Storage) cacheLocalUsernameToAccUid(username string) (accId *string, er
|
|||
}
|
||||
|
||||
func (s *Storage) cachePkeyIdToAccId(pkeyId []byte) (accId *string, err error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
// Where to put the data (in case it's found)
|
||||
var target string
|
||||
found, err := s.cache.Get(cachePasskeyIdToAccIdPrefix+string(pkeyId), &target)
|
||||
|
@ -83,6 +88,7 @@ func (s *Storage) cachePkeyIdToAccId(pkeyId []byte) (accId *string, err error) {
|
|||
// err contains an error describing why an account couldn't be found
|
||||
// The most common one should be errCacheNotFound
|
||||
func (s *Storage) cacheAccIdToData(id string) (acc *Account, err error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
var target Account
|
||||
found, err := s.cache.Get(cacheUserIdToAccPrefix+id, &target)
|
||||
if !found {
|
||||
|
@ -100,6 +106,7 @@ func (s *Storage) cacheAccIdToData(id string) (acc *Account, err error) {
|
|||
// err contains an error describing why a note couldn't be found
|
||||
// The most common one should be errCacheNotFound
|
||||
func (s *Storage) cacheNoteIdToData(id string) (note *Note, err error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
target := Note{}
|
||||
found, err := s.cache.Get(cacheNoteIdToNotePrefix+id, &target)
|
||||
if !found {
|
||||
|
|
4
storage/cache/cache.go
vendored
4
storage/cache/cache.go
vendored
|
@ -13,6 +13,7 @@ import (
|
|||
"github.com/redis/go-redis/v9"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/config"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
)
|
||||
|
||||
type Cache struct {
|
||||
|
@ -72,6 +73,7 @@ func NewCache(maxSize int64, redisUrl *string) (*Cache, error) {
|
|||
}
|
||||
|
||||
func (c *Cache) Get(key string, target any) (bool, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
return false, nil
|
||||
data, err := c.cache.Get(ctxBackground, key)
|
||||
if err != nil {
|
||||
|
@ -85,6 +87,7 @@ func (c *Cache) Get(key string, target any) (bool, error) {
|
|||
}
|
||||
|
||||
func (c *Cache) Set(key string, value any) error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
return nil
|
||||
data, err := c.encoders.Encode(value)
|
||||
if err != nil {
|
||||
|
@ -95,6 +98,7 @@ func (c *Cache) Set(key string, value any) error {
|
|||
}
|
||||
|
||||
func (c *Cache) Delete(key string) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
// Error return doesn't matter here. Delete is delete is gone
|
||||
_ = c.cache.Delete(ctxBackground, key)
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -35,6 +37,7 @@ type InboundJob struct {
|
|||
}
|
||||
|
||||
func (s *Storage) AddNewInboundJob(data []byte, source InboundJobSource, inboxOwner *string) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
newJob := InboundJob{
|
||||
RawData: data,
|
||||
Source: source,
|
||||
|
@ -45,6 +48,7 @@ func (s *Storage) AddNewInboundJob(data []byte, source InboundJobSource, inboxOw
|
|||
|
||||
// Get the specified amount of jobs, sorted by age (oldest first)
|
||||
func (s *Storage) GetOldestInboundJobs(amount uint) ([]InboundJob, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
jobs := []InboundJob{}
|
||||
switch err := s.db.Order("id asc, created_at asc").Limit(int(amount)).Find(jobs).Error; err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
|
@ -57,6 +61,7 @@ func (s *Storage) GetOldestInboundJobs(amount uint) ([]InboundJob, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) CompleteInboundJob(id uint) error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
s.db.Delete(InboundJob{Model: gorm.Model{ID: id}})
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -3,6 +3,8 @@ package storage
|
|||
import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -31,6 +33,7 @@ type MediaMetadata struct {
|
|||
}
|
||||
|
||||
func (s *Storage) NewMediaMetadata(location, mediaType, name string) (*MediaMetadata, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
newMedia := MediaMetadata{
|
||||
Location: location,
|
||||
Name: name,
|
||||
|
@ -41,6 +44,7 @@ func (s *Storage) NewMediaMetadata(location, mediaType, name string) (*MediaMeta
|
|||
}
|
||||
|
||||
func (s *Storage) FuzzyFindMediaMetadataByName(name string) ([]MediaMetadata, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
notes := []MediaMetadata{}
|
||||
err := s.db.Where("name LIKE %?%", name).Find(notes).Error
|
||||
if err != nil {
|
||||
|
@ -50,6 +54,7 @@ func (s *Storage) FuzzyFindMediaMetadataByName(name string) ([]MediaMetadata, er
|
|||
}
|
||||
|
||||
func (s *Storage) GetMediaMetadataById(id string) (*MediaMetadata, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
media := MediaMetadata{ID: id}
|
||||
err := s.db.First(&media).Error
|
||||
if err != nil {
|
||||
|
@ -59,6 +64,7 @@ func (s *Storage) GetMediaMetadataById(id string) (*MediaMetadata, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) FuzzyFindMediaMetadataByLocation(location string) ([]MediaMetadata, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
data := []MediaMetadata{}
|
||||
if err := s.db.Where("location LIKE %?%", location).Find(data).Error; err != nil {
|
||||
return nil, err
|
||||
|
@ -67,15 +73,18 @@ func (s *Storage) FuzzyFindMediaMetadataByLocation(location string) ([]MediaMeta
|
|||
}
|
||||
|
||||
func (s *Storage) DeleteMediaMetadataById(id string) error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
return s.db.Delete(MediaMetadata{ID: id}).Error
|
||||
}
|
||||
|
||||
func (s *Storage) DeleteMediaMetadataByFuzzyLocation(location string) error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
var tmp MediaMetadata
|
||||
return s.db.Where("location LIKE %?%", location).Delete(&tmp).Error
|
||||
}
|
||||
|
||||
func (s *Storage) DeleteMediaMetadataByFuzzyName(name string) error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
var tmp MediaMetadata
|
||||
return s.db.Where("name LIKE %?%", name).Delete(&tmp).Error
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -40,6 +41,7 @@ type Note struct {
|
|||
}
|
||||
|
||||
func (s *Storage) FindNoteById(id string) (*Note, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
note := &Note{}
|
||||
cacheNote, err := s.cacheNoteIdToData(id)
|
||||
switch err {
|
||||
|
@ -68,6 +70,7 @@ func (s *Storage) FindNoteById(id string) (*Note, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) FindNotesByFuzzyContent(fuzzyContent string) ([]Note, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
notes := []Note{}
|
||||
// TODO: Figure out if cache can be used here too
|
||||
err := s.db.Where("raw_content LIKE %?%", fuzzyContent).Find(notes).Error
|
||||
|
@ -78,6 +81,7 @@ func (s *Storage) FindNotesByFuzzyContent(fuzzyContent string) ([]Note, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) FindNotesByAuthorHandle(handle string) ([]Note, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
acc, err := s.FindAccountByFullHandle(handle)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("account with handle %s not found: %w", handle, err)
|
||||
|
@ -86,6 +90,7 @@ func (s *Storage) FindNotesByAuthorHandle(handle string) ([]Note, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) FindNotesByAuthorId(id string) ([]Note, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
notes := []Note{}
|
||||
err := s.db.Where("creator = ?", id).Find(notes).Error
|
||||
switch err {
|
||||
|
@ -99,6 +104,7 @@ func (s *Storage) FindNotesByAuthorId(id string) ([]Note, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) UpdateNote(note *Note) error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
if note == nil || note.ID == "" {
|
||||
return ErrInvalidData
|
||||
}
|
||||
|
@ -121,6 +127,7 @@ func (s *Storage) CreateNote() (*Note, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) DeleteNote(id string) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
s.cache.Delete(cacheNoteIdToNotePrefix + id)
|
||||
s.db.Delete(Note{ID: id})
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -13,6 +15,7 @@ type OutboundJob struct {
|
|||
}
|
||||
|
||||
func (s *Storage) AddNewOutboundJob(data []byte, targetDomain string, targetUrl string) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
newJob := OutboundJob{
|
||||
Data: data,
|
||||
TargetServer: targetDomain,
|
||||
|
@ -23,6 +26,7 @@ func (s *Storage) AddNewOutboundJob(data []byte, targetDomain string, targetUrl
|
|||
|
||||
// Get the specified amount of jobs, sorted by age (oldest first)
|
||||
func (s *Storage) GetOldestOutboundJobs(amount uint) ([]OutboundJob, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
jobs := []OutboundJob{}
|
||||
err := s.db.Order("id asc, created_at asc").Limit(int(amount)).Find(jobs).Error
|
||||
switch err {
|
||||
|
@ -36,8 +40,13 @@ func (s *Storage) GetOldestOutboundJobs(amount uint) ([]OutboundJob, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) GetOutboundJobsForDomain(domain string, amount uint) ([]OutboundJob, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
jobs := []OutboundJob{}
|
||||
err := s.db.Where("target_server = ?", domain).Order("id asc, created_at asc").Limit(int(amount)).Find(jobs).Error
|
||||
err := s.db.Where("target_server = ?", domain).
|
||||
Order("id asc, created_at asc").
|
||||
Limit(int(amount)).
|
||||
Find(jobs).
|
||||
Error
|
||||
switch err {
|
||||
case gorm.ErrRecordNotFound:
|
||||
return nil, ErrEntryNotFound
|
||||
|
@ -49,6 +58,7 @@ func (s *Storage) GetOutboundJobsForDomain(domain string, amount uint) ([]Outbou
|
|||
}
|
||||
|
||||
func (s *Storage) CompleteOutboundJob(id uint) error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
s.db.Delete(OutboundJob{Model: gorm.Model{ID: id}})
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/go-webauthn/webauthn/webauthn"
|
||||
"github.com/google/uuid"
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
)
|
||||
|
||||
// Session data used during login attempts with a passkey
|
||||
|
@ -18,6 +19,7 @@ type PasskeySession struct {
|
|||
|
||||
// Generate some id for a new session. Just returns a new uuid
|
||||
func (s *Storage) GenSessionID() (string, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
x := uuid.NewString()
|
||||
log.Debug().Str("session-id", x).Msg("Generated new passkey session id")
|
||||
return x, nil
|
||||
|
@ -26,6 +28,7 @@ func (s *Storage) GenSessionID() (string, error) {
|
|||
// Look for an active session with a given id
|
||||
// Returns the session if found and a bool indicating if a session was found
|
||||
func (s *Storage) GetSession(sessionId string) (*webauthn.SessionData, bool) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
log.Debug().Str("id", sessionId).Msg("Looking for passkey session")
|
||||
session := PasskeySession{}
|
||||
res := s.db.Where("id = ?", sessionId).First(&session)
|
||||
|
@ -38,6 +41,7 @@ func (s *Storage) GetSession(sessionId string) (*webauthn.SessionData, bool) {
|
|||
|
||||
// Save (or update) a session with the new data
|
||||
func (s *Storage) SaveSession(token string, data *webauthn.SessionData) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
log.Debug().Str("id", token).Any("webauthn-data", data).Msg("Saving passkey session")
|
||||
session := PasskeySession{
|
||||
ID: token,
|
||||
|
@ -49,6 +53,7 @@ func (s *Storage) SaveSession(token string, data *webauthn.SessionData) {
|
|||
// Delete a session
|
||||
// NOTE: This is a hard delete since the session struct contains no DeletedAt field
|
||||
func (s *Storage) DeleteSession(token string) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
log.Debug().Str("id", token).Msg("Deleting passkey session (if one exists)")
|
||||
s.db.Delete(&PasskeySession{ID: token})
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -14,6 +16,7 @@ type RemoteServer struct {
|
|||
}
|
||||
|
||||
func (s *Storage) FindRemoteServerByDomain(url string) (*RemoteServer, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
server := RemoteServer{}
|
||||
err := s.db.Where("domain = ?").First(&server).Error
|
||||
switch err {
|
||||
|
@ -28,6 +31,7 @@ func (s *Storage) FindRemoteServerByDomain(url string) (*RemoteServer, error) {
|
|||
|
||||
// Find a remote server with a given display name
|
||||
func (s *Storage) FindRemoteServerByDisplayName(displayName string) (*RemoteServer, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
server := RemoteServer{}
|
||||
err := s.db.Where("name = ?", displayName).First(&server).Error
|
||||
switch err {
|
||||
|
@ -41,6 +45,7 @@ func (s *Storage) FindRemoteServerByDisplayName(displayName string) (*RemoteServ
|
|||
}
|
||||
|
||||
func (s *Storage) FindRemoteServerById(id uint) (*RemoteServer, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
server := RemoteServer{}
|
||||
err := s.db.First(&server, id).Error
|
||||
switch err {
|
||||
|
@ -58,6 +63,7 @@ func (s *Storage) NewRemoteServer(
|
|||
url, displayName, icon string,
|
||||
serverType RemoteServerType,
|
||||
) (*RemoteServer, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
_, err := s.FindRemoteServerByDomain(url)
|
||||
switch err {
|
||||
case nil:
|
||||
|
@ -84,6 +90,7 @@ func (s *Storage) NewRemoteServer(
|
|||
// If icon is set, update that
|
||||
// Returns the updated version
|
||||
func (s *Storage) UpdateRemoteServer(url string, displayName, icon *string) (*RemoteServer, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
if displayName == nil && icon == nil {
|
||||
return nil, ErrNothingToChange
|
||||
}
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package storage
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -161,6 +163,8 @@ Misskey "permissions" (no order):
|
|||
*/
|
||||
|
||||
func (s *Storage) NewEmptyRole(name string) (*Role, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
// Check if a role with the given name already exists
|
||||
_, err := s.FindRoleByName(name)
|
||||
switch err {
|
||||
case nil:
|
||||
|
@ -180,6 +184,7 @@ func (s *Storage) NewEmptyRole(name string) (*Role, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) FindRoleByName(name string) (*Role, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
role := Role{}
|
||||
err := s.db.Where("name = ?", name).First(&role).Error
|
||||
switch err {
|
||||
|
@ -193,6 +198,7 @@ func (s *Storage) FindRoleByName(name string) (*Role, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) FindRolesByNames(names []string) ([]Role, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
roles := []Role{}
|
||||
err := s.db.Where("name IN ?", names).Find(&roles).Error
|
||||
switch err {
|
||||
|
@ -204,3 +210,8 @@ func (s *Storage) FindRolesByNames(names []string) ([]Role, error) {
|
|||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Storage) UpdateRole(role *Role) error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
return s.db.Save(role).Error
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import (
|
|||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/config"
|
||||
"gitlab.com/mstarongitlab/linstrom/storage/cache"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
"gorm.io/driver/postgres"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
@ -29,6 +30,7 @@ type Storage struct {
|
|||
}
|
||||
|
||||
func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
db, err := gorm.Open(postgres.Open(dbUrl), &gorm.Config{
|
||||
Logger: newGormLogger(log.Logger),
|
||||
})
|
||||
|
@ -46,6 +48,7 @@ func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
|
|||
OutboundJob{},
|
||||
AccessToken{},
|
||||
Emote{},
|
||||
UserInfoField{},
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to apply migrations: %w", err)
|
||||
|
@ -56,6 +59,11 @@ func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
|
|||
if err = s.insertDefaultRoles(); err != nil {
|
||||
return nil, fmt.Errorf("default roles insertion failed: %w", err)
|
||||
}
|
||||
|
||||
if err = s.insertPlaceholderFile(); err != nil {
|
||||
return nil, fmt.Errorf("placeholder file insertion failed: %w", err)
|
||||
}
|
||||
|
||||
if err = s.insertSelfFromConfig(); err != nil {
|
||||
return nil, fmt.Errorf("self insertion failed: %w", err)
|
||||
}
|
||||
|
@ -64,6 +72,7 @@ func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) insertSelfFromConfig() error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
const ServerActorId = "self"
|
||||
|
||||
var err error
|
||||
|
@ -80,7 +89,7 @@ func (s *Storage) insertSelfFromConfig() error {
|
|||
IsSelf: true,
|
||||
Name: config.GlobalConfig.Self.ServerDisplayName,
|
||||
ServerType: REMOTE_SERVER_LINSTROM,
|
||||
// Icon: "", // TODO: Set to server icon media
|
||||
Icon: "placeholder", // TODO: Set to server icon media
|
||||
}).FirstOrCreate(&serverData).Error
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -107,9 +116,6 @@ func (s *Storage) insertSelfFromConfig() error {
|
|||
IsBot: true,
|
||||
// Followers: []string{},
|
||||
// Follows: []string{},
|
||||
// Icon: "", // TODO: Replace with reference to server icon
|
||||
// Background: "", // TODO: Replace with reference to background media
|
||||
// Banner: "", // TODO: Replace with reference to banner media
|
||||
Indexable: false,
|
||||
RestrictedFollow: false,
|
||||
IdentifiesAs: []Being{},
|
||||
|
@ -120,6 +126,9 @@ func (s *Storage) insertSelfFromConfig() error {
|
|||
Attrs(Account{
|
||||
PublicKey: serverActorPublicKey,
|
||||
PrivateKey: serverActorPrivateKey,
|
||||
Icon: "placeholder",
|
||||
Background: nil,
|
||||
Banner: nil,
|
||||
}).
|
||||
FirstOrCreate(&serverActor).Error
|
||||
if err != nil {
|
||||
|
@ -129,6 +138,7 @@ func (s *Storage) insertSelfFromConfig() error {
|
|||
}
|
||||
|
||||
func (s *Storage) insertDefaultRoles() error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
for _, role := range allDefaultRoles {
|
||||
log.Debug().Str("role-name", role.Name).Msg("Inserting default role")
|
||||
if err := s.db.FirstOrCreate(role).Error; err != nil {
|
||||
|
@ -137,3 +147,16 @@ func (s *Storage) insertDefaultRoles() error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Storage) insertPlaceholderFile() error {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
return s.db.Model(&MediaMetadata{}).Assign(&MediaMetadata{
|
||||
ID: "placeholder",
|
||||
Type: "image/webp",
|
||||
Name: "placeholderFile",
|
||||
Blurred: false,
|
||||
Remote: false,
|
||||
Location: "/placeholder-file",
|
||||
AltText: "Greyscale image of a pidgeon, captioned with the text \"Duck\"",
|
||||
}).FirstOrCreate(&MediaMetadata{}).Error
|
||||
}
|
||||
|
|
|
@ -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")
|
||||
|
|
|
@ -3,6 +3,8 @@ package storage
|
|||
import (
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"gitlab.com/mstarongitlab/linstrom/util"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
|
@ -22,6 +24,7 @@ type UserInfoField struct {
|
|||
// TODO: Add functions to store, load, update and delete these
|
||||
|
||||
func (s *Storage) FindUserFieldById(id uint) (*UserInfoField, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
entry := UserInfoField{}
|
||||
err := s.db.First(&entry, id).Error
|
||||
switch err {
|
||||
|
@ -35,6 +38,7 @@ func (s *Storage) FindUserFieldById(id uint) (*UserInfoField, error) {
|
|||
}
|
||||
|
||||
func (s *Storage) FindMultipleUserFieldsById(ids []uint) ([]UserInfoField, error) {
|
||||
defer util.Untrace(util.Trace(&log.Logger))
|
||||
entries := []UserInfoField{}
|
||||
err := s.db.Where(ids).Find(&entries).Error
|
||||
switch err {
|
||||
|
|
20
util/tracing.go
Normal file
20
util/tracing.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
func Trace(l *zerolog.Logger) *zerolog.Logger {
|
||||
if e := l.Trace(); e.Enabled() {
|
||||
e.Caller(2).
|
||||
Msg("Entered function")
|
||||
}
|
||||
return l
|
||||
}
|
||||
|
||||
func Untrace(l *zerolog.Logger) {
|
||||
if e := l.Trace(); e.Enabled() {
|
||||
e.Caller(2).
|
||||
Msg("Exited function")
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue