extension.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. FallbackContentType string `yaml:"fallback-content-type"`
  15. Parameters map[string]string `yaml:"parameters"`
  16. AllowHtml bool `yaml:"allow-potentially-dangerous-html"`
  17. Extension feed.Extension `yaml:"-"`
  18. cachedHTML template.HTML `yaml:"-"`
  19. }
  20. func (widget *Extension) Initialize() error {
  21. widget.withTitle("Extension").withCacheDuration(time.Minute * 30)
  22. if widget.URL == "" {
  23. return errors.New("no extension URL specified")
  24. }
  25. _, err := url.Parse(widget.URL)
  26. if err != nil {
  27. return err
  28. }
  29. return nil
  30. }
  31. func (widget *Extension) Update(ctx context.Context) {
  32. extension, err := feed.FetchExtension(feed.ExtensionRequestOptions{
  33. URL: widget.URL,
  34. FallbackContentType: widget.FallbackContentType,
  35. Parameters: widget.Parameters,
  36. AllowHtml: widget.AllowHtml,
  37. })
  38. widget.canContinueUpdateAfterHandlingErr(err)
  39. widget.Extension = extension
  40. if extension.Title != "" {
  41. widget.Title = extension.Title
  42. }
  43. widget.cachedHTML = widget.render(widget, assets.ExtensionTemplate)
  44. }
  45. func (widget *Extension) Render() template.HTML {
  46. return widget.cachedHTML
  47. }