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"
|
"strings"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"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)
|
// 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
|
// err contains an error describing why an account's id couldn't be found
|
||||||
// The most common one should be errCacheNotFound
|
// The most common one should be errCacheNotFound
|
||||||
func (s *Storage) cacheHandleToAccUid(handle string) (accId *string, err error) {
|
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)
|
// Where to put the data (in case it's found)
|
||||||
var target string
|
var target string
|
||||||
found, err := s.cache.Get(cacheUserHandleToIdPrefix+strings.TrimLeft(handle, "@"), &target)
|
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
|
// err contains an error describing why an account's id couldn't be found
|
||||||
// The most common one should be errCacheNotFound
|
// The most common one should be errCacheNotFound
|
||||||
func (s *Storage) cacheLocalUsernameToAccUid(username string) (accId *string, err error) {
|
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)
|
// Where to put the data (in case it's found)
|
||||||
var target string
|
var target string
|
||||||
found, err := s.cache.Get(cacheLocalUsernameToIdPrefix+username, &target)
|
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) {
|
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)
|
// Where to put the data (in case it's found)
|
||||||
var target string
|
var target string
|
||||||
found, err := s.cache.Get(cachePasskeyIdToAccIdPrefix+string(pkeyId), &target)
|
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
|
// err contains an error describing why an account couldn't be found
|
||||||
// The most common one should be errCacheNotFound
|
// The most common one should be errCacheNotFound
|
||||||
func (s *Storage) cacheAccIdToData(id string) (acc *Account, err error) {
|
func (s *Storage) cacheAccIdToData(id string) (acc *Account, err error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
var target Account
|
var target Account
|
||||||
found, err := s.cache.Get(cacheUserIdToAccPrefix+id, &target)
|
found, err := s.cache.Get(cacheUserIdToAccPrefix+id, &target)
|
||||||
if !found {
|
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
|
// err contains an error describing why a note couldn't be found
|
||||||
// The most common one should be errCacheNotFound
|
// The most common one should be errCacheNotFound
|
||||||
func (s *Storage) cacheNoteIdToData(id string) (note *Note, err error) {
|
func (s *Storage) cacheNoteIdToData(id string) (note *Note, err error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
target := Note{}
|
target := Note{}
|
||||||
found, err := s.cache.Get(cacheNoteIdToNotePrefix+id, &target)
|
found, err := s.cache.Get(cacheNoteIdToNotePrefix+id, &target)
|
||||||
if !found {
|
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/redis/go-redis/v9"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"gitlab.com/mstarongitlab/linstrom/config"
|
"gitlab.com/mstarongitlab/linstrom/config"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cache struct {
|
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) {
|
func (c *Cache) Get(key string, target any) (bool, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
return false, nil
|
return false, nil
|
||||||
data, err := c.cache.Get(ctxBackground, key)
|
data, err := c.cache.Get(ctxBackground, key)
|
||||||
if err != nil {
|
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 {
|
func (c *Cache) Set(key string, value any) error {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
return nil
|
return nil
|
||||||
data, err := c.encoders.Encode(value)
|
data, err := c.encoders.Encode(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -95,6 +98,7 @@ func (c *Cache) Set(key string, value any) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Delete(key string) {
|
func (c *Cache) Delete(key string) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
// Error return doesn't matter here. Delete is delete is gone
|
// Error return doesn't matter here. Delete is delete is gone
|
||||||
_ = c.cache.Delete(ctxBackground, key)
|
_ = c.cache.Delete(ctxBackground, key)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -35,6 +37,7 @@ type InboundJob struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) AddNewInboundJob(data []byte, source InboundJobSource, inboxOwner *string) {
|
func (s *Storage) AddNewInboundJob(data []byte, source InboundJobSource, inboxOwner *string) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
newJob := InboundJob{
|
newJob := InboundJob{
|
||||||
RawData: data,
|
RawData: data,
|
||||||
Source: source,
|
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)
|
// Get the specified amount of jobs, sorted by age (oldest first)
|
||||||
func (s *Storage) GetOldestInboundJobs(amount uint) ([]InboundJob, error) {
|
func (s *Storage) GetOldestInboundJobs(amount uint) ([]InboundJob, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
jobs := []InboundJob{}
|
jobs := []InboundJob{}
|
||||||
switch err := s.db.Order("id asc, created_at asc").Limit(int(amount)).Find(jobs).Error; err {
|
switch err := s.db.Order("id asc, created_at asc").Limit(int(amount)).Find(jobs).Error; err {
|
||||||
case gorm.ErrRecordNotFound:
|
case gorm.ErrRecordNotFound:
|
||||||
|
@ -57,6 +61,7 @@ func (s *Storage) GetOldestInboundJobs(amount uint) ([]InboundJob, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) CompleteInboundJob(id uint) error {
|
func (s *Storage) CompleteInboundJob(id uint) error {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
s.db.Delete(InboundJob{Model: gorm.Model{ID: id}})
|
s.db.Delete(InboundJob{Model: gorm.Model{ID: id}})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@ package storage
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -31,6 +33,7 @@ type MediaMetadata struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) NewMediaMetadata(location, mediaType, name string) (*MediaMetadata, error) {
|
func (s *Storage) NewMediaMetadata(location, mediaType, name string) (*MediaMetadata, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
newMedia := MediaMetadata{
|
newMedia := MediaMetadata{
|
||||||
Location: location,
|
Location: location,
|
||||||
Name: name,
|
Name: name,
|
||||||
|
@ -41,6 +44,7 @@ func (s *Storage) NewMediaMetadata(location, mediaType, name string) (*MediaMeta
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) FuzzyFindMediaMetadataByName(name string) ([]MediaMetadata, error) {
|
func (s *Storage) FuzzyFindMediaMetadataByName(name string) ([]MediaMetadata, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
notes := []MediaMetadata{}
|
notes := []MediaMetadata{}
|
||||||
err := s.db.Where("name LIKE %?%", name).Find(notes).Error
|
err := s.db.Where("name LIKE %?%", name).Find(notes).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -50,6 +54,7 @@ func (s *Storage) FuzzyFindMediaMetadataByName(name string) ([]MediaMetadata, er
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) GetMediaMetadataById(id string) (*MediaMetadata, error) {
|
func (s *Storage) GetMediaMetadataById(id string) (*MediaMetadata, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
media := MediaMetadata{ID: id}
|
media := MediaMetadata{ID: id}
|
||||||
err := s.db.First(&media).Error
|
err := s.db.First(&media).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -59,6 +64,7 @@ func (s *Storage) GetMediaMetadataById(id string) (*MediaMetadata, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) FuzzyFindMediaMetadataByLocation(location string) ([]MediaMetadata, error) {
|
func (s *Storage) FuzzyFindMediaMetadataByLocation(location string) ([]MediaMetadata, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
data := []MediaMetadata{}
|
data := []MediaMetadata{}
|
||||||
if err := s.db.Where("location LIKE %?%", location).Find(data).Error; err != nil {
|
if err := s.db.Where("location LIKE %?%", location).Find(data).Error; err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -67,15 +73,18 @@ func (s *Storage) FuzzyFindMediaMetadataByLocation(location string) ([]MediaMeta
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) DeleteMediaMetadataById(id string) error {
|
func (s *Storage) DeleteMediaMetadataById(id string) error {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
return s.db.Delete(MediaMetadata{ID: id}).Error
|
return s.db.Delete(MediaMetadata{ID: id}).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) DeleteMediaMetadataByFuzzyLocation(location string) error {
|
func (s *Storage) DeleteMediaMetadataByFuzzyLocation(location string) error {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
var tmp MediaMetadata
|
var tmp MediaMetadata
|
||||||
return s.db.Where("location LIKE %?%", location).Delete(&tmp).Error
|
return s.db.Where("location LIKE %?%", location).Delete(&tmp).Error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) DeleteMediaMetadataByFuzzyName(name string) error {
|
func (s *Storage) DeleteMediaMetadataByFuzzyName(name string) error {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
var tmp MediaMetadata
|
var tmp MediaMetadata
|
||||||
return s.db.Where("name LIKE %?%", name).Delete(&tmp).Error
|
return s.db.Where("name LIKE %?%", name).Delete(&tmp).Error
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,6 +41,7 @@ type Note struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) FindNoteById(id string) (*Note, error) {
|
func (s *Storage) FindNoteById(id string) (*Note, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
note := &Note{}
|
note := &Note{}
|
||||||
cacheNote, err := s.cacheNoteIdToData(id)
|
cacheNote, err := s.cacheNoteIdToData(id)
|
||||||
switch err {
|
switch err {
|
||||||
|
@ -68,6 +70,7 @@ func (s *Storage) FindNoteById(id string) (*Note, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) FindNotesByFuzzyContent(fuzzyContent string) ([]Note, error) {
|
func (s *Storage) FindNotesByFuzzyContent(fuzzyContent string) ([]Note, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
notes := []Note{}
|
notes := []Note{}
|
||||||
// TODO: Figure out if cache can be used here too
|
// TODO: Figure out if cache can be used here too
|
||||||
err := s.db.Where("raw_content LIKE %?%", fuzzyContent).Find(notes).Error
|
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) {
|
func (s *Storage) FindNotesByAuthorHandle(handle string) ([]Note, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
acc, err := s.FindAccountByFullHandle(handle)
|
acc, err := s.FindAccountByFullHandle(handle)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("account with handle %s not found: %w", handle, err)
|
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) {
|
func (s *Storage) FindNotesByAuthorId(id string) ([]Note, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
notes := []Note{}
|
notes := []Note{}
|
||||||
err := s.db.Where("creator = ?", id).Find(notes).Error
|
err := s.db.Where("creator = ?", id).Find(notes).Error
|
||||||
switch err {
|
switch err {
|
||||||
|
@ -99,6 +104,7 @@ func (s *Storage) FindNotesByAuthorId(id string) ([]Note, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) UpdateNote(note *Note) error {
|
func (s *Storage) UpdateNote(note *Note) error {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
if note == nil || note.ID == "" {
|
if note == nil || note.ID == "" {
|
||||||
return ErrInvalidData
|
return ErrInvalidData
|
||||||
}
|
}
|
||||||
|
@ -121,6 +127,7 @@ func (s *Storage) CreateNote() (*Note, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) DeleteNote(id string) {
|
func (s *Storage) DeleteNote(id string) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
s.cache.Delete(cacheNoteIdToNotePrefix + id)
|
s.cache.Delete(cacheNoteIdToNotePrefix + id)
|
||||||
s.db.Delete(Note{ID: id})
|
s.db.Delete(Note{ID: id})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,6 +15,7 @@ type OutboundJob struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) AddNewOutboundJob(data []byte, targetDomain string, targetUrl string) {
|
func (s *Storage) AddNewOutboundJob(data []byte, targetDomain string, targetUrl string) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
newJob := OutboundJob{
|
newJob := OutboundJob{
|
||||||
Data: data,
|
Data: data,
|
||||||
TargetServer: targetDomain,
|
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)
|
// Get the specified amount of jobs, sorted by age (oldest first)
|
||||||
func (s *Storage) GetOldestOutboundJobs(amount uint) ([]OutboundJob, error) {
|
func (s *Storage) GetOldestOutboundJobs(amount uint) ([]OutboundJob, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
jobs := []OutboundJob{}
|
jobs := []OutboundJob{}
|
||||||
err := s.db.Order("id asc, created_at asc").Limit(int(amount)).Find(jobs).Error
|
err := s.db.Order("id asc, created_at asc").Limit(int(amount)).Find(jobs).Error
|
||||||
switch err {
|
switch err {
|
||||||
|
@ -36,8 +40,13 @@ func (s *Storage) GetOldestOutboundJobs(amount uint) ([]OutboundJob, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) GetOutboundJobsForDomain(domain string, amount uint) ([]OutboundJob, error) {
|
func (s *Storage) GetOutboundJobsForDomain(domain string, amount uint) ([]OutboundJob, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
jobs := []OutboundJob{}
|
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 {
|
switch err {
|
||||||
case gorm.ErrRecordNotFound:
|
case gorm.ErrRecordNotFound:
|
||||||
return nil, ErrEntryNotFound
|
return nil, ErrEntryNotFound
|
||||||
|
@ -49,6 +58,7 @@ func (s *Storage) GetOutboundJobsForDomain(domain string, amount uint) ([]Outbou
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) CompleteOutboundJob(id uint) error {
|
func (s *Storage) CompleteOutboundJob(id uint) error {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
s.db.Delete(OutboundJob{Model: gorm.Model{ID: id}})
|
s.db.Delete(OutboundJob{Model: gorm.Model{ID: id}})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"github.com/go-webauthn/webauthn/webauthn"
|
"github.com/go-webauthn/webauthn/webauthn"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Session data used during login attempts with a passkey
|
// 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
|
// Generate some id for a new session. Just returns a new uuid
|
||||||
func (s *Storage) GenSessionID() (string, error) {
|
func (s *Storage) GenSessionID() (string, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
x := uuid.NewString()
|
x := uuid.NewString()
|
||||||
log.Debug().Str("session-id", x).Msg("Generated new passkey session id")
|
log.Debug().Str("session-id", x).Msg("Generated new passkey session id")
|
||||||
return x, nil
|
return x, nil
|
||||||
|
@ -26,6 +28,7 @@ func (s *Storage) GenSessionID() (string, error) {
|
||||||
// Look for an active session with a given id
|
// Look for an active session with a given id
|
||||||
// Returns the session if found and a bool indicating if a session was found
|
// Returns the session if found and a bool indicating if a session was found
|
||||||
func (s *Storage) GetSession(sessionId string) (*webauthn.SessionData, bool) {
|
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")
|
log.Debug().Str("id", sessionId).Msg("Looking for passkey session")
|
||||||
session := PasskeySession{}
|
session := PasskeySession{}
|
||||||
res := s.db.Where("id = ?", sessionId).First(&session)
|
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
|
// Save (or update) a session with the new data
|
||||||
func (s *Storage) SaveSession(token string, data *webauthn.SessionData) {
|
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")
|
log.Debug().Str("id", token).Any("webauthn-data", data).Msg("Saving passkey session")
|
||||||
session := PasskeySession{
|
session := PasskeySession{
|
||||||
ID: token,
|
ID: token,
|
||||||
|
@ -49,6 +53,7 @@ func (s *Storage) SaveSession(token string, data *webauthn.SessionData) {
|
||||||
// Delete a session
|
// Delete a session
|
||||||
// NOTE: This is a hard delete since the session struct contains no DeletedAt field
|
// NOTE: This is a hard delete since the session struct contains no DeletedAt field
|
||||||
func (s *Storage) DeleteSession(token string) {
|
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)")
|
log.Debug().Str("id", token).Msg("Deleting passkey session (if one exists)")
|
||||||
s.db.Delete(&PasskeySession{ID: token})
|
s.db.Delete(&PasskeySession{ID: token})
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -14,6 +16,7 @@ type RemoteServer struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) FindRemoteServerByDomain(url string) (*RemoteServer, error) {
|
func (s *Storage) FindRemoteServerByDomain(url string) (*RemoteServer, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
server := RemoteServer{}
|
server := RemoteServer{}
|
||||||
err := s.db.Where("domain = ?").First(&server).Error
|
err := s.db.Where("domain = ?").First(&server).Error
|
||||||
switch err {
|
switch err {
|
||||||
|
@ -28,6 +31,7 @@ func (s *Storage) FindRemoteServerByDomain(url string) (*RemoteServer, error) {
|
||||||
|
|
||||||
// Find a remote server with a given display name
|
// Find a remote server with a given display name
|
||||||
func (s *Storage) FindRemoteServerByDisplayName(displayName string) (*RemoteServer, error) {
|
func (s *Storage) FindRemoteServerByDisplayName(displayName string) (*RemoteServer, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
server := RemoteServer{}
|
server := RemoteServer{}
|
||||||
err := s.db.Where("name = ?", displayName).First(&server).Error
|
err := s.db.Where("name = ?", displayName).First(&server).Error
|
||||||
switch err {
|
switch err {
|
||||||
|
@ -41,6 +45,7 @@ func (s *Storage) FindRemoteServerByDisplayName(displayName string) (*RemoteServ
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) FindRemoteServerById(id uint) (*RemoteServer, error) {
|
func (s *Storage) FindRemoteServerById(id uint) (*RemoteServer, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
server := RemoteServer{}
|
server := RemoteServer{}
|
||||||
err := s.db.First(&server, id).Error
|
err := s.db.First(&server, id).Error
|
||||||
switch err {
|
switch err {
|
||||||
|
@ -58,6 +63,7 @@ func (s *Storage) NewRemoteServer(
|
||||||
url, displayName, icon string,
|
url, displayName, icon string,
|
||||||
serverType RemoteServerType,
|
serverType RemoteServerType,
|
||||||
) (*RemoteServer, error) {
|
) (*RemoteServer, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
_, err := s.FindRemoteServerByDomain(url)
|
_, err := s.FindRemoteServerByDomain(url)
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
|
@ -84,6 +90,7 @@ func (s *Storage) NewRemoteServer(
|
||||||
// If icon is set, update that
|
// If icon is set, update that
|
||||||
// Returns the updated version
|
// Returns the updated version
|
||||||
func (s *Storage) UpdateRemoteServer(url string, displayName, icon *string) (*RemoteServer, error) {
|
func (s *Storage) UpdateRemoteServer(url string, displayName, icon *string) (*RemoteServer, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
if displayName == nil && icon == nil {
|
if displayName == nil && icon == nil {
|
||||||
return nil, ErrNothingToChange
|
return nil, ErrNothingToChange
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package storage
|
package storage
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -161,6 +163,8 @@ Misskey "permissions" (no order):
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func (s *Storage) NewEmptyRole(name string) (*Role, error) {
|
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)
|
_, err := s.FindRoleByName(name)
|
||||||
switch err {
|
switch err {
|
||||||
case nil:
|
case nil:
|
||||||
|
@ -180,6 +184,7 @@ func (s *Storage) NewEmptyRole(name string) (*Role, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) FindRoleByName(name string) (*Role, error) {
|
func (s *Storage) FindRoleByName(name string) (*Role, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
role := Role{}
|
role := Role{}
|
||||||
err := s.db.Where("name = ?", name).First(&role).Error
|
err := s.db.Where("name = ?", name).First(&role).Error
|
||||||
switch err {
|
switch err {
|
||||||
|
@ -193,6 +198,7 @@ func (s *Storage) FindRoleByName(name string) (*Role, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) FindRolesByNames(names []string) ([]Role, error) {
|
func (s *Storage) FindRolesByNames(names []string) ([]Role, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
roles := []Role{}
|
roles := []Role{}
|
||||||
err := s.db.Where("name IN ?", names).Find(&roles).Error
|
err := s.db.Where("name IN ?", names).Find(&roles).Error
|
||||||
switch err {
|
switch err {
|
||||||
|
@ -204,3 +210,8 @@ func (s *Storage) FindRolesByNames(names []string) ([]Role, error) {
|
||||||
return nil, err
|
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"
|
"github.com/rs/zerolog/log"
|
||||||
"gitlab.com/mstarongitlab/linstrom/config"
|
"gitlab.com/mstarongitlab/linstrom/config"
|
||||||
"gitlab.com/mstarongitlab/linstrom/storage/cache"
|
"gitlab.com/mstarongitlab/linstrom/storage/cache"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
@ -29,6 +30,7 @@ type Storage struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
|
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{
|
db, err := gorm.Open(postgres.Open(dbUrl), &gorm.Config{
|
||||||
Logger: newGormLogger(log.Logger),
|
Logger: newGormLogger(log.Logger),
|
||||||
})
|
})
|
||||||
|
@ -46,6 +48,7 @@ func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
|
||||||
OutboundJob{},
|
OutboundJob{},
|
||||||
AccessToken{},
|
AccessToken{},
|
||||||
Emote{},
|
Emote{},
|
||||||
|
UserInfoField{},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to apply migrations: %w", err)
|
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 {
|
if err = s.insertDefaultRoles(); err != nil {
|
||||||
return nil, fmt.Errorf("default roles insertion failed: %w", err)
|
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 {
|
if err = s.insertSelfFromConfig(); err != nil {
|
||||||
return nil, fmt.Errorf("self insertion failed: %w", err)
|
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 {
|
func (s *Storage) insertSelfFromConfig() error {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
const ServerActorId = "self"
|
const ServerActorId = "self"
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
@ -80,7 +89,7 @@ func (s *Storage) insertSelfFromConfig() error {
|
||||||
IsSelf: true,
|
IsSelf: true,
|
||||||
Name: config.GlobalConfig.Self.ServerDisplayName,
|
Name: config.GlobalConfig.Self.ServerDisplayName,
|
||||||
ServerType: REMOTE_SERVER_LINSTROM,
|
ServerType: REMOTE_SERVER_LINSTROM,
|
||||||
// Icon: "", // TODO: Set to server icon media
|
Icon: "placeholder", // TODO: Set to server icon media
|
||||||
}).FirstOrCreate(&serverData).Error
|
}).FirstOrCreate(&serverData).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -107,9 +116,6 @@ func (s *Storage) insertSelfFromConfig() error {
|
||||||
IsBot: true,
|
IsBot: true,
|
||||||
// Followers: []string{},
|
// Followers: []string{},
|
||||||
// Follows: []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,
|
Indexable: false,
|
||||||
RestrictedFollow: false,
|
RestrictedFollow: false,
|
||||||
IdentifiesAs: []Being{},
|
IdentifiesAs: []Being{},
|
||||||
|
@ -120,6 +126,9 @@ func (s *Storage) insertSelfFromConfig() error {
|
||||||
Attrs(Account{
|
Attrs(Account{
|
||||||
PublicKey: serverActorPublicKey,
|
PublicKey: serverActorPublicKey,
|
||||||
PrivateKey: serverActorPrivateKey,
|
PrivateKey: serverActorPrivateKey,
|
||||||
|
Icon: "placeholder",
|
||||||
|
Background: nil,
|
||||||
|
Banner: nil,
|
||||||
}).
|
}).
|
||||||
FirstOrCreate(&serverActor).Error
|
FirstOrCreate(&serverActor).Error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -129,6 +138,7 @@ func (s *Storage) insertSelfFromConfig() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) insertDefaultRoles() error {
|
func (s *Storage) insertDefaultRoles() error {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
for _, role := range allDefaultRoles {
|
for _, role := range allDefaultRoles {
|
||||||
log.Debug().Str("role-name", role.Name).Msg("Inserting default role")
|
log.Debug().Str("role-name", role.Name).Msg("Inserting default role")
|
||||||
if err := s.db.FirstOrCreate(role).Error; err != nil {
|
if err := s.db.FirstOrCreate(role).Error; err != nil {
|
||||||
|
@ -137,3 +147,16 @@ func (s *Storage) insertDefaultRoles() error {
|
||||||
}
|
}
|
||||||
return nil
|
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/mstarongithub/passkey"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"gitlab.com/mstarongitlab/linstrom/ap"
|
"gitlab.com/mstarongitlab/linstrom/ap"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -42,8 +43,8 @@ type Account struct {
|
||||||
Follows []string `gorm:"serializer:json"` // List of account ids this account follows
|
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
|
Followers []string `gorm:"serializer:json"` // List of account ids that follow this account
|
||||||
Icon string // ID of a media file used as icon
|
Icon string // ID of a media file used as icon
|
||||||
Background string // ID of a media file used as background image
|
Background *string // ID of a media file used as background image
|
||||||
Banner string // ID of a media file used as banner
|
Banner *string // ID of a media file used as banner
|
||||||
Indexable bool // Whether this account can be found by crawlers
|
Indexable bool // Whether this account can be found by crawlers
|
||||||
PublicKey []byte // The public key of the account
|
PublicKey []byte // The public key of the account
|
||||||
// Whether this account restricts following
|
// 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)
|
// 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
|
// Returns an account and nil if an account is found, otherwise nil and the error
|
||||||
func (s *Storage) FindAccountByFullHandle(handle string) (*Account, 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("Looking for account by handle")
|
||||||
log.Debug().Str("account-handle", handle).Msg("Checking if there's a cache hit")
|
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
|
// Find an account given a specific ID
|
||||||
func (s *Storage) FindAccountById(id string) (*Account, error) {
|
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("Looking for account by id")
|
||||||
log.Debug().Str("account-id", id).Msg("First trying to hit cache")
|
log.Debug().Str("account-id", id).Msg("First trying to hit cache")
|
||||||
acc, err := s.cacheAccIdToData(id)
|
acc, err := s.cacheAccIdToData(id)
|
||||||
|
@ -173,11 +174,12 @@ func (s *Storage) FindAccountById(id string) (*Account, error) {
|
||||||
if res.Error != nil {
|
if res.Error != nil {
|
||||||
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
if errors.Is(res.Error, gorm.ErrRecordNotFound) {
|
||||||
log.Warn().Str("account-id", id).Msg("Account not found")
|
log.Warn().Str("account-id", id).Msg("Account not found")
|
||||||
|
return nil, ErrEntryNotFound
|
||||||
} else {
|
} else {
|
||||||
log.Error().Err(res.Error).Str("account-id", id).Msg("Failed to look for account")
|
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")
|
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 {
|
if err = s.cache.Set(cacheUserIdToAccPrefix+id, acc); err != nil {
|
||||||
log.Warn().Err(err).Str("account-id", id).Msg("Failed to add account to cache")
|
log.Warn().Err(err).Str("account-id", id).Msg("Failed to add account to cache")
|
||||||
|
@ -186,7 +188,7 @@ func (s *Storage) FindAccountById(id string) (*Account, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) FindLocalAccountByUsername(username 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("Looking for local account")
|
||||||
log.Debug().Str("account-username", username).Msg("Checking cache first")
|
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) {
|
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("Looking for account")
|
||||||
log.Debug().Bytes("account-passkey-id", pkeyId).Msg("Checking cache first")
|
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
|
// Update a given account in storage and cache
|
||||||
func (s *Storage) UpdateAccount(acc *Account) error {
|
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 the account is nil or doesn't have an id, error out
|
||||||
if acc == nil || acc.ID == "" {
|
if acc == nil || acc.ID == "" {
|
||||||
return ErrInvalidData
|
return ErrInvalidData
|
||||||
|
@ -304,7 +307,7 @@ func (s *Storage) UpdateAccount(acc *Account) error {
|
||||||
|
|
||||||
// Create a new empty account for future use
|
// Create a new empty account for future use
|
||||||
func (s *Storage) NewEmptyAccount() (*Account, error) {
|
func (s *Storage) NewEmptyAccount() (*Account, error) {
|
||||||
log.Trace().Caller().Send()
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
log.Debug().Msg("Creating new empty account")
|
log.Debug().Msg("Creating new empty account")
|
||||||
acc := Account{}
|
acc := Account{}
|
||||||
// Generate the 64 bit id for passkey and webauthn stuff
|
// Generate the 64 bit id for passkey and webauthn stuff
|
||||||
|
@ -322,6 +325,10 @@ func (s *Storage) NewEmptyAccount() (*Account, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to generate account role for new account: %w", err)
|
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.WebAuthnId = data
|
||||||
acc.Followers = []string{}
|
acc.Followers = []string{}
|
||||||
|
@ -332,6 +339,7 @@ func (s *Storage) NewEmptyAccount() (*Account, error) {
|
||||||
acc.IdentifiesAs = []Being{}
|
acc.IdentifiesAs = []Being{}
|
||||||
acc.PasskeyCredentials = []webauthn.Credential{}
|
acc.PasskeyCredentials = []webauthn.Credential{}
|
||||||
acc.Roles = []string{DefaultUserRole.Name, accountRole.Name}
|
acc.Roles = []string{DefaultUserRole.Name, accountRole.Name}
|
||||||
|
acc.Icon = "placeholder"
|
||||||
log.Debug().Any("account", &acc).Msg("Saving new account in db")
|
log.Debug().Any("account", &acc).Msg("Saving new account in db")
|
||||||
res := s.db.Save(&acc)
|
res := s.db.Save(&acc)
|
||||||
if res.Error != nil {
|
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)
|
// 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
|
// It also sets up a bunch of values that tend to be obvious for local accounts
|
||||||
func (s *Storage) NewLocalAccount(handle string) (*Account, error) {
|
func (s *Storage) NewLocalAccount(handle string) (*Account, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
log.Trace().Caller().Send()
|
log.Trace().Caller().Send()
|
||||||
log.Debug().Str("account-handle", handle).Msg("Creating new local account")
|
log.Debug().Str("account-handle", handle).Msg("Creating new local account")
|
||||||
acc, err := s.NewEmptyAccount()
|
acc, err := s.NewEmptyAccount()
|
||||||
|
@ -382,31 +391,36 @@ func (s *Storage) NewLocalAccount(handle string) (*Account, error) {
|
||||||
return acc, nil
|
return acc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Storage) DeleteAccount(accId string) error {
|
||||||
|
// TODO: Implement me
|
||||||
|
panic("Not implemented")
|
||||||
|
}
|
||||||
|
|
||||||
// ---- Section WebAuthn.User
|
// ---- Section WebAuthn.User
|
||||||
// Implements the webauthn.User interface for interaction with passkeys
|
// Implements the webauthn.User interface for interaction with passkeys
|
||||||
|
|
||||||
func (a *Account) WebAuthnID() []byte {
|
func (a *Account) WebAuthnID() []byte {
|
||||||
log.Trace().Caller().Send()
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
return a.WebAuthnId
|
return a.WebAuthnId
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Account) WebAuthnName() string {
|
func (u *Account) WebAuthnName() string {
|
||||||
log.Trace().Caller().Send()
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
return u.Username
|
return u.Username
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Account) WebAuthnDisplayName() string {
|
func (u *Account) WebAuthnDisplayName() string {
|
||||||
log.Trace().Caller().Send()
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
return u.DisplayName
|
return u.DisplayName
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Account) WebAuthnCredentials() []webauthn.Credential {
|
func (u *Account) WebAuthnCredentials() []webauthn.Credential {
|
||||||
log.Trace().Caller().Send()
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
return u.PasskeyCredentials
|
return u.PasskeyCredentials
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *Account) WebAuthnIcon() string {
|
func (u *Account) WebAuthnIcon() string {
|
||||||
log.Trace().Caller().Send()
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -414,7 +428,7 @@ func (u *Account) WebAuthnIcon() string {
|
||||||
// Implements the passkey.User interface
|
// Implements the passkey.User interface
|
||||||
|
|
||||||
func (u *Account) PutCredential(new webauthn.Credential) {
|
func (u *Account) PutCredential(new webauthn.Credential) {
|
||||||
log.Trace().Caller().Send()
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
u.PasskeyCredentials = append(u.PasskeyCredentials, new)
|
u.PasskeyCredentials = append(u.PasskeyCredentials, new)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -422,7 +436,7 @@ func (u *Account) PutCredential(new webauthn.Credential) {
|
||||||
// Implements the passkey.UserStore interface
|
// Implements the passkey.UserStore interface
|
||||||
|
|
||||||
func (s *Storage) GetOrCreateUser(userID string) passkey.User {
|
func (s *Storage) GetOrCreateUser(userID string) passkey.User {
|
||||||
log.Trace().Caller().Send()
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
log.Debug().
|
log.Debug().
|
||||||
Str("account-handle", userID).
|
Str("account-handle", userID).
|
||||||
Msg("Looking for or creating account for passkey stuff")
|
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 {
|
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")
|
log.Debug().Bytes("webauthn-id", id).Msg("Looking for account with webauthn id")
|
||||||
acc := Account{}
|
acc := Account{}
|
||||||
res := s.db.Where(Account{WebAuthnId: id}).First(&acc)
|
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) {
|
func (s *Storage) SaveUser(rawUser passkey.User) {
|
||||||
log.Trace().Caller().Send()
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
user, ok := rawUser.(*Account)
|
user, ok := rawUser.(*Account)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Error().Any("raw-user", rawUser).Msg("Failed to cast raw user to db account")
|
log.Error().Any("raw-user", rawUser).Msg("Failed to cast raw user to db account")
|
||||||
|
|
|
@ -3,6 +3,8 @@ package storage
|
||||||
import (
|
import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/rs/zerolog/log"
|
||||||
|
"gitlab.com/mstarongitlab/linstrom/util"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,6 +24,7 @@ type UserInfoField struct {
|
||||||
// TODO: Add functions to store, load, update and delete these
|
// TODO: Add functions to store, load, update and delete these
|
||||||
|
|
||||||
func (s *Storage) FindUserFieldById(id uint) (*UserInfoField, error) {
|
func (s *Storage) FindUserFieldById(id uint) (*UserInfoField, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
entry := UserInfoField{}
|
entry := UserInfoField{}
|
||||||
err := s.db.First(&entry, id).Error
|
err := s.db.First(&entry, id).Error
|
||||||
switch err {
|
switch err {
|
||||||
|
@ -35,6 +38,7 @@ func (s *Storage) FindUserFieldById(id uint) (*UserInfoField, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Storage) FindMultipleUserFieldsById(ids []uint) ([]UserInfoField, error) {
|
func (s *Storage) FindMultipleUserFieldsById(ids []uint) ([]UserInfoField, error) {
|
||||||
|
defer util.Untrace(util.Trace(&log.Logger))
|
||||||
entries := []UserInfoField{}
|
entries := []UserInfoField{}
|
||||||
err := s.db.Where(ids).Find(&entries).Error
|
err := s.db.Where(ids).Find(&entries).Error
|
||||||
switch err {
|
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