Serverside stuff. Mostly shuffling things around

This commit is contained in:
Melody Becker 2024-10-26 11:42:51 +02:00
parent 90667d96c7
commit be70109c43
20 changed files with 513 additions and 71 deletions

View file

@ -6,12 +6,21 @@
package storage
import (
"crypto/ed25519"
"fmt"
"github.com/rs/zerolog/log"
"gitlab.com/mstarongitlab/linstrom/config"
"gitlab.com/mstarongitlab/linstrom/storage/cache"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
// Always keep a reference of the server's own RemoteServer entry here
// Removes the need to perform a db request every time a new local anything
// is created
var serverSelf RemoteServer
// Storage is responsible for all database, cache and media related actions
// and serves as the lowest layer of the cake
type Storage struct {
@ -36,10 +45,78 @@ func NewStorage(dbUrl string, cache *cache.Cache) (*Storage, error) {
InboundJob{},
OutboundJob{},
AccessToken{},
Emote{},
)
if err != nil {
return nil, fmt.Errorf("failed to apply migrations: %w", err)
}
s := &Storage{db, cache}
if err = s.insertSelfFromConfig(); err != nil {
return nil, err
}
return &Storage{db, cache}, nil
return s, nil
}
func (s *Storage) insertSelfFromConfig() error {
const ServerActorId = "self"
var err error
// Insert server info
serverData := RemoteServer{}
err = s.db.Where("id = 1").
Attrs(RemoteServer{
Domain: config.GlobalConfig.General.GetFullDomain(),
}).
Assign(RemoteServer{
IsSelf: true,
Name: config.GlobalConfig.Self.ServerDisplayName,
// Icon: "", // TODO: Set to server icon media
}).FirstOrCreate(&serverData).Error
if err != nil {
return err
}
// Set module specific global var
serverSelf = serverData
// Insert server actor
serverActor := Account{}
serverActorPublicKey, serverActorPrivateKey, err := ed25519.GenerateKey(nil)
if err != nil {
return err
}
err = s.db.Where(Account{ID: ServerActorId}).
// Values to always (re)set after launch
Assign(Account{
DisplayName: config.GlobalConfig.Self.ServerActorDisplayName,
// Server: serverData,
ServerId: serverData.ID,
// CustomFields: []uint{},
Description: "Server actor of a Linstrom server",
// Tags: []string{},
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{},
Gender: []string{},
Roles: []string{}, // TODO: Add server actor role once created
}).
// Values that'll only be set on first creation
Attrs(Account{
PublicKey: serverActorPublicKey,
PrivateKey: serverActorPrivateKey,
}).
FirstOrCreate(&serverActor).Error
if err != nil {
return err
}
return nil
}