fix: notify empty receipients (#22035)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2025-01-28 18:54:42 +08:00
committed by GitHub
co-authored by Qiu Jian
parent dd75e4d4f9
commit 0c4892bed0
2 changed files with 26 additions and 10 deletions
+1 -1
View File
@@ -42,7 +42,7 @@ var (
)
func init() {
notifyClientWorkerMan = appsrv.NewWorkerManager("NotifyClientWorkerManager", 1, 50, false)
notifyClientWorkerMan = appsrv.NewWorkerManager("NotifyClientWorkerManager", 1, 1024, false)
// set db notify hook
db.SetUpdateNotifyHook(func(ctx context.Context, userCred mcclient.TokenCredential, obj db.IModel) {
@@ -182,6 +182,16 @@ func lang(ctx context.Context, contactType npk.TNotifyChannel, reIds []string, c
return langMap, nil
}
func isEmptyRecipients(recipientId []string) bool {
var recvs []string
for _, c := range recipientId {
if len(c) > 0 {
recvs = append(recvs, c)
}
}
return len(recvs) == 0
}
func genMsgViaLang(ctx context.Context, p sNotifyParams) ([]npk.SNotifyMessage, error) {
reIds := make([]string, 0)
s, err := AdminSessionGenerator(context.Background(), consts.GetRegion())
@@ -206,6 +216,10 @@ func genMsgViaLang(ctx context.Context, p sNotifyParams) ([]npk.SNotifyMessage,
reIds = p.recipientId
}
if isEmptyRecipients(p.recipientId) {
return nil, errors.Wrap(errors.ErrEmpty, "empty receipients")
}
if !hasTemplateOfTopic(p.event) {
msg := npk.SNotifyMessage{}
msg.Uid = reIds
@@ -225,6 +239,9 @@ func genMsgViaLang(ctx context.Context, p sNotifyParams) ([]npk.SNotifyMessage,
if p.channel == npk.NotifyByRobot {
langMap, err = langRobot(ctx, p.robots)
if err != nil {
return nil, err
}
} else {
langMap, err = lang(ctx, p.channel, reIds, p.contacts)
if err != nil {
@@ -332,13 +349,10 @@ func notifyNormal(ctx context.Context, recipientId []string, isGroup bool, event
}
func notifyWithChannel(ctx context.Context, p sNotifyParams, channels ...npk.TNotifyChannel) {
reps := p.recipientId
for _, c := range channels {
p.recipientId = []string{}
p.contacts = []string{}
p.channel = c
p.recipientId = reps
rawNotify(ctx, p)
np := p
np.channel = c
rawNotify(ctx, np)
}
}
@@ -367,7 +381,7 @@ func (t *notifyTask) Run() {
break
}
if !t.createReceiver {
log.Errorf("unable to send notification: %v", err)
log.Errorf("unable to send notification to contacts %s uid %s gid %s @%s: %s", strings.Join(t.msg.Contacts, ","), strings.Join(t.msg.Uid, ","), strings.Join(t.msg.Gid, ","), t.msg.ContactType, jsonutils.Marshal(err))
break
}
jerr, ok := err.(*httputils.JSONClientError)
@@ -394,10 +408,12 @@ func (t *notifyTask) Run() {
}
log.Infof("create receiver %q successfully", receiverId)
}
return
}
func intelliNotify(ctx context.Context, p sNotifyParams) {
if isEmptyRecipients(p.recipientId) {
return
}
log.Infof("recipientId: %v, contacts: %v, event %s priority %s", p.recipientId, p.contacts, p.event, p.priority)
msgs, err := genMsgViaLang(ctx, p)
if err != nil {
@@ -405,7 +421,7 @@ func intelliNotify(ctx context.Context, p sNotifyParams) {
}
for i := range msgs {
t := notifyTask{
ctx: context.Background(),
ctx: ctx,
createReceiver: p.createReceiver,
msg: msgs[i],
}