feat: enhance Safew watcher initialization and token management

- Added functionality to list and start Safew tokens in the main server function, ensuring proper initialization of the Safew watcher.
- Introduced `ensureSafewWatcher` method in the ChannelHandler to manage Safew tokens during channel creation and updates.
- Implemented `StartTokens` method in the Watcher to handle multiple tokens efficiently.
- Enhanced error handling and logging for Safew watcher operations to improve observability.
This commit is contained in:
2026-08-15 01:28:51 +08:00
parent 5e783adf7a
commit 2000908bac
4 changed files with 67 additions and 1 deletions
+15
View File
@@ -49,6 +49,7 @@ func (h *ChannelHandler) Create(c *gin.Context) {
c.JSON(http.StatusConflict, gin.H{"error": err.Error()})
return
}
h.ensureSafewWatcher(req.Type, req.Config)
c.JSON(http.StatusCreated, ch)
}
@@ -91,6 +92,7 @@ func (h *ChannelHandler) Update(c *gin.Context) {
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
h.ensureSafewWatcher(req.Type, req.Config)
c.JSON(http.StatusOK, gin.H{"ok": true})
}
@@ -160,6 +162,19 @@ func (h *ChannelHandler) respondSafewChats(c *gin.Context, token, q string) {
c.JSON(http.StatusOK, gin.H{"data": list, "total": len(list)})
}
func (h *ChannelHandler) ensureSafewWatcher(typ string, config json.RawMessage) {
if h.chats == nil || typ != "safew" {
return
}
raw := config
ch := &model.Channel{Type: typ, Config: &raw}
tok, err := safewTokenFromChannel(ch)
if err != nil {
return
}
h.chats.Ensure(tok)
}
func safewTokenFromChannel(ch *model.Channel) (string, error) {
if ch.Type != "safew" {
return "", fmt.Errorf("channel is not safew")