2024-05-31 09:54:39 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
2024-11-06 15:57:44 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-12-18 14:24:56 +00:00
|
|
|
"git.mstar.dev/mstar/linstrom/util"
|
2024-05-31 09:54:39 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RemoteServer struct {
|
2024-10-26 09:42:51 +00:00
|
|
|
gorm.Model
|
2024-05-31 09:54:39 +00:00
|
|
|
ServerType RemoteServerType // What software the server is running. Useful for formatting
|
2024-10-26 09:42:51 +00:00
|
|
|
Domain string // `gorm:"primaryKey"` // Domain the server exists under. Additional primary key
|
2024-05-31 09:54:39 +00:00
|
|
|
Name string // What the server wants to be known as (usually same as url)
|
|
|
|
Icon string // ID of a media file
|
|
|
|
IsSelf bool // Whether this server is yours truly
|
|
|
|
}
|
2024-09-12 06:56:57 +00:00
|
|
|
|
2024-11-04 06:48:46 +00:00
|
|
|
func (s *Storage) FindRemoteServerByDomain(url string) (*RemoteServer, error) {
|
2024-11-06 15:57:44 +00:00
|
|
|
defer util.Untrace(util.Trace(&log.Logger))
|
2024-10-26 09:42:51 +00:00
|
|
|
server := RemoteServer{}
|
|
|
|
err := s.db.Where("domain = ?").First(&server).Error
|
2024-09-17 13:45:43 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return &server, nil
|
|
|
|
case gorm.ErrRecordNotFound:
|
|
|
|
return nil, ErrEntryNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-12 06:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find a remote server with a given display name
|
|
|
|
func (s *Storage) FindRemoteServerByDisplayName(displayName string) (*RemoteServer, error) {
|
2024-11-06 15:57:44 +00:00
|
|
|
defer util.Untrace(util.Trace(&log.Logger))
|
2024-09-17 13:45:43 +00:00
|
|
|
server := RemoteServer{}
|
|
|
|
err := s.db.Where("name = ?", displayName).First(&server).Error
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return &server, nil
|
|
|
|
case gorm.ErrRecordNotFound:
|
|
|
|
return nil, ErrEntryNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-09-12 06:56:57 +00:00
|
|
|
}
|
|
|
|
|
2024-11-04 06:48:46 +00:00
|
|
|
func (s *Storage) FindRemoteServerById(id uint) (*RemoteServer, error) {
|
2024-11-06 15:57:44 +00:00
|
|
|
defer util.Untrace(util.Trace(&log.Logger))
|
2024-11-04 06:48:46 +00:00
|
|
|
server := RemoteServer{}
|
|
|
|
err := s.db.First(&server, id).Error
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return &server, nil
|
|
|
|
case gorm.ErrRecordNotFound:
|
|
|
|
return nil, ErrEntryNotFound
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-12 06:56:57 +00:00
|
|
|
// Create a new remote server
|
|
|
|
func (s *Storage) NewRemoteServer(
|
|
|
|
url, displayName, icon string,
|
|
|
|
serverType RemoteServerType,
|
|
|
|
) (*RemoteServer, error) {
|
2024-11-06 15:57:44 +00:00
|
|
|
defer util.Untrace(util.Trace(&log.Logger))
|
2024-11-04 06:48:46 +00:00
|
|
|
_, err := s.FindRemoteServerByDomain(url)
|
2024-09-17 13:45:43 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return nil, ErrEntryAlreadyExists
|
|
|
|
case ErrEntryNotFound: // Empty case, not found is what we want
|
|
|
|
default:
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
server := RemoteServer{
|
2024-10-26 09:42:51 +00:00
|
|
|
Domain: url,
|
2024-09-17 13:45:43 +00:00
|
|
|
Name: displayName,
|
|
|
|
Icon: icon,
|
|
|
|
ServerType: serverType,
|
|
|
|
}
|
|
|
|
err = s.db.Create(&server).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &server, nil
|
2024-09-12 06:56:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update a remote server with the given url
|
|
|
|
// If displayName is set, update that
|
|
|
|
// If icon is set, update that
|
|
|
|
// Returns the updated version
|
|
|
|
func (s *Storage) UpdateRemoteServer(url string, displayName, icon *string) (*RemoteServer, error) {
|
2024-11-06 15:57:44 +00:00
|
|
|
defer util.Untrace(util.Trace(&log.Logger))
|
2024-09-17 13:45:43 +00:00
|
|
|
if displayName == nil && icon == nil {
|
|
|
|
return nil, ErrNothingToChange
|
|
|
|
}
|
2024-11-04 15:25:39 +00:00
|
|
|
server, err := s.FindRemoteServerByDomain(url)
|
2024-09-17 13:45:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if displayName != nil {
|
|
|
|
server.Name = *displayName
|
|
|
|
}
|
|
|
|
if icon != nil {
|
|
|
|
server.Icon = *icon
|
|
|
|
}
|
|
|
|
err = s.db.Save(server).Error
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return server, nil
|
2024-09-12 06:56:57 +00:00
|
|
|
}
|