rss.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package rss
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "github.com/gorilla/feeds"
  10. "github.com/labstack/echo/v4"
  11. "github.com/usememos/gomark"
  12. "github.com/usememos/gomark/ast"
  13. "github.com/usememos/gomark/renderer"
  14. storepb "github.com/usememos/memos/proto/gen/store"
  15. "github.com/usememos/memos/server/profile"
  16. "github.com/usememos/memos/store"
  17. )
  18. const (
  19. maxRSSItemCount = 100
  20. maxRSSItemTitleLength = 128
  21. )
  22. type RSSService struct {
  23. Profile *profile.Profile
  24. Store *store.Store
  25. }
  26. func NewRSSService(profile *profile.Profile, store *store.Store) *RSSService {
  27. return &RSSService{
  28. Profile: profile,
  29. Store: store,
  30. }
  31. }
  32. func (s *RSSService) RegisterRoutes(g *echo.Group) {
  33. g.GET("/explore/rss.xml", s.GetExploreRSS)
  34. g.GET("/u/:username/rss.xml", s.GetUserRSS)
  35. }
  36. func (s *RSSService) GetExploreRSS(c echo.Context) error {
  37. ctx := c.Request().Context()
  38. normalStatus := store.Normal
  39. memoFind := store.FindMemo{
  40. RowStatus: &normalStatus,
  41. VisibilityList: []store.Visibility{store.Public},
  42. }
  43. memoList, err := s.Store.ListMemos(ctx, &memoFind)
  44. if err != nil {
  45. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
  46. }
  47. baseURL := c.Scheme() + "://" + c.Request().Host
  48. rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL)
  49. if err != nil {
  50. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
  51. }
  52. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
  53. return c.String(http.StatusOK, rss)
  54. }
  55. func (s *RSSService) GetUserRSS(c echo.Context) error {
  56. ctx := c.Request().Context()
  57. username := c.Param("username")
  58. user, err := s.Store.GetUser(ctx, &store.FindUser{
  59. Username: &username,
  60. })
  61. if err != nil {
  62. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
  63. }
  64. if user == nil {
  65. return echo.NewHTTPError(http.StatusNotFound, "User not found")
  66. }
  67. normalStatus := store.Normal
  68. memoFind := store.FindMemo{
  69. CreatorID: &user.ID,
  70. RowStatus: &normalStatus,
  71. VisibilityList: []store.Visibility{store.Public},
  72. }
  73. memoList, err := s.Store.ListMemos(ctx, &memoFind)
  74. if err != nil {
  75. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
  76. }
  77. baseURL := c.Scheme() + "://" + c.Request().Host
  78. rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL)
  79. if err != nil {
  80. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
  81. }
  82. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
  83. return c.String(http.StatusOK, rss)
  84. }
  85. func (s *RSSService) generateRSSFromMemoList(ctx context.Context, memoList []*store.Memo, baseURL string) (string, error) {
  86. feed := &feeds.Feed{
  87. Title: "Memos",
  88. Link: &feeds.Link{Href: baseURL},
  89. Description: "An open source, lightweight note-taking service. Easily capture and share your great thoughts.",
  90. Created: time.Now(),
  91. }
  92. var itemCountLimit = min(len(memoList), maxRSSItemCount)
  93. feed.Items = make([]*feeds.Item, itemCountLimit)
  94. for i := 0; i < itemCountLimit; i++ {
  95. memo := memoList[i]
  96. description, err := getRSSItemDescription(memo.Content)
  97. if err != nil {
  98. return "", err
  99. }
  100. feed.Items[i] = &feeds.Item{
  101. Title: getRSSItemTitle(memo.Content),
  102. Link: &feeds.Link{Href: baseURL + "/m/" + memo.UID},
  103. Description: description,
  104. Created: time.Unix(memo.CreatedTs, 0),
  105. }
  106. resources, err := s.Store.ListResources(ctx, &store.FindResource{
  107. MemoID: &memo.ID,
  108. })
  109. if err != nil {
  110. return "", err
  111. }
  112. if len(resources) > 0 {
  113. resource := resources[0]
  114. enclosure := feeds.Enclosure{}
  115. if resource.StorageType == storepb.ResourceStorageType_EXTERNAL || resource.StorageType == storepb.ResourceStorageType_S3 {
  116. enclosure.Url = resource.Reference
  117. } else {
  118. enclosure.Url = fmt.Sprintf("%s/file/resources/%d/%s", baseURL, resource.ID, resource.Filename)
  119. }
  120. enclosure.Length = strconv.Itoa(int(resource.Size))
  121. enclosure.Type = resource.Type
  122. feed.Items[i].Enclosure = &enclosure
  123. }
  124. }
  125. rss, err := feed.ToRss()
  126. if err != nil {
  127. return "", err
  128. }
  129. return rss, nil
  130. }
  131. func getRSSItemTitle(content string) string {
  132. nodes, _ := gomark.Parse(content)
  133. if len(nodes) > 0 {
  134. firstNode := nodes[0]
  135. title := renderer.NewStringRenderer().Render([]ast.Node{firstNode})
  136. return title
  137. }
  138. title := strings.Split(content, "\n")[0]
  139. var titleLengthLimit = min(len(title), maxRSSItemTitleLength)
  140. if titleLengthLimit < len(title) {
  141. title = title[:titleLengthLimit] + "..."
  142. }
  143. return title
  144. }
  145. func getRSSItemDescription(content string) (string, error) {
  146. nodes, err := gomark.Parse(content)
  147. if err != nil {
  148. return "", err
  149. }
  150. result := renderer.NewHTMLRenderer().Render(nodes)
  151. return result, nil
  152. }