Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
28fe74d7
authored
Nov 15, 2023
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
try to fix email
parent
b4c89fba
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
21 additions
and
15 deletions
+21
-15
controller/relay-audio.go
+1
-1
controller/relay-image.go
+1
-1
controller/relay-mj.go
+1
-1
controller/relay-text.go
+2
-2
model/token.go
+16
-10
No files found.
controller/relay-audio.go
View file @
28fe74d7
...
...
@@ -99,7 +99,7 @@ func relayAudioHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
go
func
()
{
quota
:=
countTokenText
(
audioResponse
.
Text
,
audioModel
)
quotaDelta
:=
quota
-
preConsumedQuota
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
quotaDelta
,
preConsumedQuota
)
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
common
.
SysError
(
"error consuming token remain quota: "
+
err
.
Error
())
}
...
...
controller/relay-image.go
View file @
28fe74d7
...
...
@@ -147,7 +147,7 @@ func relayImageHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode
var
textResponse
ImageResponse
defer
func
(
ctx
context
.
Context
)
{
if
consumeQuota
{
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userId
,
quota
,
0
)
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userId
,
quota
,
0
,
true
)
if
err
!=
nil
{
common
.
SysError
(
"error consuming token remain quota: "
+
err
.
Error
())
}
...
...
controller/relay-mj.go
View file @
28fe74d7
...
...
@@ -359,7 +359,7 @@ func relayMidjourneySubmit(c *gin.Context, relayMode int) *MidjourneyResponse {
defer
func
(
ctx
context
.
Context
)
{
if
consumeQuota
{
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
quota
,
0
)
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
quota
,
0
,
true
)
if
err
!=
nil
{
common
.
SysError
(
"error consuming token remain quota: "
+
err
.
Error
())
}
...
...
controller/relay-text.go
View file @
28fe74d7
...
...
@@ -400,7 +400,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
if
preConsumedQuota
!=
0
{
go
func
(
ctx
context
.
Context
)
{
// return pre-consumed quota
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
-
preConsumedQuota
,
0
)
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
-
preConsumedQuota
,
0
,
false
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error return pre-consumed quota: "
+
err
.
Error
())
}
...
...
@@ -434,7 +434,7 @@ func relayTextHelper(c *gin.Context, relayMode int) *OpenAIErrorWithStatusCode {
quota
=
0
}
quotaDelta
:=
quota
-
preConsumedQuota
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
quotaDelta
,
preConsumedQuota
)
err
:=
model
.
PostConsumeTokenQuota
(
tokenId
,
userQuota
,
quotaDelta
,
preConsumedQuota
,
true
)
if
err
!=
nil
{
common
.
LogError
(
ctx
,
"error consuming token remain quota: "
+
err
.
Error
())
}
...
...
model/token.go
View file @
28fe74d7
...
...
@@ -5,6 +5,7 @@ import (
"fmt"
"gorm.io/gorm"
"one-api/common"
"strconv"
"strings"
)
...
...
@@ -194,22 +195,31 @@ func PreConsumeTokenQuota(tokenId int, quota int) (userQuota int, err error) {
return
0
,
err
}
if
userQuota
<
quota
{
return
userQuota
,
errors
.
New
(
fmt
.
Sprintf
(
"用户额度不足,剩余额度为 %d"
,
userQuota
))
return
0
,
errors
.
New
(
fmt
.
Sprintf
(
"用户额度不足,剩余额度为 %d"
,
userQuota
))
}
if
!
token
.
UnlimitedQuota
{
err
=
DecreaseTokenQuota
(
tokenId
,
quota
)
if
err
!=
nil
{
return
userQuota
,
err
return
0
,
err
}
}
err
=
DecreaseUserQuota
(
token
.
UserId
,
quota
)
return
userQuota
,
err
return
userQuota
-
quota
,
err
}
func
PostConsumeTokenQuota
(
tokenId
int
,
userQuota
int
,
quota
int
,
preConsumedQuota
int
)
(
err
error
)
{
func
PostConsumeTokenQuota
(
tokenId
int
,
userQuota
int
,
quota
int
,
preConsumedQuota
int
,
sendEmail
bool
)
(
err
error
)
{
token
,
err
:=
GetTokenById
(
tokenId
)
if
quota
>
0
{
err
=
DecreaseUserQuota
(
token
.
UserId
,
quota
)
}
else
{
err
=
IncreaseUserQuota
(
token
.
UserId
,
-
quota
)
}
if
err
!=
nil
{
return
err
}
if
sendEmail
{
quotaTooLow
:=
userQuota
>=
common
.
QuotaRemindThreshold
&&
userQuota
-
(
quota
+
preConsumedQuota
)
<
common
.
QuotaRemindThreshold
noMoreQuota
:=
userQuota
-
(
quota
+
preConsumedQuota
)
<=
0
if
quotaTooLow
||
noMoreQuota
{
...
...
@@ -229,16 +239,12 @@ func PostConsumeTokenQuota(tokenId int, userQuota int, quota int, preConsumedQuo
if
err
!=
nil
{
common
.
SysError
(
"failed to send email"
+
err
.
Error
())
}
common
.
SysLog
(
"user quota is low, consumed quota: "
+
strconv
.
Itoa
(
quota
)
+
", user quota: "
+
strconv
.
Itoa
(
userQuota
))
}
}()
}
err
=
DecreaseUserQuota
(
token
.
UserId
,
quota
)
}
else
{
err
=
IncreaseUserQuota
(
token
.
UserId
,
-
quota
)
}
if
err
!=
nil
{
return
err
}
if
!
token
.
UnlimitedQuota
{
if
quota
>
0
{
err
=
DecreaseTokenQuota
(
tokenId
,
quota
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment