mirror of
https://github.com/glanceapp/glance.git
synced 2025-08-14 10:15:03 +02:00
feat: add change detection module
This commit is contained in:
51
internal/widget/changedetection.go
Normal file
51
internal/widget/changedetection.go
Normal file
@ -0,0 +1,51 @@
|
||||
package widget
|
||||
|
||||
import (
|
||||
"context"
|
||||
"html/template"
|
||||
"time"
|
||||
|
||||
"github.com/glanceapp/glance/internal/assets"
|
||||
"github.com/glanceapp/glance/internal/feed"
|
||||
)
|
||||
|
||||
type ChangeDetections struct {
|
||||
widgetBase `yaml:",inline"`
|
||||
ChangeDetections feed.ChangeWatches `yaml:"-"`
|
||||
Watches []string `yaml:"watches"`
|
||||
Token OptionalEnvString `yaml:"token"`
|
||||
Limit int `yaml:"limit"`
|
||||
CollapseAfter int `yaml:"collapse-after"`
|
||||
}
|
||||
|
||||
func (widget *ChangeDetections) Initialize() error {
|
||||
widget.withTitle("Changes").withCacheDuration(2 * time.Hour)
|
||||
|
||||
if widget.Limit <= 0 {
|
||||
widget.Limit = 10
|
||||
}
|
||||
|
||||
if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
|
||||
widget.CollapseAfter = 5
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (widget *ChangeDetections) Update(ctx context.Context) {
|
||||
watches, err := feed.FetchLatestDetectedChanges(widget.Watches, string(widget.Token))
|
||||
|
||||
if !widget.canContinueUpdateAfterHandlingErr(err) {
|
||||
return
|
||||
}
|
||||
|
||||
if len(watches) > widget.Limit {
|
||||
watches = watches[:widget.Limit]
|
||||
}
|
||||
|
||||
widget.ChangeDetections = watches
|
||||
}
|
||||
|
||||
func (widget *ChangeDetections) Render() template.HTML {
|
||||
return widget.render(widget, assets.ChangesTemplate)
|
||||
}
|
Reference in New Issue
Block a user