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 Servers []Server } // 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) { 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)) } }