From 81f9d0b094b85cd82fda66c42ae8d88b72fb7fa4 Mon Sep 17 00:00:00 2001 From: Sarah Jamie Lewis Date: Fri, 8 Oct 2021 10:59:32 -0700 Subject: [PATCH] Scope/Zone API Fixes per Dan's comments --- model/attr/scope.go | 41 +++++++++++++++++++++++++++++++++-------- model/attr/zone.go | 4 ++-- peer/cwtch_peer.go | 20 ++++++++------------ 3 files changed, 43 insertions(+), 22 deletions(-) diff --git a/model/attr/scope.go b/model/attr/scope.go index 90e4d3e..00c3ee0 100644 --- a/model/attr/scope.go +++ b/model/attr/scope.go @@ -15,6 +15,9 @@ values stored in the LocalScope. // Scope strongly types Scope strings type Scope string +// ScopedZonedPath typed path with a scope and a zone +type ScopedZonedPath string + // scopes for attributes const ( // on a peer, local and peer supplied data @@ -24,6 +27,8 @@ const ( // on a local profile, public data and private settings PublicScope = Scope("public") + + UnknownScope = Scope("unknown") ) // Separator for scope and the rest of path @@ -31,12 +36,37 @@ const Separator = "." // IntoScope converts a string to a 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 -func (scope Scope) ConstructScopedZonedPath(zonedPath ZonedPath) string { - return string(scope) + Separator + string(zonedPath) +func (scope Scope) ConstructScopedZonedPath(zonedPath ZonedPath) ScopedZonedPath { + 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 @@ -49,11 +79,6 @@ func (scope Scope) IsConversation() bool { 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 func GetLocalScope(path string) string { return string(LocalScope) + Separator + path diff --git a/model/attr/zone.go b/model/attr/zone.go index 2146497..77d7753 100644 --- a/model/attr/zone.go +++ b/model/attr/zone.go @@ -24,10 +24,10 @@ const ( 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 // 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) } diff --git a/peer/cwtch_peer.go b/peer/cwtch_peer.go index de47ce8..b69bcb7 100644 --- a/peer/cwtch_peer.go +++ b/peer/cwtch_peer.go @@ -55,18 +55,18 @@ type cwtchPeer struct { } 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) } func (cp *cwtchPeer) GetScopedZonedAttribute(scope attr.Scope, zone attr.Zone, key string) (string, bool) { cp.mutex.Lock() 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) - if val, exists := cp.Profile.GetAttribute(scopedZonedKey); exists { + if val, exists := cp.Profile.GetAttribute(scopedZonedKey.ToString()); exists { 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) { cp.mutex.Lock() - scopedZonedKey := scope.ConstructScopedZonedPath(zone.EnforceZone(key)) + scopedZonedKey := scope.ConstructScopedZonedPath(zone.ConstructZonedPath(key)) log.Debugf("storing attribute: %v = %v", scopedZonedKey, value) - cp.Profile.SetAttribute(scopedZonedKey, value) + cp.Profile.SetAttribute(scopedZonedKey.ToString(), value) defer cp.mutex.Unlock() cp.eventBus.Publish(event.NewEvent(event.SetAttribute, map[event.Field]string{ - event.Key: scopedZonedKey, + event.Key: scopedZonedKey.ToString(), event.Data: value, })) } @@ -206,10 +206,6 @@ type SendMessages interface { // Deprecated 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 // Deprecated use overlays instead InviteOnionToGroup(string, string) error @@ -776,7 +772,7 @@ func (cp *cwtchPeer) 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 { cp.AddContact(onion, onion, model.AuthUnknown) } @@ -829,7 +825,7 @@ func (cp *cwtchPeer) eventHandler() { } case event.NewMessageFromPeer: //event.TimestampReceived, event.RemotePeer, event.Data 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: cp.mutex.Lock()