node build fixed
This commit is contained in:
18
seanime-2.9.10/internal/notifier/beeep_test.go
Normal file
18
seanime-2.9.10/internal/notifier/beeep_test.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"github.com/gen2brain/beeep"
|
||||
"github.com/stretchr/testify/require"
|
||||
"path/filepath"
|
||||
"seanime/internal/test_utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBeeep(t *testing.T) {
|
||||
test_utils.SetTwoLevelDeep()
|
||||
test_utils.InitTestProvider(t)
|
||||
|
||||
err := beeep.Notify("Seanime", "Downloaded 1 episode", filepath.Join(test_utils.ConfigData.Path.DataDir, "logo.png"))
|
||||
require.NoError(t, err)
|
||||
|
||||
}
|
||||
27
seanime-2.9.10/internal/notifier/gotoast_test.go
Normal file
27
seanime-2.9.10/internal/notifier/gotoast_test.go
Normal file
@@ -0,0 +1,27 @@
|
||||
//go:build windows
|
||||
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"github.com/go-toast/toast"
|
||||
"path/filepath"
|
||||
"seanime/internal/test_utils"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGoToast(t *testing.T) {
|
||||
test_utils.SetTwoLevelDeep()
|
||||
test_utils.InitTestProvider(t)
|
||||
|
||||
notification := toast.Notification{
|
||||
AppID: "Seanime",
|
||||
Title: "Seanime",
|
||||
Icon: filepath.Join(test_utils.ConfigData.Path.DataDir, "logo.png"),
|
||||
Message: "Auto Downloader has downloaded 1 episode",
|
||||
}
|
||||
err := notification.Push()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
||||
74
seanime-2.9.10/internal/notifier/notifier.go
Normal file
74
seanime-2.9.10/internal/notifier/notifier.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"github.com/rs/zerolog"
|
||||
"github.com/samber/mo"
|
||||
"path/filepath"
|
||||
"seanime/internal/database/models"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type (
|
||||
Notifier struct {
|
||||
dataDir mo.Option[string]
|
||||
settings mo.Option[*models.NotificationSettings]
|
||||
mu sync.Mutex
|
||||
logoPath string
|
||||
logger mo.Option[*zerolog.Logger]
|
||||
}
|
||||
|
||||
Notification string
|
||||
)
|
||||
|
||||
const (
|
||||
AutoDownloader Notification = "Auto Downloader"
|
||||
AutoScanner Notification = "Auto Scanner"
|
||||
Debrid Notification = "Debrid"
|
||||
)
|
||||
|
||||
var GlobalNotifier = NewNotifier()
|
||||
|
||||
func init() {
|
||||
GlobalNotifier = NewNotifier()
|
||||
}
|
||||
|
||||
func NewNotifier() *Notifier {
|
||||
return &Notifier{
|
||||
dataDir: mo.None[string](),
|
||||
settings: mo.None[*models.NotificationSettings](),
|
||||
mu: sync.Mutex{},
|
||||
logger: mo.None[*zerolog.Logger](),
|
||||
}
|
||||
}
|
||||
|
||||
func (n *Notifier) SetSettings(datadir string, settings *models.NotificationSettings, logger *zerolog.Logger) {
|
||||
if datadir == "" || settings == nil {
|
||||
return
|
||||
}
|
||||
|
||||
n.mu.Lock()
|
||||
n.dataDir = mo.Some(datadir)
|
||||
n.settings = mo.Some(settings)
|
||||
n.logoPath = filepath.Join(datadir, "logo.png")
|
||||
n.logger = mo.Some(logger)
|
||||
n.mu.Unlock()
|
||||
}
|
||||
|
||||
func (n *Notifier) canProceed(id Notification) bool {
|
||||
if !n.dataDir.IsPresent() || !n.settings.IsPresent() {
|
||||
return false
|
||||
}
|
||||
|
||||
if n.settings.MustGet().DisableNotifications {
|
||||
return false
|
||||
}
|
||||
|
||||
switch id {
|
||||
case AutoDownloader:
|
||||
return !n.settings.MustGet().DisableAutoDownloaderNotifications
|
||||
case AutoScanner:
|
||||
return !n.settings.MustGet().DisableAutoScannerNotifications
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
32
seanime-2.9.10/internal/notifier/notifier_test.go
Normal file
32
seanime-2.9.10/internal/notifier/notifier_test.go
Normal file
@@ -0,0 +1,32 @@
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"seanime/internal/database/models"
|
||||
"seanime/internal/test_utils"
|
||||
"seanime/internal/util"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNotifier(t *testing.T) {
|
||||
test_utils.SetTwoLevelDeep()
|
||||
test_utils.InitTestProvider(t)
|
||||
|
||||
GlobalNotifier = NewNotifier()
|
||||
|
||||
GlobalNotifier.SetSettings(test_utils.ConfigData.Path.DataDir, &models.NotificationSettings{}, util.NewLogger())
|
||||
|
||||
GlobalNotifier.Notify(
|
||||
AutoDownloader,
|
||||
fmt.Sprintf("%d %s %s been downloaded or added to the queue.", 1, util.Pluralize(1, "episode", "episodes"), util.Pluralize(1, "has", "have")),
|
||||
)
|
||||
|
||||
GlobalNotifier.Notify(
|
||||
AutoScanner,
|
||||
fmt.Sprintf("%d %s %s been downloaded or added to the queue.", 1, util.Pluralize(1, "episode", "episodes"), util.Pluralize(1, "has", "have")),
|
||||
)
|
||||
|
||||
time.Sleep(1 * time.Second)
|
||||
|
||||
}
|
||||
39
seanime-2.9.10/internal/notifier/notify_unix.go
Normal file
39
seanime-2.9.10/internal/notifier/notify_unix.go
Normal file
@@ -0,0 +1,39 @@
|
||||
//go:build !windows
|
||||
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gen2brain/beeep"
|
||||
"seanime/internal/util"
|
||||
)
|
||||
|
||||
// Notify sends a notification to the user.
|
||||
// This is run in a goroutine.
|
||||
func (n *Notifier) Notify(id Notification, message string) {
|
||||
go func() {
|
||||
defer util.HandlePanicThen(func() {})
|
||||
|
||||
n.mu.Lock()
|
||||
defer n.mu.Unlock()
|
||||
|
||||
if !n.canProceed(id) {
|
||||
return
|
||||
}
|
||||
|
||||
err := beeep.Notify(
|
||||
fmt.Sprintf("Seanime: %s", id),
|
||||
message,
|
||||
n.logoPath,
|
||||
)
|
||||
if err != nil {
|
||||
if n.logger.IsPresent() {
|
||||
n.logger.MustGet().Trace().Msgf("notifier: Failed to push notification: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if n.logger.IsPresent() {
|
||||
n.logger.MustGet().Trace().Msgf("notifier: Pushed notification: %v", id)
|
||||
}
|
||||
}()
|
||||
}
|
||||
40
seanime-2.9.10/internal/notifier/notify_windows.go
Normal file
40
seanime-2.9.10/internal/notifier/notify_windows.go
Normal file
@@ -0,0 +1,40 @@
|
||||
//go:build windows
|
||||
|
||||
package notifier
|
||||
|
||||
import (
|
||||
"github.com/go-toast/toast"
|
||||
"seanime/internal/util"
|
||||
)
|
||||
|
||||
// Notify sends a notification to the user.
|
||||
// This is run in a goroutine.
|
||||
func (n *Notifier) Notify(id Notification, message string) {
|
||||
go func() {
|
||||
defer util.HandlePanicInModuleThen("notifier/Notify", func() {})
|
||||
|
||||
n.mu.Lock()
|
||||
defer n.mu.Unlock()
|
||||
|
||||
if !n.canProceed(id) {
|
||||
return
|
||||
}
|
||||
|
||||
notification := toast.Notification{
|
||||
AppID: "Seanime",
|
||||
Title: string(id),
|
||||
Message: message,
|
||||
Icon: n.logoPath,
|
||||
}
|
||||
|
||||
err := notification.Push()
|
||||
if err != nil {
|
||||
if n.logger.IsPresent() {
|
||||
n.logger.MustGet().Trace().Msgf("notifier: Failed to push notification: %v", err)
|
||||
}
|
||||
}
|
||||
if n.logger.IsPresent() {
|
||||
n.logger.MustGet().Trace().Msgf("notifier: Pushed notification: %v", id)
|
||||
}
|
||||
}()
|
||||
}
|
||||
Reference in New Issue
Block a user