changedetection.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package widget
  2. import (
  3. "context"
  4. "html/template"
  5. "time"
  6. "github.com/glanceapp/glance/internal/assets"
  7. "github.com/glanceapp/glance/internal/feed"
  8. )
  9. type ChangeDetections struct {
  10. widgetBase `yaml:",inline"`
  11. ChangeDetections feed.ChangeWatches `yaml:"-"`
  12. Watches []string `yaml:"watches"`
  13. Token OptionalEnvString `yaml:"token"`
  14. Limit int `yaml:"limit"`
  15. CollapseAfter int `yaml:"collapse-after"`
  16. }
  17. func (widget *ChangeDetections) Initialize() error {
  18. widget.withTitle("Changes").withCacheDuration(2 * time.Hour)
  19. if widget.Limit <= 0 {
  20. widget.Limit = 10
  21. }
  22. if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
  23. widget.CollapseAfter = 5
  24. }
  25. return nil
  26. }
  27. func (widget *ChangeDetections) Update(ctx context.Context) {
  28. watches, err := feed.FetchLatestDetectedChanges(widget.Watches, string(widget.Token))
  29. if !widget.canContinueUpdateAfterHandlingErr(err) {
  30. return
  31. }
  32. if len(watches) > widget.Limit {
  33. watches = watches[:widget.Limit]
  34. }
  35. widget.ChangeDetections = watches
  36. }
  37. func (widget *ChangeDetections) Render() template.HTML {
  38. return widget.render(widget, assets.ChangesTemplate)
  39. }