extension.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package widget
  2. import (
  3. "context"
  4. "errors"
  5. "html/template"
  6. "net/url"
  7. "time"
  8. "github.com/glanceapp/glance/internal/assets"
  9. "github.com/glanceapp/glance/internal/feed"
  10. )
  11. type Extension struct {
  12. widgetBase `yaml:",inline"`
  13. URL string `yaml:"url"`
  14. Parameters map[string]string `yaml:"parameters"`
  15. AllowHtml bool `yaml:"allow-potentially-dangerous-html"`
  16. Extension feed.Extension `yaml:"-"`
  17. cachedHTML template.HTML `yaml:"-"`
  18. }
  19. func (widget *Extension) Initialize() error {
  20. widget.withTitle("Extension").withCacheDuration(time.Minute * 30)
  21. if widget.URL == "" {
  22. return errors.New("no extension URL specified")
  23. }
  24. _, err := url.Parse(widget.URL)
  25. if err != nil {
  26. return err
  27. }
  28. return nil
  29. }
  30. func (widget *Extension) Update(ctx context.Context) {
  31. extension, err := feed.FetchExtension(feed.ExtensionRequestOptions{
  32. URL: widget.URL,
  33. Parameters: widget.Parameters,
  34. AllowHtml: widget.AllowHtml,
  35. })
  36. widget.canContinueUpdateAfterHandlingErr(err)
  37. widget.Extension = extension
  38. if extension.Title != "" {
  39. widget.Title = extension.Title
  40. }
  41. widget.cachedHTML = widget.render(widget, assets.ExtensionTemplate)
  42. }
  43. func (widget *Extension) Render() template.HTML {
  44. return widget.cachedHTML
  45. }