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
783c60ee
authored
Mar 06, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: Enhance channel status update with success tracking and dynamic notification #812
parent
063618e2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
18 deletions
+34
-18
model/channel.go
+18
-10
service/channel.go
+16
-8
No files found.
model/channel.go
View file @
783c60ee
...
...
@@ -290,35 +290,42 @@ func (channel *Channel) Delete() error {
var
channelStatusLock
sync
.
Mutex
func
UpdateChannelStatusById
(
id
int
,
status
int
,
reason
string
)
{
func
UpdateChannelStatusById
(
id
int
,
status
int
,
reason
string
)
bool
{
if
common
.
MemoryCacheEnabled
{
channelStatusLock
.
Lock
()
defer
channelStatusLock
.
Unlock
()
channelCache
,
_
:=
CacheGetChannel
(
id
)
// 如果缓存渠道存在,且状态已是目标状态,直接返回
if
channelCache
!=
nil
&&
channelCache
.
Status
==
status
{
channelStatusLock
.
Unlock
()
return
return
false
}
// 如果缓存渠道不存在(说明已经被禁用),且要设置的状态不为启用,直接返回
if
channelCache
==
nil
&&
status
!=
common
.
ChannelStatusEnabled
{
channelStatusLock
.
Unlock
()
return
return
false
}
CacheUpdateChannelStatus
(
id
,
status
)
channelStatusLock
.
Unlock
()
}
err
:=
UpdateAbilityStatus
(
id
,
status
==
common
.
ChannelStatusEnabled
)
if
err
!=
nil
{
common
.
SysError
(
"failed to update ability status: "
+
err
.
Error
())
return
false
}
channel
,
err
:=
GetChannelById
(
id
,
true
)
if
err
!=
nil
{
// find channel by id error, directly update status
err
=
DB
.
Model
(
&
Channel
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"status"
,
status
)
.
Error
if
err
!=
nil
{
common
.
SysError
(
"failed to update channel status: "
+
err
.
Error
())
result
:=
DB
.
Model
(
&
Channel
{})
.
Where
(
"id = ?"
,
id
)
.
Update
(
"status"
,
status
)
if
result
.
Error
!=
nil
{
common
.
SysError
(
"failed to update channel status: "
+
result
.
Error
.
Error
())
return
false
}
if
result
.
RowsAffected
==
0
{
return
false
}
}
else
{
if
channel
.
Status
==
status
{
return
false
}
// find channel by id success, update status and other info
info
:=
channel
.
GetOtherInfo
()
info
[
"status_reason"
]
=
reason
...
...
@@ -328,9 +335,10 @@ func UpdateChannelStatusById(id int, status int, reason string) {
err
=
channel
.
Save
()
if
err
!=
nil
{
common
.
SysError
(
"failed to update channel status: "
+
err
.
Error
())
return
false
}
}
return
true
}
func
EnableChannelByTag
(
tag
string
)
error
{
...
...
service/channel.go
View file @
783c60ee
...
...
@@ -10,19 +10,27 @@ import (
"strings"
)
func
formatNotifyType
(
channelId
int
,
status
int
)
string
{
return
fmt
.
Sprintf
(
"%s_%d_%d"
,
dto
.
NotifyTypeChannelUpdate
,
channelId
,
status
)
}
// disable & notify
func
DisableChannel
(
channelId
int
,
channelName
string
,
reason
string
)
{
model
.
UpdateChannelStatusById
(
channelId
,
common
.
ChannelStatusAutoDisabled
,
reason
)
subject
:=
fmt
.
Sprintf
(
"通道「%s」(#%d)已被禁用"
,
channelName
,
channelId
)
content
:=
fmt
.
Sprintf
(
"通道「%s」(#%d)已被禁用,原因:%s"
,
channelName
,
channelId
,
reason
)
NotifyRootUser
(
dto
.
NotifyTypeChannelUpdate
,
subject
,
content
)
success
:=
model
.
UpdateChannelStatusById
(
channelId
,
common
.
ChannelStatusAutoDisabled
,
reason
)
if
success
{
subject
:=
fmt
.
Sprintf
(
"通道「%s」(#%d)已被禁用"
,
channelName
,
channelId
)
content
:=
fmt
.
Sprintf
(
"通道「%s」(#%d)已被禁用,原因:%s"
,
channelName
,
channelId
,
reason
)
NotifyRootUser
(
formatNotifyType
(
channelId
,
common
.
ChannelStatusAutoDisabled
),
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
(
dto
.
NotifyTypeChannelUpdate
,
subject
,
content
)
success
:=
model
.
UpdateChannelStatusById
(
channelId
,
common
.
ChannelStatusEnabled
,
""
)
if
success
{
subject
:=
fmt
.
Sprintf
(
"通道「%s」(#%d)已被启用"
,
channelName
,
channelId
)
content
:=
fmt
.
Sprintf
(
"通道「%s」(#%d)已被启用"
,
channelName
,
channelId
)
NotifyRootUser
(
formatNotifyType
(
channelId
,
common
.
ChannelStatusEnabled
),
subject
,
content
)
}
}
func
ShouldDisableChannel
(
channelType
int
,
err
*
dto
.
OpenAIErrorWithStatusCode
)
bool
{
...
...
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