From 8a80f96d62ac3f003b4c3a56c27103f02293da8b Mon Sep 17 00:00:00 2001 From: rainzm Date: Thu, 3 Sep 2020 11:10:29 +0800 Subject: [PATCH] refactor(notify): hide SSunContact webconsole Every receiver has webconsole type contact that is equal with receiverid. Now, make webconsole invisible, and it does exist. --- pkg/notify/models/receiver.go | 40 +++++++----------------- pkg/notify/tasks/subcontact_pull_task.go | 2 -- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/pkg/notify/models/receiver.go b/pkg/notify/models/receiver.go index 75d82754e8..db16483343 100644 --- a/pkg/notify/models/receiver.go +++ b/pkg/notify/models/receiver.go @@ -51,13 +51,11 @@ var ( api.DINGTALK, api.FEISHU, api.WORKWX, - api.WEBCONSOLE, } AllSubContactTypes = []string{ api.DINGTALK, api.FEISHU, api.WORKWX, - api.WEBCONSOLE, } AllRobotContactTypes = []string{ api.FEISHU_ROBOT, @@ -155,7 +153,6 @@ func (rm *SReceiverManager) InitializeData() error { receiver.Name = user.Name receiver.DomainId = user.DomainId } - webconsole := false for _, contact := range contacts { switch contact.ContactType { case api.EMAIL: @@ -182,16 +179,12 @@ func (rm *SReceiverManager) InitializeData() error { } else { receiver.VerifiedMobile = tristate.False } + case api.WEBCONSOLE: default: var subContact SSubContact subContact.Type = contact.ContactType - if subContact.Type == api.WEBCONSOLE { - webconsole = true - subContact.Contact = uid - } else { - subContact.ParentContactType = api.MOBILE - subContact.Contact = contact.Contact - } + subContact.ParentContactType = api.MOBILE + subContact.Contact = contact.Contact subContact.ReceiverID = uid subContact.ParentContactType = api.MOBILE if contact.Enabled == "1" { @@ -207,16 +200,6 @@ func (rm *SReceiverManager) InitializeData() error { receiver.subContactCache[contact.ContactType] = &subContact } } - if !webconsole { - receiver.subContactCache[api.WEBCONSOLE] = &SSubContact{ - ReceiverID: receiver.Id, - Type: api.WEBCONSOLE, - Contact: receiver.Id, - ParentContactType: "", - Enabled: tristate.True, - Verified: tristate.True, - } - } err := rm.TableSpec().InsertOrUpdate(ctx, &receiver) if err != nil { return errors.Wrap(err, "InsertOrUpdate") @@ -294,6 +277,9 @@ func (r *SReceiver) IsEnabledContactType(ct string) (bool, error) { if utils.IsInStringArray(ct, AllRobotContactTypes) { return true, nil } + if ct == api.WEBCONSOLE { + return true, nil + } cts, err := r.GetEnabledContactTypes() if err != nil { return false, errors.Wrap(err, "GetEnabledContactTypes") @@ -347,9 +333,7 @@ func (r *SReceiver) setEnabledContactType(contactType string, enabled bool) { ReceiverID: r.Id, Enabled: tristate.NewFromBool(enabled), } - if contactType != api.WEBCONSOLE { - subContact.ParentContactType = api.MOBILE - } + subContact.ParentContactType = api.MOBILE r.subContactCache[contactType] = subContact } } @@ -382,9 +366,7 @@ func (r *SReceiver) MarkContactTypeVerified(contactType string) error { ReceiverID: r.Id, Enabled: tristate.True, } - if contactType != api.WEBCONSOLE { - subContact.ParentContactType = api.MOBILE - } + subContact.ParentContactType = api.MOBILE r.subContactCache[contactType] = subContact } return nil @@ -405,9 +387,7 @@ func (r *SReceiver) setVerifiedContactType(contactType string, enabled bool) { ReceiverID: r.Id, Verified: tristate.NewFromBool(enabled), } - if contactType != api.WEBCONSOLE { - subContact.ParentContactType = api.MOBILE - } + subContact.ParentContactType = api.MOBILE r.subContactCache[contactType] = subContact } } @@ -932,6 +912,8 @@ func (r *SReceiver) GetContact(cType string) (string, error) { return r.Email, nil case cType == api.MOBILE: return r.Mobile, nil + case cType == api.WEBCONSOLE: + return r.Id, nil case utils.IsInStringArray(cType, AllRobotContactTypes): return r.Mobile, nil default: diff --git a/pkg/notify/tasks/subcontact_pull_task.go b/pkg/notify/tasks/subcontact_pull_task.go index e209a17006..0275e1a0ce 100644 --- a/pkg/notify/tasks/subcontact_pull_task.go +++ b/pkg/notify/tasks/subcontact_pull_task.go @@ -71,8 +71,6 @@ func (self *SubcontactPullTask) OnInit(ctx context.Context, obj db.IStandaloneMo receiver.SetContact(cType, userid) receiver.MarkContactTypeVerified(cType) } - receiver.SetContact(apis.WEBCONSOLE, receiver.Id) - receiver.MarkContactTypeVerified(apis.WEBCONSOLE) // push cache err := receiver.PushCache(ctx) if err != nil {