mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-17 16:06:38 +08:00
54 lines
1.5 KiB
Go
54 lines
1.5 KiB
Go
package service
|
||
|
||
import (
|
||
"fmt"
|
||
"net/http"
|
||
"one-api/common"
|
||
relaymodel "one-api/dto"
|
||
"one-api/model"
|
||
)
|
||
|
||
// disable & notify
|
||
func DisableChannel(channelId int, channelName string, reason string) {
|
||
model.UpdateChannelStatusById(channelId, common.ChannelStatusAutoDisabled)
|
||
subject := fmt.Sprintf("通道「%s」(#%d)已被禁用", channelName, channelId)
|
||
content := fmt.Sprintf("通道「%s」(#%d)已被禁用,原因:%s", channelName, channelId, reason)
|
||
notifyRootUser(subject, content)
|
||
}
|
||
|
||
func EnableChannel(channelId int, channelName string) {
|
||
model.UpdateChannelStatusById(channelId, common.ChannelStatusEnabled)
|
||
subject := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
||
content := fmt.Sprintf("通道「%s」(#%d)已被启用", channelName, channelId)
|
||
notifyRootUser(subject, content)
|
||
}
|
||
|
||
func ShouldDisableChannel(err *relaymodel.OpenAIError, statusCode int) bool {
|
||
if !common.AutomaticDisableChannelEnabled {
|
||
return false
|
||
}
|
||
if err == nil {
|
||
return false
|
||
}
|
||
if statusCode == http.StatusUnauthorized {
|
||
return true
|
||
}
|
||
if err.Type == "insufficient_quota" || err.Code == "invalid_api_key" || err.Code == "account_deactivated" || err.Code == "billing_not_active" {
|
||
return true
|
||
}
|
||
return false
|
||
}
|
||
|
||
func ShouldEnableChannel(err error, openAIErr *relaymodel.OpenAIError) bool {
|
||
if !common.AutomaticEnableChannelEnabled {
|
||
return false
|
||
}
|
||
if err != nil {
|
||
return false
|
||
}
|
||
if openAIErr != nil {
|
||
return false
|
||
}
|
||
return true
|
||
}
|