rss.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 RSS struct {
  10. widgetBase `yaml:",inline"`
  11. FeedRequests []feed.RSSFeedRequest `yaml:"feeds"`
  12. Style string `yaml:"style"`
  13. ThumbnailHeight float64 `yaml:"thumbnail-height"`
  14. CardHeight float64 `yaml:"card-height"`
  15. Items feed.RSSFeedItems `yaml:"-"`
  16. Limit int `yaml:"limit"`
  17. CollapseAfter int `yaml:"collapse-after"`
  18. }
  19. func (widget *RSS) Initialize() error {
  20. widget.withTitle("RSS Feed").withCacheDuration(1 * time.Hour)
  21. if widget.Limit <= 0 {
  22. widget.Limit = 25
  23. }
  24. if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
  25. widget.CollapseAfter = 5
  26. }
  27. if widget.ThumbnailHeight < 0 {
  28. widget.ThumbnailHeight = 0
  29. }
  30. if widget.CardHeight < 0 {
  31. widget.CardHeight = 0
  32. }
  33. return nil
  34. }
  35. func (widget *RSS) Update(ctx context.Context) {
  36. items, err := feed.GetItemsFromRSSFeeds(widget.FeedRequests)
  37. if !widget.canContinueUpdateAfterHandlingErr(err) {
  38. return
  39. }
  40. if len(items) > widget.Limit {
  41. items = items[:widget.Limit]
  42. }
  43. widget.Items = items
  44. }
  45. func (widget *RSS) Render() template.HTML {
  46. if widget.Style == "horizontal-cards" {
  47. return widget.render(widget, assets.RSSHorizontalCardsTemplate)
  48. }
  49. if widget.Style == "horizontal-cards-2" {
  50. return widget.render(widget, assets.RSSHorizontalCards2Template)
  51. }
  52. return widget.render(widget, assets.RSSListTemplate)
  53. }