2024-05-08 11:47:25 +02:00
|
|
|
package widget
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"html/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/glanceapp/glance/internal/assets"
|
|
|
|
"github.com/glanceapp/glance/internal/feed"
|
|
|
|
)
|
|
|
|
|
2024-05-30 23:53:59 +02:00
|
|
|
type ChangeDetection struct {
|
2024-05-08 11:47:25 +02:00
|
|
|
widgetBase `yaml:",inline"`
|
2024-05-30 23:53:59 +02:00
|
|
|
ChangeDetections feed.ChangeDetectionWatches `yaml:"-"`
|
|
|
|
WatchUUIDs []string `yaml:"watches"`
|
|
|
|
InstanceURL string `yaml:"instance-url"`
|
|
|
|
Token OptionalEnvString `yaml:"token"`
|
|
|
|
Limit int `yaml:"limit"`
|
|
|
|
CollapseAfter int `yaml:"collapse-after"`
|
2024-05-08 11:47:25 +02:00
|
|
|
}
|
|
|
|
|
2024-05-30 23:53:59 +02:00
|
|
|
func (widget *ChangeDetection) Initialize() error {
|
|
|
|
widget.withTitle("Change Detection").withCacheDuration(1 * time.Hour)
|
2024-05-08 11:47:25 +02:00
|
|
|
|
|
|
|
if widget.Limit <= 0 {
|
|
|
|
widget.Limit = 10
|
|
|
|
}
|
|
|
|
|
|
|
|
if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
|
|
|
|
widget.CollapseAfter = 5
|
|
|
|
}
|
|
|
|
|
2024-05-30 23:53:59 +02:00
|
|
|
if widget.InstanceURL == "" {
|
|
|
|
widget.InstanceURL = "https://www.changedetection.io"
|
|
|
|
}
|
|
|
|
|
2024-05-08 11:47:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-30 23:53:59 +02:00
|
|
|
func (widget *ChangeDetection) Update(ctx context.Context) {
|
|
|
|
if len(widget.WatchUUIDs) == 0 {
|
|
|
|
uuids, err := feed.FetchWatchUUIDsFromChangeDetection(widget.InstanceURL, string(widget.Token))
|
|
|
|
|
|
|
|
if !widget.canContinueUpdateAfterHandlingErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.WatchUUIDs = uuids
|
|
|
|
}
|
|
|
|
|
|
|
|
watches, err := feed.FetchWatchesFromChangeDetection(widget.InstanceURL, widget.WatchUUIDs, string(widget.Token))
|
2024-05-08 11:47:25 +02:00
|
|
|
|
|
|
|
if !widget.canContinueUpdateAfterHandlingErr(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(watches) > widget.Limit {
|
|
|
|
watches = watches[:widget.Limit]
|
|
|
|
}
|
|
|
|
|
|
|
|
widget.ChangeDetections = watches
|
|
|
|
}
|
|
|
|
|
2024-05-30 23:53:59 +02:00
|
|
|
func (widget *ChangeDetection) Render() template.HTML {
|
|
|
|
return widget.render(widget, assets.ChangeDetectionTemplate)
|
2024-05-08 11:47:25 +02:00
|
|
|
}
|