From 3e432c77bdb0a6cfb4c14dd5657b61ad6f4ac3d5 Mon Sep 17 00:00:00 2001 From: "Laisky.Cai" Date: Sun, 9 Mar 2025 12:52:08 +0000 Subject: [PATCH] fix: streamline Notify function with switch-case for notification methods --- common/message/main.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/common/message/main.go b/common/message/main.go index 20bab35e..2aa4657d 100644 --- a/common/message/main.go +++ b/common/message/main.go @@ -1,6 +1,8 @@ package message import ( + "fmt" + "github.com/pkg/errors" "github.com/songquanpeng/one-api/common/config" ) @@ -12,8 +14,8 @@ const ( ) func Notify(by string, title string, description string, content string) error { - - if by == ByAll { + switch by { + case ByAll: var errMsgs []string if err := SendEmail(title, config.RootUserEmail, content); err != nil { errMsgs = append(errMsgs, fmt.Sprintf("failed to send email: %v", err)) @@ -26,13 +28,11 @@ func Notify(by string, title string, description string, content string) error { return fmt.Errorf("multiple errors occurred: %v", errMsgs) } return nil - } - - if by == ByEmail { + case ByEmail: return SendEmail(title, config.RootUserEmail, content) - } - if by == ByMessagePusher { + case ByMessagePusher: return SendMessage(title, description, content) + default: + return errors.Errorf("unknown notify method: %s", by) } - return errors.Errorf("unknown notify method: %s", by) }