rss.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. package v1
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "strconv"
  9. "strings"
  10. "time"
  11. "github.com/gorilla/feeds"
  12. "github.com/labstack/echo/v4"
  13. "github.com/pkg/errors"
  14. "github.com/yuin/goldmark"
  15. "github.com/usememos/memos/common/util"
  16. "github.com/usememos/memos/store"
  17. )
  18. const maxRSSItemCount = 100
  19. const maxRSSItemTitleLength = 100
  20. func (s *APIV1Service) registerRSSRoutes(g *echo.Group) {
  21. g.GET("/explore/rss.xml", s.GetExploreRSS)
  22. g.GET("/u/:id/rss.xml", s.GetUserRSS)
  23. }
  24. // GetExploreRSS godoc
  25. //
  26. // @Summary Get RSS
  27. // @Tags rss
  28. // @Produce xml
  29. // @Success 200 {object} nil "RSS"
  30. // @Failure 500 {object} nil "Failed to get system customized profile | Failed to find memo list | Failed to generate rss"
  31. // @Router /explore/rss.xml [GET]
  32. func (s *APIV1Service) GetExploreRSS(c echo.Context) error {
  33. ctx := c.Request().Context()
  34. systemCustomizedProfile, err := s.getSystemCustomizedProfile(ctx)
  35. if err != nil {
  36. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
  37. }
  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, systemCustomizedProfile)
  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. // GetUserRSS godoc
  56. //
  57. // @Summary Get RSS for a user
  58. // @Tags rss
  59. // @Produce xml
  60. // @Param id path int true "User ID"
  61. // @Success 200 {object} nil "RSS"
  62. // @Failure 400 {object} nil "User id is not a number"
  63. // @Failure 500 {object} nil "Failed to get system customized profile | Failed to find memo list | Failed to generate rss"
  64. // @Router /u/{id}/rss.xml [GET]
  65. func (s *APIV1Service) GetUserRSS(c echo.Context) error {
  66. ctx := c.Request().Context()
  67. id, err := util.ConvertStringToInt32(c.Param("id"))
  68. if err != nil {
  69. return echo.NewHTTPError(http.StatusBadRequest, "User id is not a number").SetInternal(err)
  70. }
  71. systemCustomizedProfile, err := s.getSystemCustomizedProfile(ctx)
  72. if err != nil {
  73. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
  74. }
  75. normalStatus := store.Normal
  76. memoFind := store.FindMemo{
  77. CreatorID: &id,
  78. RowStatus: &normalStatus,
  79. VisibilityList: []store.Visibility{store.Public},
  80. }
  81. memoList, err := s.Store.ListMemos(ctx, &memoFind)
  82. if err != nil {
  83. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
  84. }
  85. baseURL := c.Scheme() + "://" + c.Request().Host
  86. rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL, systemCustomizedProfile)
  87. if err != nil {
  88. return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
  89. }
  90. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
  91. return c.String(http.StatusOK, rss)
  92. }
  93. func (s *APIV1Service) generateRSSFromMemoList(ctx context.Context, memoList []*store.Memo, baseURL string, profile *CustomizedProfile) (string, error) {
  94. feed := &feeds.Feed{
  95. Title: profile.Name,
  96. Link: &feeds.Link{Href: baseURL},
  97. Description: profile.Description,
  98. Created: time.Now(),
  99. }
  100. var itemCountLimit = util.Min(len(memoList), maxRSSItemCount)
  101. feed.Items = make([]*feeds.Item, itemCountLimit)
  102. for i := 0; i < itemCountLimit; i++ {
  103. memo := memoList[i]
  104. feed.Items[i] = &feeds.Item{
  105. Title: getRSSItemTitle(memo.Content),
  106. Link: &feeds.Link{Href: baseURL + "/m/" + fmt.Sprintf("%d", memo.ID)},
  107. Description: getRSSItemDescription(memo.Content),
  108. Created: time.Unix(memo.CreatedTs, 0),
  109. Enclosure: &feeds.Enclosure{Url: baseURL + "/m/" + fmt.Sprintf("%d", memo.ID) + "/image"},
  110. }
  111. if len(memo.ResourceIDList) > 0 {
  112. resourceID := memo.ResourceIDList[0]
  113. resource, err := s.Store.GetResource(ctx, &store.FindResource{
  114. ID: &resourceID,
  115. })
  116. if err != nil {
  117. return "", err
  118. }
  119. if resource == nil {
  120. return "", errors.Errorf("Resource not found: %d", resourceID)
  121. }
  122. enclosure := feeds.Enclosure{}
  123. if resource.ExternalLink != "" {
  124. enclosure.Url = resource.ExternalLink
  125. } else {
  126. enclosure.Url = baseURL + "/o/r/" + fmt.Sprintf("%d", resource.ID)
  127. }
  128. enclosure.Length = strconv.Itoa(int(resource.Size))
  129. enclosure.Type = resource.Type
  130. feed.Items[i].Enclosure = &enclosure
  131. }
  132. }
  133. rss, err := feed.ToRss()
  134. if err != nil {
  135. return "", err
  136. }
  137. return rss, nil
  138. }
  139. func (s *APIV1Service) getSystemCustomizedProfile(ctx context.Context) (*CustomizedProfile, error) {
  140. systemSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
  141. Name: SystemSettingCustomizedProfileName.String(),
  142. })
  143. if err != nil {
  144. return nil, err
  145. }
  146. customizedProfile := &CustomizedProfile{
  147. Name: "memos",
  148. LogoURL: "",
  149. Description: "",
  150. Locale: "en",
  151. Appearance: "system",
  152. ExternalURL: "",
  153. }
  154. if systemSetting != nil {
  155. if err := json.Unmarshal([]byte(systemSetting.Value), customizedProfile); err != nil {
  156. return nil, err
  157. }
  158. }
  159. return customizedProfile, nil
  160. }
  161. func getRSSItemTitle(content string) string {
  162. var title string
  163. if isTitleDefined(content) {
  164. title = strings.Split(content, "\n")[0][2:]
  165. } else {
  166. title = strings.Split(content, "\n")[0]
  167. var titleLengthLimit = util.Min(len(title), maxRSSItemTitleLength)
  168. if titleLengthLimit < len(title) {
  169. title = title[:titleLengthLimit] + "..."
  170. }
  171. }
  172. return title
  173. }
  174. func getRSSItemDescription(content string) string {
  175. var description string
  176. if isTitleDefined(content) {
  177. var firstLineEnd = strings.Index(content, "\n")
  178. description = strings.Trim(content[firstLineEnd+1:], " ")
  179. } else {
  180. description = content
  181. }
  182. // TODO: use our `./plugin/gomark` parser to handle markdown-like content.
  183. var buf bytes.Buffer
  184. if err := goldmark.Convert([]byte(description), &buf); err != nil {
  185. panic(err)
  186. }
  187. return buf.String()
  188. }
  189. func isTitleDefined(content string) bool {
  190. return strings.HasPrefix(content, "# ")
  191. }