mirror of
https://github.com/truewhile/MeBox.git
synced 2026-10-01 12:06:38 +08:00
Fix notification event toggles and license heartbeat
This commit is contained in:
@@ -113,13 +113,28 @@ func licenseStatusHandler(svc *service.Container) gin.HandlerFunc {
|
||||
if err == nil {
|
||||
deviceID, idErr := ensureLicenseDeviceID(c.Request.Context(), svc, "")
|
||||
if idErr == nil {
|
||||
var upstream licenseServerStatusResp
|
||||
if getErr := client.get(c.Request.Context(), "/api/v1/status/"+url.PathEscape(deviceID), &upstream); getErr == nil && upstream.Valid {
|
||||
applyLicenseStatus(&state, upstream, deviceID)
|
||||
_ = persistLicenseState(c.Request.Context(), svc, state)
|
||||
} else if getErr == nil && !upstream.Valid {
|
||||
state.Valid = false
|
||||
deviceName, _ := svc.Repo.Setting.Get(c.Request.Context(), licenseDeviceNameSetting)
|
||||
if strings.TrimSpace(deviceName) == "" {
|
||||
deviceName = defaultLicenseDeviceName()
|
||||
_ = svc.Repo.Setting.Set(c.Request.Context(), licenseDeviceNameSetting, deviceName)
|
||||
}
|
||||
var signed licenseServerSignedResp
|
||||
if heartbeatErr := client.post(c.Request.Context(), "/api/v1/heartbeat", map[string]any{
|
||||
"fingerprint": deviceID,
|
||||
"instance_id": deviceID,
|
||||
"device_name": deviceName,
|
||||
}, &signed); heartbeatErr == nil && client.verifySigned(&signed) == nil {
|
||||
state = licenseStateFromSigned(signed, deviceID, deviceName)
|
||||
_ = persistLicenseState(c.Request.Context(), svc, state)
|
||||
} else {
|
||||
var upstream licenseServerStatusResp
|
||||
if getErr := client.get(c.Request.Context(), "/api/v1/status/"+url.PathEscape(deviceID), &upstream); getErr == nil && upstream.Valid {
|
||||
applyLicenseStatus(&state, upstream, deviceID)
|
||||
_ = persistLicenseState(c.Request.Context(), svc, state)
|
||||
} else if getErr == nil && !upstream.Valid {
|
||||
state.Valid = false
|
||||
_ = persistLicenseState(c.Request.Context(), svc, state)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -146,10 +161,16 @@ func licenseHeartbeatHandler(svc *service.Container) gin.HandlerFunc {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
deviceName, _ := svc.Repo.Setting.Get(c.Request.Context(), licenseDeviceNameSetting)
|
||||
if strings.TrimSpace(deviceName) == "" {
|
||||
deviceName = defaultLicenseDeviceName()
|
||||
_ = svc.Repo.Setting.Set(c.Request.Context(), licenseDeviceNameSetting, deviceName)
|
||||
}
|
||||
var upstream licenseServerSignedResp
|
||||
if err := client.post(c.Request.Context(), "/api/v1/heartbeat", map[string]any{
|
||||
"fingerprint": deviceID,
|
||||
"instance_id": deviceID,
|
||||
"device_name": deviceName,
|
||||
}, &upstream); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
@@ -158,7 +179,6 @@ func licenseHeartbeatHandler(svc *service.Container) gin.HandlerFunc {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
deviceName, _ := svc.Repo.Setting.Get(c.Request.Context(), licenseDeviceNameSetting)
|
||||
state := licenseStateFromSigned(upstream, deviceID, deviceName)
|
||||
if err := persistLicenseState(c.Request.Context(), svc, state); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
|
||||
@@ -182,9 +182,15 @@ func (s *NotifyChannelService) Test(ctx context.Context, id string) error {
|
||||
return s.dispatchOne(ctx, *row, "MediaStationGo 测试通知", "如果你看到这条消息,说明该通道工作正常。")
|
||||
}
|
||||
|
||||
const (
|
||||
NotifyEventAll = "__all__"
|
||||
NotifyEventNone = "__none__"
|
||||
)
|
||||
|
||||
// Broadcast sends a message to every enabled channel that subscribes to
|
||||
// `event` (an empty Events slice means "all events"). Failures are
|
||||
// logged and never abort the loop.
|
||||
// `event`. Legacy empty Events values mean "all events"; the explicit
|
||||
// NotifyEventNone sentinel means the channel stays enabled but receives no
|
||||
// event push.
|
||||
func (s *NotifyChannelService) Broadcast(ctx context.Context, title, body, event string) {
|
||||
rows, err := s.repo.NotifyChannel.ListEnabled(ctx)
|
||||
if err != nil {
|
||||
@@ -201,10 +207,10 @@ func (s *NotifyChannelService) Broadcast(ctx context.Context, title, body, event
|
||||
}
|
||||
}
|
||||
|
||||
// channelSubscribes returns true when the channel's Events list is
|
||||
// empty (= all events) or contains `event`.
|
||||
// channelSubscribes returns true when the channel's Events list contains the
|
||||
// event, or when the list is the legacy empty/"all events" value.
|
||||
func channelSubscribes(n model.NotifyChannel, event string) bool {
|
||||
if event == "" || n.Events == "" || n.Events == "[]" {
|
||||
if event == "" || n.Events == "" {
|
||||
return true
|
||||
}
|
||||
var ev []string
|
||||
@@ -215,6 +221,12 @@ func channelSubscribes(n model.NotifyChannel, event string) bool {
|
||||
return true
|
||||
}
|
||||
for _, e := range ev {
|
||||
switch e {
|
||||
case NotifyEventNone:
|
||||
return false
|
||||
case NotifyEventAll:
|
||||
return true
|
||||
}
|
||||
if e == event {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ShukeBta/MediaStationGo/internal/model"
|
||||
)
|
||||
|
||||
func TestChannelSubscribesCanDisableAllEvents(t *testing.T) {
|
||||
channel := model.NotifyChannel{Events: `["` + NotifyEventNone + `"]`}
|
||||
|
||||
if channelSubscribes(channel, EventDownloadComplete) {
|
||||
t.Fatal("explicit none sentinel should disable event pushes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannelSubscribesKeepsLegacyEmptyAsAllEvents(t *testing.T) {
|
||||
for _, raw := range []string{"", "[]"} {
|
||||
channel := model.NotifyChannel{Events: raw}
|
||||
if !channelSubscribes(channel, EventDownloadComplete) {
|
||||
t.Fatalf("legacy events %q should still subscribe to all events", raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChannelSubscribesSupportsExplicitAllAndSpecificEvents(t *testing.T) {
|
||||
all := model.NotifyChannel{Events: `["` + NotifyEventAll + `"]`}
|
||||
if !channelSubscribes(all, EventScrapeFailed) {
|
||||
t.Fatal("explicit all sentinel should subscribe to every event")
|
||||
}
|
||||
|
||||
specific := model.NotifyChannel{Events: `["` + EventDownloadComplete + `"]`}
|
||||
if !channelSubscribes(specific, EventDownloadComplete) {
|
||||
t.Fatal("specific event should be subscribed")
|
||||
}
|
||||
if channelSubscribes(specific, EventScrapeFailed) {
|
||||
t.Fatal("unlisted event should not be subscribed")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user