meowlib/client/serverlist.go
ycc 93e972900f
All checks were successful
continuous-integration/drone/push Build is passing
Refactor structs with some getters/setters - Peers part
2024-02-07 16:08:24 +01:00

53 lines
1.5 KiB
Go

package client
import "errors"
// ServerList manages lists that are used in Identity
// - Message servers list
// - ArchiveServers lists
// - Owned servers lists
// - Matriochka paths
type ServerList struct {
Name string `json:"name,omitempty"`
Servers []Server `json:"servers,omitempty"`
}
// FilterByIdxs returns a filtered server list filtered according to an index list
func (sl *ServerList) FilterByIdxs(MessageServerIdxs []int) (filtered *ServerList, err error) {
filtered.Servers = []Server{}
for _, i := range MessageServerIdxs {
if i > len(sl.Servers)-1 {
return nil, errors.New("requested server out of range of defined message servers")
}
}
for _, i := range MessageServerIdxs {
filtered.Servers = append(filtered.Servers, sl.Servers[i])
}
return filtered, nil
}
// GetServerByIdx returns a server from it's index
func (sl *ServerList) GetServerByIdx(idx int) (server *Server, err error) {
if idx > len(sl.Servers)-1 {
return nil, errors.New("requested server out of range of defined message servers")
}
return &sl.Servers[idx], nil
}
// GetServerByPubkey returns a server from it's public key
func (sl *ServerList) GetServerByPubkey(pubkey string) (filtered *Server) {
for _, srv := range sl.Servers {
if srv.PublicKey == pubkey {
return &srv
}
}
return nil
}
// AddUrls is a simple utility functon used mainly as a shortcut for testing purposes
func (sl *ServerList) AddUrls(urls []string) {
for _, url := range urls {
sl.Servers = append(sl.Servers, *CreateServerFromUrl(url))
}
}