lobsters.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. InstanceURL string `yaml:"instance-url"`
  13. CustomURL string `yaml:"custom-url"`
  14. Limit int `yaml:"limit"`
  15. CollapseAfter int `yaml:"collapse-after"`
  16. SortBy string `yaml:"sort-by"`
  17. Tags []string `yaml:"tags"`
  18. ShowThumbnails bool `yaml:"-"`
  19. }
  20. func (widget *Lobsters) Initialize() error {
  21. widget.withTitle("Lobsters").withCacheDuration(time.Hour)
  22. if widget.InstanceURL == "" {
  23. widget.withTitleURL("https://lobste.rs")
  24. } else {
  25. widget.withTitleURL(widget.InstanceURL)
  26. }
  27. if widget.SortBy == "" || (widget.SortBy != "hot" && widget.SortBy != "new") {
  28. widget.SortBy = "hot"
  29. }
  30. if widget.Limit <= 0 {
  31. widget.Limit = 15
  32. }
  33. if widget.CollapseAfter == 0 || widget.CollapseAfter < -1 {
  34. widget.CollapseAfter = 5
  35. }
  36. return nil
  37. }
  38. func (widget *Lobsters) Update(ctx context.Context) {
  39. posts, err := feed.FetchLobstersPosts(widget.CustomURL, widget.InstanceURL, widget.SortBy, widget.Tags)
  40. if !widget.canContinueUpdateAfterHandlingErr(err) {
  41. return
  42. }
  43. if widget.Limit < len(posts) {
  44. posts = posts[:widget.Limit]
  45. }
  46. widget.Posts = posts
  47. }
  48. func (widget *Lobsters) Render() template.HTML {
  49. return widget.render(widget, assets.ForumPostsTemplate)
  50. }