87 lines
2.0 KiB
Go
87 lines
2.0 KiB
Go
|
|
package notify
|
||
|
|
|
||
|
|
import (
|
||
|
|
"strconv"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
const (
|
||
|
|
defaultFileScope = "default"
|
||
|
|
clientFileDomain = "client"
|
||
|
|
serverFileDomain = "server"
|
||
|
|
serverTransportScopeSuffix = "#tg:"
|
||
|
|
)
|
||
|
|
|
||
|
|
func normalizeFileScope(scope string) string {
|
||
|
|
cleaned := strings.TrimSpace(scope)
|
||
|
|
if cleaned == "" {
|
||
|
|
return defaultFileScope
|
||
|
|
}
|
||
|
|
return cleaned
|
||
|
|
}
|
||
|
|
|
||
|
|
func clientFileScope() string {
|
||
|
|
return clientFileDomain
|
||
|
|
}
|
||
|
|
|
||
|
|
func serverFileScope(peer any) string {
|
||
|
|
logical := logicalConnFromPeer(peer)
|
||
|
|
if logical == nil {
|
||
|
|
return serverFileDomain + ":unknown"
|
||
|
|
}
|
||
|
|
id := strings.TrimSpace(logical.ID())
|
||
|
|
if id == "" {
|
||
|
|
return serverFileDomain + ":unknown"
|
||
|
|
}
|
||
|
|
return serverFileDomain + ":" + id
|
||
|
|
}
|
||
|
|
|
||
|
|
func serverTransportScope(peer any) string {
|
||
|
|
logical := logicalConnFromPeer(peer)
|
||
|
|
if logical == nil {
|
||
|
|
return serverFileDomain + ":unknown"
|
||
|
|
}
|
||
|
|
return serverTransportScopeByGeneration(logical, logical.transportGenerationSnapshot())
|
||
|
|
}
|
||
|
|
|
||
|
|
func serverTransportScopeForTransport(transport *TransportConn) string {
|
||
|
|
if transport == nil {
|
||
|
|
return serverFileDomain + ":unknown"
|
||
|
|
}
|
||
|
|
return transport.transportScope()
|
||
|
|
}
|
||
|
|
|
||
|
|
func serverTransportScopeByGeneration(peer any, generation uint64) string {
|
||
|
|
base := serverFileScope(peer)
|
||
|
|
if generation == 0 {
|
||
|
|
return base
|
||
|
|
}
|
||
|
|
return base + serverTransportScopeSuffix + strconv.FormatUint(generation, 10)
|
||
|
|
}
|
||
|
|
|
||
|
|
func serverTransportDeliveryScopes(peer any) []string {
|
||
|
|
logical := logicalConnFromPeer(peer)
|
||
|
|
if logical == nil {
|
||
|
|
return []string{serverFileDomain + ":unknown"}
|
||
|
|
}
|
||
|
|
base := serverFileScope(logical)
|
||
|
|
transport := serverTransportScope(logical)
|
||
|
|
if transport == base {
|
||
|
|
return []string{base}
|
||
|
|
}
|
||
|
|
return []string{transport, base}
|
||
|
|
}
|
||
|
|
|
||
|
|
func serverTransportDeliveryScopesForTransport(transport *TransportConn) []string {
|
||
|
|
if transport == nil {
|
||
|
|
return []string{serverFileDomain + ":unknown"}
|
||
|
|
}
|
||
|
|
return transport.deliveryScopes()
|
||
|
|
}
|
||
|
|
|
||
|
|
func scopeBelongsToServerFileScope(scope string, base string) bool {
|
||
|
|
scope = normalizeFileScope(scope)
|
||
|
|
base = normalizeFileScope(base)
|
||
|
|
return scope == base || strings.HasPrefix(scope, base+serverTransportScopeSuffix)
|
||
|
|
}
|