lobsters.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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 Lobsters struct {
  10. widgetBase `yaml:",inline"`
  11. Posts feed.ForumPosts `yaml:"-"`
  12. Limit int `yaml:"limit"`
  13. CollapseAfter int `yaml:"collapse-after"`
  14. SortBy string `yaml:"sort-by"`
  15. Tags []string `yaml:"tags"`
  16. ShowThumbnails bool `yaml:"-"`
  17. }
  18. func (widget *Lobsters) Initialize() error {
  19. widget.withTitle("Lobsters").withCacheDuration(30 * time.Minute)
  20. if widget.SortBy == "" || (widget.SortBy != "hot" && widget.SortBy != "new") {
  21. widget.SortBy = "hot"
  22. }
  23. if widget.Limit <= 0 {
  24. widget.Limit = 15
  25. }
  26. if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
  27. widget.CollapseAfter = 5
  28. }
  29. return nil
  30. }
  31. func (widget *Lobsters) Update(ctx context.Context) {
  32. posts, err := feed.FetchLobstersPosts(widget.SortBy, widget.Tags)
  33. if !widget.canContinueUpdateAfterHandlingErr(err) {
  34. return
  35. }
  36. if widget.Limit < len(posts) {
  37. posts = posts[:widget.Limit]
  38. }
  39. widget.Posts = posts
  40. }
  41. func (widget *Lobsters) Render() template.HTML {
  42. return widget.render(widget, assets.ForumPostsTemplate)
  43. }