From 3302cc28b671892e3d94f6ce7b299ffdfa366ed2 Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Tue, 13 Mar 2018 13:14:19 -0500 Subject: [PATCH 1/3] Don't add duplicate URNs to contacts --- flows/actions/add_contact_urn.go | 6 ++++-- flows/contact.go | 20 +++++++++++++++++--- flows/events/contact_urn_added.go | 3 +-- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/flows/actions/add_contact_urn.go b/flows/actions/add_contact_urn.go index 78253eea6..e33128a0d 100644 --- a/flows/actions/add_contact_urn.go +++ b/flows/actions/add_contact_urn.go @@ -63,8 +63,10 @@ func (a *AddContactURNAction) Execute(run flows.FlowRun, step flows.Step, log fl log.Add(events.NewErrorEvent(fmt.Errorf("unable to add URN '%s:%s': %s", a.Scheme, evaluatedPath, err.Error()))) return nil } - urn = urn.Normalize("") - log.Add(events.NewURNAddedEvent(urn)) + if !run.Contact().HasURN(urn) { + log.Add(events.NewURNAddedEvent(urn)) + } + return nil } diff --git a/flows/contact.go b/flows/contact.go index 2fa72a350..43d3fc1e9 100644 --- a/flows/contact.go +++ b/flows/contact.go @@ -76,10 +76,24 @@ func (c *Contact) Name() string { return c.name } func (c *Contact) URNs() URNList { return c.urns } // AddURN adds a new URN to this contact -func (c *Contact) AddURN(urn urns.URN) { - // TODO normalize and check we're not adding duplicates +func (c *Contact) AddURN(urn urns.URN) error { + if c.HasURN(urn) { + return fmt.Errorf("contact already has the URN %s", urn) + } + c.urns = append(c.urns, &ContactURN{URN: urn.Normalize("")}) + return nil +} - c.urns = append(c.urns, &ContactURN{URN: urn}) +// HasURN checks whether the contact has the given URN +func (c *Contact) HasURN(urn urns.URN) bool { + urn = urn.Normalize("") + + for u := range c.urns { + if c.urns[u].URN == urn { + return true + } + } + return false } // Groups returns the groups that this contact belongs to diff --git a/flows/events/contact_urn_added.go b/flows/events/contact_urn_added.go index b89eddd58..1fc8b16d2 100644 --- a/flows/events/contact_urn_added.go +++ b/flows/events/contact_urn_added.go @@ -47,6 +47,5 @@ func (e *ContactURNAddedEvent) Apply(run flows.FlowRun) error { return fmt.Errorf("can't apply event in session without a contact") } - run.Contact().AddURN(e.URN) - return nil + return run.Contact().AddURN(e.URN) } From 6aa54c263154b97f3529a2bc3c1d9e461049b79a Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Tue, 13 Mar 2018 13:17:55 -0500 Subject: [PATCH 2/3] Adding a duplicate URN should be consistent with adding a duplicate group --- flows/contact.go | 6 +++--- flows/events/contact_urn_added.go | 3 ++- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/flows/contact.go b/flows/contact.go index 43d3fc1e9..c6594011d 100644 --- a/flows/contact.go +++ b/flows/contact.go @@ -76,12 +76,12 @@ func (c *Contact) Name() string { return c.name } func (c *Contact) URNs() URNList { return c.urns } // AddURN adds a new URN to this contact -func (c *Contact) AddURN(urn urns.URN) error { +func (c *Contact) AddURN(urn urns.URN) bool { if c.HasURN(urn) { - return fmt.Errorf("contact already has the URN %s", urn) + return false } c.urns = append(c.urns, &ContactURN{URN: urn.Normalize("")}) - return nil + return true } // HasURN checks whether the contact has the given URN diff --git a/flows/events/contact_urn_added.go b/flows/events/contact_urn_added.go index 1fc8b16d2..b89eddd58 100644 --- a/flows/events/contact_urn_added.go +++ b/flows/events/contact_urn_added.go @@ -47,5 +47,6 @@ func (e *ContactURNAddedEvent) Apply(run flows.FlowRun) error { return fmt.Errorf("can't apply event in session without a contact") } - return run.Contact().AddURN(e.URN) + run.Contact().AddURN(e.URN) + return nil } From 323437e28fd802eebdb8ec2dda000decc692440b Mon Sep 17 00:00:00 2001 From: Rowan Seymour Date: Tue, 13 Mar 2018 13:47:08 -0500 Subject: [PATCH 3/3] Fix for style --- flows/contact.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flows/contact.go b/flows/contact.go index c6594011d..1371bb7e2 100644 --- a/flows/contact.go +++ b/flows/contact.go @@ -88,8 +88,8 @@ func (c *Contact) AddURN(urn urns.URN) bool { func (c *Contact) HasURN(urn urns.URN) bool { urn = urn.Normalize("") - for u := range c.urns { - if c.urns[u].URN == urn { + for _, u := range c.urns { + if u.URN == urn { return true } }