Skip to content

Commit

Permalink
Merge pull request #342 from nyaruka/contact_status_pt2
Browse files Browse the repository at this point in the history
🗽 Read from contact.status instead of is_stopped/is_blocked
  • Loading branch information
rowanseymour authored Aug 25, 2020
2 parents 0a6917e + 1c86ee8 commit 809f3b9
Show file tree
Hide file tree
Showing 9 changed files with 163 additions and 40 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
github.com/mattn/go-sqlite3 v1.10.0 // indirect
github.com/nyaruka/ezconf v0.2.1
github.com/nyaruka/gocommon v1.3.0
github.com/nyaruka/goflow v0.102.0
github.com/nyaruka/goflow v0.102.1
github.com/nyaruka/librato v1.0.0
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d
github.com/nyaruka/null v1.2.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ github.com/nyaruka/ezconf v0.2.1 h1:TDXWoqjqYya1uhou1mAJZg7rgFYL98EB0Tb3+BWtUh0=
github.com/nyaruka/ezconf v0.2.1/go.mod h1:ey182kYkw2MIi4XiWe1FR/mzI33WCmTWuceDYYxgnQw=
github.com/nyaruka/gocommon v1.3.0 h1:IqaPT4KQ2oVq/2Ivp/c+RVCs8v71+RzPU2VhMoRrgpU=
github.com/nyaruka/gocommon v1.3.0/go.mod h1:w7lKxIkm/qLAoO9Y3aI1LV7EiYogn6+1C8MTEjxTC9M=
github.com/nyaruka/goflow v0.102.0 h1:WdcQYZ8smr+M2Xbz+3kg1wSl8pVPwz7Ba0Z2ivvehlQ=
github.com/nyaruka/goflow v0.102.0/go.mod h1:wuvXZTs6a6S1rjSRLaQGVxDfKomDJ/1XQoLXCqFekK4=
github.com/nyaruka/goflow v0.102.1 h1:7QX2jTwV7uIbaGnkkpmB+ao+E7Cmyar9g7sRQH4Bu3M=
github.com/nyaruka/goflow v0.102.1/go.mod h1:wuvXZTs6a6S1rjSRLaQGVxDfKomDJ/1XQoLXCqFekK4=
github.com/nyaruka/librato v1.0.0 h1:Vznj9WCeC1yZXbBYyYp40KnbmXLbEkjKmHesV/v2SR0=
github.com/nyaruka/librato v1.0.0/go.mod h1:pkRNLFhFurOz0QqBz6/DuTFhHHxAubWxs4Jx+J7yUgg=
github.com/nyaruka/logrus_sentry v0.8.2-0.20190129182604-c2962b80ba7d h1:hyp9u36KIwbTCo2JAJ+TuJcJBc+UZzEig7RI/S5Dvkc=
Expand Down
Binary file modified mailroom_test.dump
Binary file not shown.
48 changes: 21 additions & 27 deletions models/contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import (
"github.com/nyaruka/goflow/flows"
"github.com/nyaruka/goflow/utils/uuids"
"github.com/nyaruka/null"
"github.com/olivere/elastic"

"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"github.com/olivere/elastic"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -52,10 +52,18 @@ const (
ContactStatusArchived = "V"
)

var contactStatusMap = map[flows.ContactStatus]ContactStatus{
flows.ContactStatusActive: ContactStatusActive,
flows.ContactStatusBlocked: ContactStatusBlocked,
flows.ContactStatusStopped: ContactStatusStopped,
var contactToModelStatus = map[flows.ContactStatus]ContactStatus{
flows.ContactStatusActive: ContactStatusActive,
flows.ContactStatusBlocked: ContactStatusBlocked,
flows.ContactStatusStopped: ContactStatusStopped,
flows.ContactStatusArchived: ContactStatusArchived,
}

var contactToFlowStatus = map[ContactStatus]flows.ContactStatus{
ContactStatusActive: flows.ContactStatusActive,
ContactStatusBlocked: flows.ContactStatusBlocked,
ContactStatusStopped: flows.ContactStatusStopped,
ContactStatusArchived: flows.ContactStatusArchived,
}

// LoadContact loads a contact from the passed in id
Expand Down Expand Up @@ -93,8 +101,7 @@ func LoadContacts(ctx context.Context, db Queryer, org *OrgAssets, ids []Contact
uuid: e.UUID,
name: e.Name,
language: e.Language,
isStopped: e.IsStopped,
isBlocked: e.IsBlocked,
status: e.Status,
createdOn: e.CreatedOn,
modifiedOn: e.ModifiedOn,
lastSeenOn: e.LastSeenOn,
Expand Down Expand Up @@ -354,7 +361,7 @@ func (c *Contact) FlowContact(org *OrgAssets) (*flows.Contact, error) {
flows.ContactID(c.id),
c.name,
c.language,
c.Status(),
contactToFlowStatus[c.Status()],
org.Env().Timezone(),
c.createdOn,
c.lastSeenOn,
Expand Down Expand Up @@ -387,7 +394,7 @@ func (c *Contact) Unstop(ctx context.Context, db *sqlx.DB) error {
if err != nil {
return errors.Wrapf(err, "error unstopping contact")
}
c.isStopped = false
c.status = ContactStatusActive
return nil
}

Expand All @@ -397,8 +404,7 @@ type Contact struct {
uuid flows.ContactUUID
name string
language envs.Language
isStopped bool
isBlocked bool
status ContactStatus
fields map[string]*flows.Value
groups []*Group
urns []urns.URN
Expand All @@ -411,24 +417,14 @@ func (c *Contact) ID() ContactID { return c.id }
func (c *Contact) UUID() flows.ContactUUID { return c.uuid }
func (c *Contact) Name() string { return c.name }
func (c *Contact) Language() envs.Language { return c.language }
func (c *Contact) IsStopped() bool { return c.isStopped }
func (c *Contact) IsBlocked() bool { return c.isBlocked }
func (c *Contact) Status() ContactStatus { return c.status }
func (c *Contact) Fields() map[string]*flows.Value { return c.fields }
func (c *Contact) Groups() []*Group { return c.groups }
func (c *Contact) URNs() []urns.URN { return c.urns }
func (c *Contact) CreatedOn() time.Time { return c.createdOn }
func (c *Contact) ModifiedOn() time.Time { return c.modifiedOn }
func (c *Contact) LastSeenOn() *time.Time { return c.lastSeenOn }

func (c *Contact) Status() flows.ContactStatus {
if c.isBlocked {
return flows.ContactStatusBlocked
} else if c.isStopped {
return flows.ContactStatusStopped
}
return flows.ContactStatusActive
}

// fieldValueEnvelope is our utility struct for the value of a field
type fieldValueEnvelope struct {
Text types.XText `json:"text"`
Expand Down Expand Up @@ -484,8 +480,7 @@ type contactEnvelope struct {
UUID flows.ContactUUID `json:"uuid"`
Name string `json:"name"`
Language envs.Language `json:"language"`
IsStopped bool `json:"is_stopped"`
IsBlocked bool `json:"is_blocked"`
Status ContactStatus `json:"status"`
Fields map[assets.FieldUUID]*fieldValueEnvelope `json:"fields"`
GroupIDs []GroupID `json:"group_ids"`
URNs []ContactURN `json:"urns"`
Expand All @@ -501,8 +496,7 @@ SELECT ROW_TO_JSON(r) FROM (SELECT
uuid,
name,
language,
is_stopped,
is_blocked,
status,
is_active,
created_on,
modified_on,
Expand Down Expand Up @@ -1349,7 +1343,7 @@ func UpdateContactStatus(ctx context.Context, tx Queryer, changes []*ContactStat
for _, ch := range changes {
blocked := ch.Status == flows.ContactStatusBlocked
stopped := ch.Status == flows.ContactStatusStopped
status := contactStatusMap[ch.Status]
status := contactToModelStatus[ch.Status]

if blocked || stopped {
archiveTriggersForContactIDs = append(archiveTriggersForContactIDs, ch.ContactID)
Expand Down
2 changes: 1 addition & 1 deletion models/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ func CreateBroadcastMessages(ctx context.Context, db Queryer, rp *redis.Pool, oa

// utility method to build up our message
buildMessage := func(c *Contact, forceURN urns.URN) (*Msg, error) {
if c.IsStopped() || c.IsBlocked() {
if c.Status() != ContactStatusActive {
return nil, nil
}

Expand Down
10 changes: 5 additions & 5 deletions tasks/handler/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ func handleTimedEvent(ctx context.Context, db *sqlx.DB, rp *redis.Pool, eventTyp
return errors.Wrapf(err, "error loading contact")
}

// contact has been deleted or is blocked, ignore this event
if len(contacts) == 0 || contacts[0].IsBlocked() {
// contact has been deleted or is blocked/stopped/archived, ignore this event
if len(contacts) == 0 || contacts[0].Status() != models.ContactStatusActive {
return nil
}

Expand Down Expand Up @@ -315,7 +315,7 @@ func HandleChannelEvent(ctx context.Context, db *sqlx.DB, rp *redis.Pool, eventT
}

// contact has been deleted or is blocked, ignore this event
if len(contacts) == 0 || contacts[0].IsBlocked() {
if len(contacts) == 0 || contacts[0].Status() == models.ContactStatusBlocked {
return nil, nil
}

Expand Down Expand Up @@ -526,7 +526,7 @@ func handleMsgEvent(ctx context.Context, db *sqlx.DB, rp *redis.Pool, event *Msg
}

// if this channel is no longer active or this contact is blocked, ignore this message (mark it as handled)
if channel == nil || modelContact.IsBlocked() {
if channel == nil || modelContact.Status() == models.ContactStatusBlocked {
err := models.UpdateMessage(ctx, db, event.MsgID, models.MsgStatusHandled, models.VisibilityArchived, models.TypeInbox, topupID)
if err != nil {
return errors.Wrapf(err, "error marking blocked or nil channel message as handled")
Expand All @@ -536,7 +536,7 @@ func handleMsgEvent(ctx context.Context, db *sqlx.DB, rp *redis.Pool, event *Msg

// stopped contact? they are unstopped if they send us an incoming message
newContact := event.NewContact
if modelContact.IsStopped() {
if modelContact.Status() == models.ContactStatusStopped {
err := modelContact.Unstop(ctx, db)
if err != nil {
return errors.Wrapf(err, "error unstopping contact")
Expand Down
129 changes: 129 additions & 0 deletions web/contact/testdata/modify.json
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,135 @@
}
]
},
{
"label": "set status to blocked",
"method": "POST",
"path": "/mr/contact/modify",
"body": {
"org_id": 1,
"user_id": 1,
"contact_ids": [
10000
],
"modifiers": [
{
"type": "status",
"status": "blocked"
}
]
},
"status": 200,
"response": {
"10000": {
"contact": {
"uuid": "6393abc0-283d-4c9b-a1b3-641a035c34bf",
"id": 10000,
"status": "blocked",
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z"
},
"events": [
{
"type": "contact_status_changed",
"created_on": "2018-07-06T12:30:00.123456789Z",
"status": "blocked"
}
]
}
},
"db_assertions": [
{
"query": "SELECT count(*) FROM contacts_contact WHERE id = 10000 AND status = 'B'",
"count": 1
}
]
},
{
"label": "set status to archived",
"method": "POST",
"path": "/mr/contact/modify",
"body": {
"org_id": 1,
"user_id": 1,
"contact_ids": [
10000
],
"modifiers": [
{
"type": "status",
"status": "archived"
}
]
},
"status": 200,
"response": {
"10000": {
"contact": {
"uuid": "6393abc0-283d-4c9b-a1b3-641a035c34bf",
"id": 10000,
"status": "archived",
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z"
},
"events": [
{
"type": "contact_status_changed",
"created_on": "2018-07-06T12:30:00.123456789Z",
"status": "archived"
}
]
}
},
"db_assertions": [
{
"query": "SELECT count(*) FROM contacts_contact WHERE id = 10000 AND status = 'V'",
"count": 1
}
]
},
{
"label": "set status to active",
"method": "POST",
"path": "/mr/contact/modify",
"body": {
"org_id": 1,
"user_id": 1,
"contact_ids": [
10000
],
"modifiers": [
{
"type": "status",
"status": "active"
}
]
},
"status": 200,
"response": {
"10000": {
"contact": {
"uuid": "6393abc0-283d-4c9b-a1b3-641a035c34bf",
"id": 10000,
"status": "active",
"timezone": "America/Los_Angeles",
"created_on": "2018-07-06T12:30:00.123457Z"
},
"events": [
{
"type": "contact_status_changed",
"created_on": "2018-07-06T12:30:00.123456789Z",
"status": "active"
}
]
}
},
"db_assertions": [
{
"query": "SELECT count(*) FROM contacts_contact WHERE id = 10000 AND status = 'A'",
"count": 1
}
]
},
{
"label": "set text field with valid value",
"method": "POST",
Expand Down
6 changes: 3 additions & 3 deletions web/ivr/ivr.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ func handleFlow(ctx context.Context, s *web.Server, r *http.Request, rawW http.R
return client.WriteErrorResponse(w, errors.Wrapf(err, "no such contact"))
}
if len(contacts) == 0 {
return client.WriteErrorResponse(w, errors.Errorf("no contact width id: %d", conn.ContactID()))
return client.WriteErrorResponse(w, errors.Errorf("no contact with id: %d", conn.ContactID()))
}
if contacts[0].IsStopped() || contacts[0].IsBlocked() {
return client.WriteErrorResponse(w, errors.Errorf("no contact width id: %d", conn.ContactID()))
if contacts[0].Status() != models.ContactStatusActive {
return client.WriteErrorResponse(w, errors.Errorf("no contact with id: %d", conn.ContactID()))
}

// load the URN for this connection
Expand Down
2 changes: 1 addition & 1 deletion web/ivr/ivr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func TestNexmoIVR(t *testing.T) {
} else {
type CallForm struct {
To []struct {
Number int64 `json:"number`
Number int64 `json:"number"`
} `json:"to"`
}
body, _ := ioutil.ReadAll(r.Body)
Expand Down

0 comments on commit 809f3b9

Please sign in to comment.