rss.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. NoItemsMessage string `yaml:"-"`
  19. }
  20. func (widget *RSS) Initialize() error {
  21. widget.withTitle("RSS Feed").withCacheDuration(1 * time.Hour)
  22. if widget.Limit <= 0 {
  23. widget.Limit = 25
  24. }
  25. if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
  26. widget.CollapseAfter = 5
  27. }
  28. if widget.ThumbnailHeight < 0 {
  29. widget.ThumbnailHeight = 0
  30. }
  31. if widget.CardHeight < 0 {
  32. widget.CardHeight = 0
  33. }
  34. if widget.Style == "detailed-list" {
  35. for i := range widget.FeedRequests {
  36. widget.FeedRequests[i].IsDetailed = true
  37. }
  38. }
  39. widget.NoItemsMessage = "No items were returned from the feeds."
  40. return nil
  41. }
  42. func (widget *RSS) Update(ctx context.Context) {
  43. items, err := feed.GetItemsFromRSSFeeds(widget.FeedRequests)
  44. if !widget.canContinueUpdateAfterHandlingErr(err) {
  45. return
  46. }
  47. if len(items) > widget.Limit {
  48. items = items[:widget.Limit]
  49. }
  50. widget.Items = items
  51. }
  52. func (widget *RSS) Render() template.HTML {
  53. if widget.Style == "horizontal-cards" {
  54. return widget.render(widget, assets.RSSHorizontalCardsTemplate)
  55. }
  56. if widget.Style == "horizontal-cards-2" {
  57. return widget.render(widget, assets.RSSHorizontalCards2Template)
  58. }
  59. if widget.Style == "detailed-list" {
  60. return widget.render(widget, assets.RSSDetailedListTemplate)
  61. }
  62. return widget.render(widget, assets.RSSListTemplate)
  63. }