2021-06-24 22:30:46 +00:00
|
|
|
package groups
|
|
|
|
|
|
|
|
import (
|
|
|
|
"cwtch.im/cwtch/event"
|
|
|
|
"cwtch.im/cwtch/model"
|
2021-11-17 20:33:51 +00:00
|
|
|
"cwtch.im/cwtch/model/attr"
|
2021-06-24 22:30:46 +00:00
|
|
|
"cwtch.im/cwtch/peer"
|
2021-11-17 20:33:51 +00:00
|
|
|
"cwtch.im/cwtch/protocol/connections"
|
2021-06-24 22:30:46 +00:00
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const serverPrefix = "server:"
|
|
|
|
const tofuBundlePrefix = "tofubundle:"
|
|
|
|
const groupPrefix = "torv3"
|
|
|
|
const groupExperiment = "tapir-groups-experiment"
|
|
|
|
|
|
|
|
const importBundlePrefix = "importBundle"
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ServerList is a json encoded list of servers
|
|
|
|
ServerList = event.Field("ServerList")
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// UpdateServerInfo is an event containing a ProfileOnion and a ServerList
|
|
|
|
UpdateServerInfo = event.Type("UpdateServerInfo")
|
|
|
|
)
|
|
|
|
|
|
|
|
// GroupFunctionality provides experiment gated server functionality
|
|
|
|
type GroupFunctionality struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExperimentGate returns GroupFunctionality if the experiment is enabled, and an error otherwise.
|
|
|
|
func ExperimentGate(experimentMap map[string]bool) (*GroupFunctionality, error) {
|
|
|
|
if experimentMap[groupExperiment] {
|
|
|
|
return new(GroupFunctionality), nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("gated by %v", groupExperiment)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetServerInfoList compiles all the information the UI might need regarding all servers..
|
2021-11-17 20:33:51 +00:00
|
|
|
func (gf *GroupFunctionality) GetServerInfoList(profile peer.CwtchPeer) []Server {
|
2021-06-24 22:30:46 +00:00
|
|
|
var servers []Server
|
|
|
|
for _, server := range profile.GetServers() {
|
|
|
|
servers = append(servers, gf.GetServerInfo(server, profile))
|
|
|
|
}
|
|
|
|
return servers
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetServerInfo compiles all the information the UI might need regarding a particular server including any verified
|
|
|
|
// cryptographic keys
|
2021-11-17 20:33:51 +00:00
|
|
|
func (gf *GroupFunctionality) GetServerInfo(serverOnion string, profile peer.CwtchPeer) Server {
|
|
|
|
serverInfo,_ := profile.FetchConversationInfo(serverOnion)
|
2021-06-24 22:30:46 +00:00
|
|
|
keyTypes := []model.KeyType{model.KeyTypeServerOnion, model.KeyTypeTokenOnion, model.KeyTypePrivacyPass}
|
|
|
|
var serverKeys []ServerKey
|
|
|
|
|
|
|
|
for _, keyType := range keyTypes {
|
2021-11-17 20:33:51 +00:00
|
|
|
if key, has := serverInfo.GetAttribute(attr.PublicScope, attr.ServerKeyZone, string(keyType)); has {
|
2021-06-24 22:30:46 +00:00
|
|
|
serverKeys = append(serverKeys, ServerKey{Type: string(keyType), Key: key})
|
|
|
|
}
|
|
|
|
}
|
2021-11-17 20:33:51 +00:00
|
|
|
return Server{Onion: serverOnion, Status: connections.ConnectionStateName[profile.GetPeerState(serverInfo.Handle)], Keys: serverKeys}
|
|
|
|
}
|