rss.go 2.1 KB

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