Skip to content

Commit

Permalink
feat(notify): add notification for login failed
Browse files Browse the repository at this point in the history
  • Loading branch information
Darkness4 committed Nov 11, 2023
1 parent 225fd68 commit 6c01468
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 0 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ notifier:
# message: <empty>
# priority: 10

## LoginFailed happens when the cookies refresh failed.
## Available fields:
## - Error
loginFailed:
enabled: true
# title: "login failed"
# message: "{{ .Error }}"
# priority: 10

## Panicked is sent when a critical error happens.
## When this happens, it is recommended to contact the developer and open an issue.
## Available fields:
Expand Down
3 changes: 3 additions & 0 deletions cmd/watch/watch_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ func handleConfig(ctx context.Context, config *Config) {
if params.CookiesRefreshDuration != 0 && params.CookiesFile != "" {
log.Info().Dur("duration", params.CookiesRefreshDuration).Msg("will refresh cookies")
if err := fc2.Login(ctx, fc2.WithHTTPClient(client)); err != nil {
if err := notifier.NotifyLoginFailed(ctx, err); err != nil {
log.Err(err).Msg("notify failed")
}
log.Err(err).
Msg("failed to login to id.fc2.com, we will try again, but you should extract new cookies")
}
Expand Down
9 changes: 9 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ notifier:
# message: <empty>
# priority: 10

## LoginFailed happens when the cookies refresh failed.
## Available fields:
## - Error
loginFailed:
enabled: true
# title: "login failed"
# message: "{{ .Error }}"
# priority: 10

## Panicked is sent when a critical error happens.
## When this happens, it is recommended to contact the developer and open an issue.
## Available fields:
Expand Down
4 changes: 4 additions & 0 deletions fc2/fc2_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/Darkness4/fc2-live-dl-go/notify/notifier"
"github.com/rs/zerolog/log"
)

Expand Down Expand Up @@ -140,6 +141,9 @@ func LoginLoop(
select {
case <-ticker.C:
if err := Login(ctx, opts...); err != nil {
if err := notifier.NotifyLoginFailed(ctx, err); err != nil {
log.Err(err).Msg("notify failed")
}
log.Err(err).
Msg("failed to login to id.fc2.com, we will try again, but you should extract new cookies")
}
Expand Down
4 changes: 4 additions & 0 deletions notify/notifier/notifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func NotifyConfigReloaded(ctx context.Context) error {
return Notifier.NotifyConfigReloaded(ctx)
}

func NotifyLoginFailed(ctx context.Context, capture error) error {
return Notifier.NotifyLoginFailed(ctx, capture)
}

func NotifyPanicked(ctx context.Context, capture any) error {
return Notifier.NotifyPanicked(ctx, capture)
}
Expand Down
47 changes: 47 additions & 0 deletions notify/notify_formated.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

type NotificationFormats struct {
ConfigReloaded NotificationFormat `yaml:"configReloaded,omitempty"`
LoginFailed NotificationFormat `yaml:"loginFailed,omitempty"`
Panicked NotificationFormat `yaml:"panicked,omitempty"`
Idle NotificationFormat `yaml:"idle,omitempty"`
PreparingFiles NotificationFormat `yaml:"preparingFiles,omitempty"`
Expand All @@ -29,6 +30,7 @@ type NotificationFormat struct {

type NotificationTemplates struct {
ConfigReloaded NotificationTemplate
LoginFailed NotificationTemplate
Panicked NotificationTemplate
Idle NotificationTemplate
PreparingFiles NotificationTemplate
Expand All @@ -51,6 +53,12 @@ var DefaultNotificationFormats = NotificationFormats{
Message: "",
Priority: 10,
},
LoginFailed: NotificationFormat{
Enabled: ptr.Ref(true),
Title: "login failed",
Message: "{{ .Error }}",
Priority: 10,
},
Panicked: NotificationFormat{
Enabled: ptr.Ref(true),
Title: "panicked",
Expand Down Expand Up @@ -116,6 +124,7 @@ func (old *NotificationFormat) applyNotificationFormatDefault(
func applyNotificationFormatsDefault(new NotificationFormats) NotificationFormats {
formats := DefaultNotificationFormats
formats.ConfigReloaded.applyNotificationFormatDefault(new.ConfigReloaded)
formats.LoginFailed.applyNotificationFormatDefault(new.LoginFailed)
formats.Panicked.applyNotificationFormatDefault(new.Panicked)
formats.Idle.applyNotificationFormatDefault(new.Idle)
formats.PreparingFiles.applyNotificationFormatDefault(new.PreparingFiles)
Expand All @@ -137,6 +146,7 @@ func initializeTemplate(format NotificationFormat) NotificationTemplate {
func initializeTemplates(formats NotificationFormats) NotificationTemplates {
return NotificationTemplates{
ConfigReloaded: initializeTemplate(formats.ConfigReloaded),
LoginFailed: initializeTemplate(formats.LoginFailed),
Panicked: initializeTemplate(formats.Panicked),
Idle: initializeTemplate(formats.Idle),
PreparingFiles: initializeTemplate(formats.PreparingFiles),
Expand All @@ -151,6 +161,7 @@ func initializeTemplates(formats NotificationFormats) NotificationTemplates {
type FormatedNotifier interface {
BaseNotifier
NotifyConfigReloaded(ctx context.Context) error
NotifyLoginFailed(ctx context.Context, capture error) error
NotifyPanicked(ctx context.Context, capture any) error
NotifyIdle(ctx context.Context, channelID string, labels map[string]string) error
NotifyPreparingFiles(
Expand Down Expand Up @@ -424,6 +435,42 @@ func (n *formatedNotifier) NotifyIdle(
)
}

func (n *formatedNotifier) NotifyLoginFailed(ctx context.Context, capture error) error {
if n.NotificationFormats.LoginFailed.Enabled == nil ||
(n.NotificationFormats.LoginFailed.Enabled != nil &&
!(*n.NotificationFormats.LoginFailed.Enabled)) {
return nil
}
var titleSB strings.Builder
var messageSB strings.Builder
if err := n.NotificationTemplates.LoginFailed.TitleTemplate.Execute(
&titleSB,
struct {
Error error
}{
Error: capture,
},
); err != nil {
return err
}
if err := n.NotificationTemplates.LoginFailed.MessageTemplate.Execute(
&messageSB,
struct {
Error any
}{
Error: capture,
},
); err != nil {
return err
}
return n.Notify(
ctx,
titleSB.String(),
messageSB.String(),
n.NotificationFormats.LoginFailed.Priority,
)
}

func (n *formatedNotifier) NotifyPanicked(ctx context.Context, capture any) error {
if n.NotificationFormats.Panicked.Enabled == nil ||
(n.NotificationFormats.Panicked.Enabled != nil &&
Expand Down

0 comments on commit 6c01468

Please sign in to comment.