feat(notify): Add VerifiedNote for SSubContact

VerifiedNote will be 'ok' if SSubContact verified successfully.
Otherwise, there will be there value for VerifiedNote:
1. no such mobile: mobile is not exist, change and try again.
2. incomplete config: tell admin to improve the configuration.
3. service exceptions: try again or connect to admin.
This commit is contained in:
rainzm
2020-09-03 12:16:37 +08:00
parent bf12f5b57c
commit 3e982ab79a
3 changed files with 44 additions and 3 deletions
+22
View File
@@ -372,6 +372,26 @@ func (r *SReceiver) MarkContactTypeVerified(contactType string) error {
return nil
}
func (r *SReceiver) MarkContactTypeUnVerified(contactType string, note string) error {
if err := r.PullCache(false); err != nil {
return err
}
if sc, ok := r.subContactCache[contactType]; ok {
sc.Verified = tristate.False
sc.VerifiedNote = note
} else {
subContact := &SSubContact{
Type: contactType,
ReceiverID: r.Id,
VerifiedNote: note,
Verified: tristate.False,
}
subContact.ParentContactType = api.MOBILE
r.subContactCache[contactType] = subContact
}
return nil
}
func (r *SReceiver) setVerifiedContactType(contactType string, enabled bool) {
switch contactType {
case api.EMAIL:
@@ -630,6 +650,7 @@ func (r *SReceiver) PreUpdate(ctx context.Context, userCred mcclient.TokenCreden
for _, c := range r.subContactCache {
if c.ParentContactType == api.EMAIL {
c.Verified = tristate.False
c.VerifiedNote = "email changed, re-verify"
}
}
}
@@ -638,6 +659,7 @@ func (r *SReceiver) PreUpdate(ctx context.Context, userCred mcclient.TokenCreden
for _, c := range r.subContactCache {
if c.ParentContactType == api.MOBILE {
c.Verified = tristate.False
c.VerifiedNote = "mobile changed, re-verify"
}
}
}
+1
View File
@@ -35,6 +35,7 @@ type SSubContact struct {
ParentContactType string `width:"16" nullable:"false"`
Enabled tristate.TriState `nullable:"false" default:"false"`
Verified tristate.TriState `nullable:"false" default:"false"`
VerifiedNote string `width:"1024"`
}
var SubContactManager *SSubContactManager
+21 -3
View File
@@ -20,11 +20,13 @@ import (
"yunion.io/x/jsonutils"
"yunion.io/x/log"
"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"
apis "yunion.io/x/onecloud/pkg/apis/notify"
"yunion.io/x/onecloud/pkg/cloudcommon/db"
"yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
"yunion.io/x/onecloud/pkg/notify"
"yunion.io/x/onecloud/pkg/notify/models"
"yunion.io/x/onecloud/pkg/util/logclient"
)
@@ -51,6 +53,7 @@ func (self *SubcontactPullTask) taskFailed(ctx context.Context, receiver *models
}
func (self *SubcontactPullTask) OnInit(ctx context.Context, obj db.IStandaloneModel, body jsonutils.JSONObject) {
failedReasons := make([]string, 0)
// pull contacts
receiver := obj.(*models.SReceiver)
if len(receiver.Mobile) == 0 {
@@ -64,13 +67,28 @@ func (self *SubcontactPullTask) OnInit(ctx context.Context, obj db.IStandaloneMo
}
userid, err := models.NotifyService.ContactByMobile(ctx, receiver.Mobile, cType)
if err != nil {
reason := fmt.Sprintf("fail to get %s contact by mobile %q: %v", cType, receiver.Mobile, err)
self.taskFailed(ctx, receiver, reason)
return
var reason string
if errors.Cause(err) == notify.ErrNoSuchMobile {
receiver.MarkContactTypeUnVerified(cType, notify.ErrNoSuchMobile.Error())
reason = fmt.Sprintf("%q: no such mobile %s", cType, receiver.Mobile)
} else if errors.Cause(err) == notify.ErrIncompleteConfig {
receiver.MarkContactTypeUnVerified(cType, notify.ErrIncompleteConfig.Error())
reason = fmt.Sprintf("%q: %v", cType, err)
} else {
receiver.MarkContactTypeUnVerified(cType, "service exceptions")
reason = fmt.Sprintf("%q: %v", cType, err)
}
failedReasons = append(failedReasons, reason)
continue
}
receiver.SetContact(cType, userid)
receiver.MarkContactTypeVerified(cType)
}
if len(failedReasons) > 0 {
reason := strings.Join(failedReasons, "; ")
self.taskFailed(ctx, receiver, reason)
return
}
// push cache
err := receiver.PushCache(ctx)
if err != nil {