feat: add notifier (#144)

* ♻️ refactor: email refactor

*  feat: add notifier
This commit is contained in:
Buer
2024-04-09 15:00:06 +08:00
committed by GitHub
parent 76d22f0572
commit a3719cd78a
33 changed files with 1386 additions and 188 deletions

View File

@@ -265,6 +265,19 @@ func (channel *Channel) Delete() error {
return err
}
func (channel *Channel) StatusToStr() string {
switch channel.Status {
case common.ChannelStatusEnabled:
return "启用"
case common.ChannelStatusAutoDisabled:
return "自动禁用"
case common.ChannelStatusManuallyDisabled:
return "手动禁用"
}
return "禁用"
}
func UpdateChannelStatusById(id int, status int) {
err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
if err != nil {

View File

@@ -22,6 +22,7 @@ func SetupDB() {
common.FatalLog("failed to initialize database: " + err.Error())
}
ChannelGroup.Load()
common.RootUserEmail = GetRootUserEmail()
if viper.GetBool("BATCH_UPDATE_ENABLED") {
common.BatchUpdateEnabled = true

View File

@@ -2,8 +2,8 @@ package model
import (
"errors"
"fmt"
"one-api/common"
"one-api/common/stmp"
"gorm.io/gorm"
)
@@ -114,15 +114,13 @@ func GetTokenById(id int) (*Token, error) {
}
func (token *Token) Insert() error {
var err error
err = DB.Create(token).Error
err := DB.Create(token).Error
return err
}
// Update Make sure your token's fields is completed, because this will update non-zero values
func (token *Token) Update() error {
var err error
err = DB.Model(token).Select("name", "status", "expired_time", "remain_quota", "unlimited_quota").Updates(token).Error
err := DB.Model(token).Select("name", "status", "expired_time", "remain_quota", "unlimited_quota").Updates(token).Error
return err
}
@@ -132,8 +130,7 @@ func (token *Token) SelectUpdate() error {
}
func (token *Token) Delete() error {
var err error
err = DB.Delete(token).Error
err := DB.Delete(token).Error
return err
}
@@ -228,26 +225,35 @@ func PreConsumeTokenQuota(tokenId int, quota int) (err error) {
}
func sendQuotaWarningEmail(userId int, userQuota int, noMoreQuota bool) {
email, err := GetUserEmail(userId)
if err != nil {
user := User{Id: userId}
if err := user.FillUserById(); err != nil {
common.SysError("failed to fetch user email: " + err.Error())
return
}
prompt := "您的额度即将用尽"
if noMoreQuota {
prompt = "您的额度已用尽"
if user.Email == "" {
common.SysError("user email is empty")
return
}
if email != "" {
topUpLink := fmt.Sprintf("%s/topup", common.ServerAddress)
err = common.SendEmail(prompt, email,
fmt.Sprintf("%s当前剩余额度为 %d为了不影响您的使用请及时充值。<br/>充值链接:<a href='%s'>%s</a>", prompt, userQuota, topUpLink, topUpLink))
if err != nil {
common.SysError("failed to send email" + err.Error())
}
userName := user.DisplayName
if userName == "" {
userName = user.Username
}
err := stmp.SendQuotaWarningCodeEmail(userName, user.Email, userQuota, noMoreQuota)
if err != nil {
common.SysError("failed to send email" + err.Error())
}
}
func PostConsumeTokenQuota(tokenId int, quota int) (err error) {
token, err := GetTokenById(tokenId)
if err != nil {
return err
}
if quota > 0 {
err = DecreaseUserQuota(token.UserId, quota)
} else {

View File

@@ -149,6 +149,11 @@ func (user *User) Update(updatePassword bool) error {
}
}
err = DB.Model(user).Updates(user).Error
if err == nil && user.Role == common.RoleRootUser {
common.RootUserEmail = user.Email
}
return err
}
@@ -201,7 +206,14 @@ func (user *User) FillUserById() error {
if user.Id == 0 {
return errors.New("id 为空!")
}
DB.Where(User{Id: user.Id}).First(user)
result := DB.Where(User{Id: user.Id}).First(user)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return errors.New("没有找到用户!")
}
return result.Error
}
return nil
}
@@ -209,7 +221,14 @@ func (user *User) FillUserByEmail() error {
if user.Email == "" {
return errors.New("email 为空!")
}
DB.Where(User{Email: user.Email}).First(user)
result := DB.Where(User{Email: user.Email}).First(user)
if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return errors.New("没有找到用户!")
}
return result.Error
}
return nil
}