Merge branch 'main' of gitlab.com:mstarongitlab/linstrom

also delete server-old with all its problems
This commit is contained in:
Melody Becker 2024-09-13 20:41:47 +02:00
commit d99efca667
Signed by: mstar
SSH key fingerprint: SHA256:vkXfS9FG2pVNVfvDrzd1VW9n8VJzqqdKQGljxxX8uK8
47 changed files with 2200 additions and 694 deletions

View file

@ -1,3 +1,8 @@
// TODO: Unify function names
// Storage is the handler for cache and db access
// It handles storing various data in the database as well as caching that data
// Said data includes notes, accounts, metadata about media files, servers and similar
package storage
import (
@ -5,6 +10,7 @@ import (
"fmt"
"github.com/glebarez/sqlite"
"gitlab.com/mstarongitlab/linstrom/storage/cache"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
@ -12,69 +18,52 @@ import (
// Storage is responsible for all database, cache and media related actions
// and serves as the lowest layer of the cake
type Storage struct {
db *gorm.DB
db *gorm.DB
cache *cache.Cache
}
var ErrAccountNotFound = errors.New("account not found")
var ErrInvalidData = errors.New("invalid data")
// Build a new storage using sqlite as database backend
func NewStorageSqlite(filePath string) (*Storage, error) {
func NewStorageSqlite(filePath string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(sqlite.Open(filePath))
if err != nil {
return nil, err
}
return storageFromEmptyDb(db)
return storageFromEmptyDb(db, cache)
}
func NewStoragePostgres(dbUrl string) (*Storage, error) {
func NewStoragePostgres(dbUrl string, cache *cache.Cache) (*Storage, error) {
db, err := gorm.Open(postgres.Open(dbUrl))
if err != nil {
return nil, err
}
return storageFromEmptyDb(db)
return storageFromEmptyDb(db, cache)
}
func storageFromEmptyDb(db *gorm.DB) (*Storage, error) {
func storageFromEmptyDb(db *gorm.DB, cache *cache.Cache) (*Storage, error) {
// AutoMigrate ensures the db is in a state where all the structs given here
// have their own tables and relations setup. It also updates tables if necessary
if err := db.AutoMigrate(
placeholderMediaFile,
placeholderUser,
placeholderNote,
placeholderServer,
); err != nil {
return nil, fmt.Errorf("problem while auto migrating: %w", err)
}
// Afterwards add the placeholder entries for each table.
// FirstOrCreate either creates a new entry or retrieves the first matching one
// We only care about the creation if there is none yet, so no need to carry the result over
if res := db.FirstOrCreate(placeholderMediaFile); res.Error != nil {
return nil, fmt.Errorf("failed to add placeholder media file: %w", res.Error)
}
if res := db.FirstOrCreate(placeholderUser()); res.Error != nil {
return nil, fmt.Errorf("failed to add placeholder media file: %w", res.Error)
}
if res := db.FirstOrCreate(placeholderNote); res.Error != nil {
return nil, fmt.Errorf("failed to add placeholder media file: %w", res.Error)
}
if res := db.FirstOrCreate(placeholderServer); res.Error != nil {
return nil, fmt.Errorf("failed to add placeholder media file: %w", res.Error)
err := db.AutoMigrate(
MediaMetadata{},
Account{},
RemoteServer{},
Note{},
Role{},
PasskeySession{},
)
if err != nil {
return nil, err
}
// And finally, build the actual storage struct
return &Storage{
db: db,
db: db,
cache: cache,
}, nil
}
func (s *Storage) FindLocalAccount(handle string) (*User, error) {
acc := User{}
res := s.db.Where("handle = ?", handle).First(&acc)
if res.Error != nil {
return nil, fmt.Errorf("failed to query db: %w", res.Error)
}
if res.RowsAffected == 0 {
return nil, ErrAccountNotFound
}
return nil, errors.New("unimplemented")
// TODO: Placeholder. Update to proper implementation later. Including signature
func (s *Storage) FindLocalAccount(handle string) (string, error) {
return handle, nil
}