Live video on the AT Protocol
at natb/workers-docs 70 lines 2.2 kB view raw
1package model 2 3import ( 4 "context" 5 "errors" 6 "fmt" 7 "time" 8 9 lexutil "github.com/bluesky-social/indigo/lex/util" 10 "gorm.io/gorm" 11 "stream.place/streamplace/pkg/streamplace" 12) 13 14// ServerSettings represents a user's settings for a particular Streamplace node 15type ServerSettings struct { 16 Server string `gorm:"primaryKey;column:server"` 17 RepoDID string `gorm:"primaryKey;column:repo_did"` 18 Record *[]byte `gorm:"column:record"` 19 Created time.Time `gorm:"column:created;not null"` 20 Updated time.Time `gorm:"column:updated;not null"` 21} 22 23// TableName specifies the table name for the ServerSettings model 24func (ServerSettings) TableName() string { 25 return "server_settings" 26} 27 28// ToStreamplaceServerSettings converts the model to a streamplace ServerSettings 29func (m *ServerSettings) ToStreamplaceServerSettings() (*streamplace.ServerSettings, error) { 30 if m.Record == nil { 31 return nil, fmt.Errorf("no record data") 32 } 33 rec, err := lexutil.CborDecodeValue(*m.Record) 34 if err != nil { 35 return nil, fmt.Errorf("error decoding server settings: %w", err) 36 } 37 ss, ok := rec.(*streamplace.ServerSettings) 38 if !ok { 39 return nil, fmt.Errorf("invalid server settings") 40 } 41 return ss, nil 42} 43 44// UpdateServerSettings creates or updates a server settings record 45func (m *DBModel) UpdateServerSettings(ctx context.Context, settings *ServerSettings) error { 46 now := time.Now() 47 if settings.Created.IsZero() { 48 settings.Created = now 49 } 50 settings.Updated = now 51 return m.DB.Save(settings).Error 52} 53 54// GetServerSettings retrieves server settings for a given server and repoDID 55func (m *DBModel) GetServerSettings(ctx context.Context, server string, repoDID string) (*ServerSettings, error) { 56 var settings ServerSettings 57 err := m.DB.Where("server = ? AND repo_did = ?", server, repoDID).First(&settings).Error 58 if errors.Is(err, gorm.ErrRecordNotFound) { 59 return nil, nil 60 } 61 if err != nil { 62 return nil, err 63 } 64 return &settings, nil 65} 66 67// DeleteServerSettings deletes server settings for a given server and repoDID 68func (m *DBModel) DeleteServerSettings(ctx context.Context, server string, repoDID string) error { 69 return m.DB.Where("server = ? AND repo_did = ?", server, repoDID).Delete(&ServerSettings{}).Error 70}