changedetection.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 ChangeDetection struct {
  10. widgetBase `yaml:",inline"`
  11. ChangeDetections feed.ChangeDetectionWatches `yaml:"-"`
  12. WatchUUIDs []string `yaml:"watches"`
  13. InstanceURL string `yaml:"instance-url"`
  14. Token OptionalEnvString `yaml:"token"`
  15. Limit int `yaml:"limit"`
  16. CollapseAfter int `yaml:"collapse-after"`
  17. }
  18. func (widget *ChangeDetection) Initialize() error {
  19. widget.withTitle("Change Detection").withCacheDuration(1 * time.Hour)
  20. if widget.Limit <= 0 {
  21. widget.Limit = 10
  22. }
  23. if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
  24. widget.CollapseAfter = 5
  25. }
  26. if widget.InstanceURL == "" {
  27. widget.InstanceURL = "https://www.changedetection.io"
  28. }
  29. return nil
  30. }
  31. func (widget *ChangeDetection) Update(ctx context.Context) {
  32. if len(widget.WatchUUIDs) == 0 {
  33. uuids, err := feed.FetchWatchUUIDsFromChangeDetection(widget.InstanceURL, string(widget.Token))
  34. if !widget.canContinueUpdateAfterHandlingErr(err) {
  35. return
  36. }
  37. widget.WatchUUIDs = uuids
  38. }
  39. watches, err := feed.FetchWatchesFromChangeDetection(widget.InstanceURL, widget.WatchUUIDs, string(widget.Token))
  40. if !widget.canContinueUpdateAfterHandlingErr(err) {
  41. return
  42. }
  43. if len(watches) > widget.Limit {
  44. watches = watches[:widget.Limit]
  45. }
  46. widget.ChangeDetections = watches
  47. }
  48. func (widget *ChangeDetection) Render() template.HTML {
  49. return widget.render(widget, assets.ChangeDetectionTemplate)
  50. }