meowlib/client/serverlist.go

53 lines
1.5 KiB
Go
Raw Normal View History

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 {
2023-11-27 00:31:43 +01:00
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) {
2023-11-09 22:50:51 +01:00
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.ServerData.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))
}
}