Scope/Zone API Fixes per Dan's comments
continuous-integration/drone/push Build is passing Details
continuous-integration/drone/pr Build is passing Details

This commit is contained in:
Sarah Jamie Lewis 2021-10-08 10:59:32 -07:00
parent aec3c40180
commit 81f9d0b094
3 changed files with 43 additions and 22 deletions

View File

@ -15,6 +15,9 @@ values stored in the LocalScope.
// Scope strongly types Scope strings // Scope strongly types Scope strings
type Scope string type Scope string
// ScopedZonedPath typed path with a scope and a zone
type ScopedZonedPath string
// scopes for attributes // scopes for attributes
const ( const (
// on a peer, local and peer supplied data // on a peer, local and peer supplied data
@ -24,6 +27,8 @@ const (
// on a local profile, public data and private settings // on a local profile, public data and private settings
PublicScope = Scope("public") PublicScope = Scope("public")
UnknownScope = Scope("unknown")
) )
// Separator for scope and the rest of path // Separator for scope and the rest of path
@ -31,12 +36,37 @@ const Separator = "."
// IntoScope converts a string to a Scope // IntoScope converts a string to a Scope
func IntoScope(scope string) Scope { func IntoScope(scope string) Scope {
return Scope(scope) switch scope {
case "local":
return LocalScope
case "peer":
return PeerScope
case "conversation":
return ConversationScope
case "public":
return PublicScope
}
return UnknownScope
} }
// ConstructScopedZonedPath enforces a scope over a zoned path // ConstructScopedZonedPath enforces a scope over a zoned path
func (scope Scope) ConstructScopedZonedPath(zonedPath ZonedPath) string { func (scope Scope) ConstructScopedZonedPath(zonedPath ZonedPath) ScopedZonedPath {
return string(scope) + Separator + string(zonedPath) return ScopedZonedPath(string(scope) + Separator + string(zonedPath))
}
// ToString converts a ScopedZonedPath to a string
func (szp ScopedZonedPath) ToString() string {
return string(szp)
}
// IsLocal returns true if the scope is a local scope
func (scope Scope) IsLocal() bool {
return scope == LocalScope
}
// IsPeer returns true if the scope is a peer scope
func (scope Scope) IsPeer() bool {
return scope == PeerScope
} }
// IsPublic returns true if the scope is a public scope // IsPublic returns true if the scope is a public scope
@ -49,11 +79,6 @@ func (scope Scope) IsConversation() bool {
return scope == ConversationScope return scope == ConversationScope
} }
// GetPublicScope takes a path and attaches the pubic scope to it
func GetPublicScope(path string) string {
return string(PublicScope) + Separator + path
}
// GetLocalScope takes a path and attaches the local scope to it // GetLocalScope takes a path and attaches the local scope to it
func GetLocalScope(path string) string { func GetLocalScope(path string) string {
return string(LocalScope) + Separator + path return string(LocalScope) + Separator + path

View File

@ -24,10 +24,10 @@ const (
UnknownZone = Zone("unknown") UnknownZone = Zone("unknown")
) )
// EnforceZone takes a path and attaches a zone to it. // ConstructZonedPath takes a path and attaches a zone to it.
// Note that this returns a ZonedPath which isn't directly usable, it must be given to ConstructScopedZonedPath // Note that this returns a ZonedPath which isn't directly usable, it must be given to ConstructScopedZonedPath
// in order to be realized into an actual attribute path. // in order to be realized into an actual attribute path.
func (zone Zone) EnforceZone(path string) ZonedPath { func (zone Zone) ConstructZonedPath(path string) ZonedPath {
return ZonedPath(string(zone) + Separator + path) return ZonedPath(string(zone) + Separator + path)
} }

View File

@ -55,18 +55,18 @@ type cwtchPeer struct {
} }
func (cp *cwtchPeer) SendScopedZonedGetValToContact(handle string, scope attr.Scope, zone attr.Zone, path string) { func (cp *cwtchPeer) SendScopedZonedGetValToContact(handle string, scope attr.Scope, zone attr.Zone, path string) {
event := event.NewEventList(event.SendGetValMessageToPeer, event.RemotePeer, handle, event.Scope, string(scope), event.Path, string(zone.EnforceZone(path))) event := event.NewEventList(event.SendGetValMessageToPeer, event.RemotePeer, handle, event.Scope, string(scope), event.Path, string(zone.ConstructZonedPath(path)))
cp.eventBus.Publish(event) cp.eventBus.Publish(event)
} }
func (cp *cwtchPeer) GetScopedZonedAttribute(scope attr.Scope, zone attr.Zone, key string) (string, bool) { func (cp *cwtchPeer) GetScopedZonedAttribute(scope attr.Scope, zone attr.Zone, key string) (string, bool) {
cp.mutex.Lock() cp.mutex.Lock()
defer cp.mutex.Unlock() defer cp.mutex.Unlock()
scopedZonedKey := scope.ConstructScopedZonedPath(zone.EnforceZone(key)) scopedZonedKey := scope.ConstructScopedZonedPath(zone.ConstructZonedPath(key))
log.Debugf("looking up attribute %v %v %v (%v)", scope, zone, key, scopedZonedKey) log.Debugf("looking up attribute %v %v %v (%v)", scope, zone, key, scopedZonedKey)
if val, exists := cp.Profile.GetAttribute(scopedZonedKey); exists { if val, exists := cp.Profile.GetAttribute(scopedZonedKey.ToString()); exists {
return val, true return val, true
} }
@ -79,12 +79,12 @@ func (cp *cwtchPeer) GetScopedZonedAttribute(scope attr.Scope, zone attr.Zone, k
func (cp *cwtchPeer) SetScopedZonedAttribute(scope attr.Scope, zone attr.Zone, key string, value string) { func (cp *cwtchPeer) SetScopedZonedAttribute(scope attr.Scope, zone attr.Zone, key string, value string) {
cp.mutex.Lock() cp.mutex.Lock()
scopedZonedKey := scope.ConstructScopedZonedPath(zone.EnforceZone(key)) scopedZonedKey := scope.ConstructScopedZonedPath(zone.ConstructZonedPath(key))
log.Debugf("storing attribute: %v = %v", scopedZonedKey, value) log.Debugf("storing attribute: %v = %v", scopedZonedKey, value)
cp.Profile.SetAttribute(scopedZonedKey, value) cp.Profile.SetAttribute(scopedZonedKey.ToString(), value)
defer cp.mutex.Unlock() defer cp.mutex.Unlock()
cp.eventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{ cp.eventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{
event.Key: scopedZonedKey, event.Key: scopedZonedKey.ToString(),
event.Data: value, event.Data: value,
})) }))
} }
@ -206,10 +206,6 @@ type SendMessages interface {
// Deprecated // Deprecated
SendMessageToPeer(string, string) string SendMessageToPeer(string, string) string
// TODO This should probably not be exposed
// Deprecated do not use this to store messages
StoreMessage(onion string, messageTxt string, sent time.Time)
// TODO // TODO
// Deprecated use overlays instead // Deprecated use overlays instead
InviteOnionToGroup(string, string) error InviteOnionToGroup(string, string) error
@ -776,7 +772,7 @@ func (cp *cwtchPeer) Shutdown() {
cp.queue.Shutdown() cp.queue.Shutdown()
} }
func (cp *cwtchPeer) StoreMessage(onion string, messageTxt string, sent time.Time) { func (cp *cwtchPeer) storeMessage(onion string, messageTxt string, sent time.Time) {
if cp.GetContact(onion) == nil { if cp.GetContact(onion) == nil {
cp.AddContact(onion, onion, model.AuthUnknown) cp.AddContact(onion, onion, model.AuthUnknown)
} }
@ -829,7 +825,7 @@ func (cp *cwtchPeer) eventHandler() {
} }
case event.NewMessageFromPeer: //event.TimestampReceived, event.RemotePeer, event.Data case event.NewMessageFromPeer: //event.TimestampReceived, event.RemotePeer, event.Data
ts, _ := time.Parse(time.RFC3339Nano, ev.Data[event.TimestampReceived]) ts, _ := time.Parse(time.RFC3339Nano, ev.Data[event.TimestampReceived])
cp.StoreMessage(ev.Data[event.RemotePeer], ev.Data[event.Data], ts) cp.storeMessage(ev.Data[event.RemotePeer], ev.Data[event.Data], ts)
case event.PeerAcknowledgement: case event.PeerAcknowledgement:
cp.mutex.Lock() cp.mutex.Lock()