Fix notification event toggles and license heartbeat

This commit is contained in:
ShukeBta
2026-06-17 20:35:11 +08:00
parent b128b4d11e
commit a0aeb07f03
12 changed files with 164 additions and 42 deletions
+17 -5
View File
@@ -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
}
+39
View File
@@ -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")
}
}