Skip to content

Commit

Permalink
fix(node): contact request for unknown
Browse files Browse the repository at this point in the history
Signed-off-by: Godefroy Ponsinet <godefroy.ponsinet@outlook.com>
  • Loading branch information
90dy committed Apr 8, 2019
1 parent d05c0c4 commit aeda67e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
4 changes: 2 additions & 2 deletions core/node/event_handlers.go
Expand Up @@ -32,8 +32,8 @@ func (n *Node) handleContactRequest(ctx context.Context, input *entity.Event) er
// FIXME: validate input

sql := n.sql(ctx)
_, err = bsql.FindContact(sql, &entity.Contact{ID: attrs.Me.ID})
if err == nil {
contact, err := bsql.FindContact(sql, &entity.Contact{ID: attrs.Me.ID})
if err == nil && contact.Status != entity.Contact_Unknown {
return errorcodes.ErrContactReqExisting.New()
}

Expand Down
6 changes: 3 additions & 3 deletions core/node/nodeapi.go
Expand Up @@ -228,11 +228,11 @@ func (n *Node) ContactRequest(ctx context.Context, req *node.ContactRequestInput
sql := n.sql(ctx)
contact, err := bsql.FindContact(sql, req.ToContact())

if errors.Cause(err) == gorm.ErrRecordNotFound {
if errors.Cause(err) == gorm.ErrRecordNotFound || contact.Status == entity.Contact_Unknown {
// save contact in database
contact = req.ToContact()
contact.Status = entity.Contact_IsRequested
if err = sql.Set("gorm:association_autoupdate", true).Save(contact).Error; err != nil {
if err = bsql.ContactSave(sql, contact); err != nil {
return nil, errorcodes.ErrDbCreate.Wrap(err)
}
} else if err != nil {
Expand All @@ -250,7 +250,7 @@ func (n *Node) ContactRequest(ctx context.Context, req *node.ContactRequestInput
} else if contact.Status == entity.Contact_Myself {
return nil, errorcodes.ErrContactReqMyself.New()

} else if contact.Status != entity.Contact_Unknown {
} else {
return nil, errorcodes.ErrContactReqExisting.New()
}

Expand Down
17 changes: 17 additions & 0 deletions core/sql/helpers.go
Expand Up @@ -180,3 +180,20 @@ func ConversationSave(db *gorm.DB, c *entity.Conversation) error {

return nil
}

func ContactSave(db *gorm.DB, c *entity.Contact) error {
if err := db.Save(c).Error; err != nil {
logger().Error(fmt.Sprintf("cannot save contact %+v, err: %+v", c, err.Error()))
return err
}

c.Devices = append(c.Devices, &entity.Device{ID: c.ID, ContactID: c.ID})
for _, device := range c.Devices {
if err := db.Save(device).Error; err != nil {
logger().Error(fmt.Sprintf("cannot save devices %+v, err %+v", device, err.Error()))
return err
}
}

return nil
}

0 comments on commit aeda67e

Please sign in to comment.