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"
|
2022-03-04 00:34:39 +00:00
|
|
|
constants2 "cwtch.im/cwtch/model/constants"
|
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"
|
2021-11-20 18:10:14 +00:00
|
|
|
"git.openprivacy.ca/cwtch.im/libcwtch-go/constants"
|
2021-06-24 22:30:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const groupExperiment = "tapir-groups-experiment"
|
|
|
|
|
|
|
|
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 {
|
2021-11-18 23:44:21 +00:00
|
|
|
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-20 18:10:14 +00:00
|
|
|
|
2021-12-11 00:28:13 +00:00
|
|
|
description, _ := serverInfo.GetAttribute(attr.LocalScope, attr.ServerZone, constants.Description)
|
2022-03-04 00:34:39 +00:00
|
|
|
startTimeStr := serverInfo.Attributes[attr.LocalScope.ConstructScopedZonedPath(attr.LegacyGroupZone.ConstructZonedPath(constants2.SyncPreLastMessageTime)).ToString()]
|
|
|
|
recentTimeStr := serverInfo.Attributes[attr.LocalScope.ConstructScopedZonedPath(attr.LegacyGroupZone.ConstructZonedPath(constants2.SyncMostRecentMessageTime)).ToString()]
|
|
|
|
syncStatus := SyncStatus{startTimeStr, recentTimeStr}
|
|
|
|
|
|
|
|
return Server{Onion: serverOnion, Identifier: serverInfo.ID, Status: connections.ConnectionStateName[profile.GetPeerState(serverInfo.Handle)], Keys: serverKeys, Description: description, SyncProgress: syncStatus}
|
2021-12-07 21:48:56 +00:00
|
|
|
}
|