glance/internal/widget/rss.go

84 lines
2.0 KiB
Go
Raw Normal View History

2024-04-27 21:10:24 +02:00
package widget
import (
"context"
"html/template"
"time"
"github.com/glanceapp/glance/internal/assets"
"github.com/glanceapp/glance/internal/feed"
)
type RSS struct {
widgetBase `yaml:",inline"`
FeedRequests []feed.RSSFeedRequest `yaml:"feeds"`
Style string `yaml:"style"`
ThumbnailHeight float64 `yaml:"thumbnail-height"`
CardHeight float64 `yaml:"card-height"`
Items feed.RSSFeedItems `yaml:"-"`
Limit int `yaml:"limit"`
CollapseAfter int `yaml:"collapse-after"`
SingleLineTitles bool `yaml:"single-line-titles"`
NoItemsMessage string `yaml:"-"`
2024-04-27 21:10:24 +02:00
}
func (widget *RSS) Initialize() error {
widget.withTitle("RSS Feed").withCacheDuration(1 * time.Hour)
if widget.Limit <= 0 {
widget.Limit = 25
}
if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
widget.CollapseAfter = 5
}
2024-05-09 07:01:16 +02:00
if widget.ThumbnailHeight < 0 {
widget.ThumbnailHeight = 0
}
if widget.CardHeight < 0 {
widget.CardHeight = 0
}
if widget.Style == "detailed-list" {
2024-05-21 18:28:30 +02:00
for i := range widget.FeedRequests {
widget.FeedRequests[i].IsDetailed = true
2024-05-21 18:28:30 +02:00
}
}
widget.NoItemsMessage = "No items were returned from the feeds."
2024-04-27 21:10:24 +02:00
return nil
}
func (widget *RSS) Update(ctx context.Context) {
items, err := feed.GetItemsFromRSSFeeds(widget.FeedRequests)
if !widget.canContinueUpdateAfterHandlingErr(err) {
return
}
if len(items) > widget.Limit {
items = items[:widget.Limit]
}
widget.Items = items
}
func (widget *RSS) Render() template.HTML {
if widget.Style == "horizontal-cards" {
return widget.render(widget, assets.RSSHorizontalCardsTemplate)
}
if widget.Style == "horizontal-cards-2" {
return widget.render(widget, assets.RSSHorizontalCards2Template)
2024-04-27 21:10:24 +02:00
}
2024-05-21 18:28:30 +02:00
if widget.Style == "detailed-list" {
return widget.render(widget, assets.RSSDetailedListTemplate)
}
2024-04-27 21:10:24 +02:00
return widget.render(widget, assets.RSSListTemplate)
}