2024-01-29 21:04:35 +08:00
|
|
|
package rss
|
2022-10-26 20:13:02 +08:00
|
|
|
|
|
|
|
import (
|
2023-02-09 21:17:15 +08:00
|
|
|
"context"
|
2022-10-26 20:13:02 +08:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-02-09 21:17:15 +08:00
|
|
|
"strings"
|
2022-10-26 20:13:02 +08:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/feeds"
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-01-29 21:04:35 +08:00
|
|
|
"github.com/usememos/gomark"
|
2024-01-29 19:14:46 +08:00
|
|
|
"github.com/usememos/gomark/ast"
|
|
|
|
"github.com/usememos/gomark/renderer"
|
2024-01-29 21:08:30 +08:00
|
|
|
|
2023-10-26 09:02:50 +08:00
|
|
|
"github.com/usememos/memos/internal/util"
|
2024-01-29 21:04:35 +08:00
|
|
|
"github.com/usememos/memos/server/profile"
|
2023-05-25 21:50:37 +08:00
|
|
|
"github.com/usememos/memos/store"
|
2022-10-26 20:13:02 +08:00
|
|
|
)
|
|
|
|
|
2023-12-23 17:58:49 +08:00
|
|
|
const (
|
|
|
|
maxRSSItemCount = 100
|
|
|
|
maxRSSItemTitleLength = 128
|
|
|
|
)
|
2023-07-06 22:53:38 +08:00
|
|
|
|
2024-01-29 21:04:35 +08:00
|
|
|
type RSSService struct {
|
|
|
|
Profile *profile.Profile
|
|
|
|
Store *store.Store
|
2023-08-09 21:53:06 +08:00
|
|
|
}
|
|
|
|
|
2024-01-29 21:04:35 +08:00
|
|
|
func NewRSSService(profile *profile.Profile, store *store.Store) *RSSService {
|
|
|
|
return &RSSService{
|
|
|
|
Profile: profile,
|
|
|
|
Store: store,
|
2023-08-09 21:53:06 +08:00
|
|
|
}
|
2024-01-29 21:04:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *RSSService) RegisterRoutes(g *echo.Group) {
|
|
|
|
g.GET("/explore/rss.xml", s.GetExploreRSS)
|
|
|
|
g.GET("/u/:username/rss.xml", s.GetUserRSS)
|
|
|
|
}
|
2023-02-09 21:17:15 +08:00
|
|
|
|
2024-01-29 21:04:35 +08:00
|
|
|
func (s *RSSService) GetExploreRSS(c echo.Context) error {
|
|
|
|
ctx := c.Request().Context()
|
2023-08-09 21:53:06 +08:00
|
|
|
normalStatus := store.Normal
|
|
|
|
memoFind := store.FindMemo{
|
|
|
|
RowStatus: &normalStatus,
|
|
|
|
VisibilityList: []store.Visibility{store.Public},
|
|
|
|
}
|
|
|
|
memoList, err := s.Store.ListMemos(ctx, &memoFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
|
|
|
}
|
2023-02-09 21:17:15 +08:00
|
|
|
|
2023-08-09 21:53:06 +08:00
|
|
|
baseURL := c.Scheme() + "://" + c.Request().Host
|
2024-01-29 21:04:35 +08:00
|
|
|
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL)
|
2023-08-09 21:53:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
|
|
|
|
}
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
|
|
|
|
return c.String(http.StatusOK, rss)
|
|
|
|
}
|
2023-02-09 21:17:15 +08:00
|
|
|
|
2024-01-29 21:04:35 +08:00
|
|
|
func (s *RSSService) GetUserRSS(c echo.Context) error {
|
2023-08-09 21:53:06 +08:00
|
|
|
ctx := c.Request().Context()
|
2024-01-29 21:04:35 +08:00
|
|
|
username := c.Param("username")
|
|
|
|
user, err := s.Store.GetUser(ctx, &store.FindUser{
|
|
|
|
Username: &username,
|
|
|
|
})
|
2023-08-09 21:53:06 +08:00
|
|
|
if err != nil {
|
2024-01-29 21:04:35 +08:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
|
2023-08-09 21:53:06 +08:00
|
|
|
}
|
2024-01-29 21:04:35 +08:00
|
|
|
if user == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusNotFound, "User not found")
|
2023-08-09 21:53:06 +08:00
|
|
|
}
|
2022-10-26 20:13:02 +08:00
|
|
|
|
2023-08-09 21:53:06 +08:00
|
|
|
normalStatus := store.Normal
|
|
|
|
memoFind := store.FindMemo{
|
2024-01-29 21:04:35 +08:00
|
|
|
CreatorID: &user.ID,
|
2023-08-09 21:53:06 +08:00
|
|
|
RowStatus: &normalStatus,
|
|
|
|
VisibilityList: []store.Visibility{store.Public},
|
|
|
|
}
|
|
|
|
memoList, err := s.Store.ListMemos(ctx, &memoFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
|
|
|
}
|
2022-10-26 20:13:02 +08:00
|
|
|
|
2023-08-09 21:53:06 +08:00
|
|
|
baseURL := c.Scheme() + "://" + c.Request().Host
|
2024-01-29 21:04:35 +08:00
|
|
|
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL)
|
2023-08-09 21:53:06 +08:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
|
|
|
|
}
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
|
|
|
|
return c.String(http.StatusOK, rss)
|
2023-02-09 21:17:15 +08:00
|
|
|
}
|
2022-10-26 20:13:02 +08:00
|
|
|
|
2024-01-29 21:04:35 +08:00
|
|
|
func (s *RSSService) generateRSSFromMemoList(ctx context.Context, memoList []*store.Memo, baseURL string) (string, error) {
|
2023-02-11 14:19:26 +08:00
|
|
|
feed := &feeds.Feed{
|
2024-01-29 21:04:35 +08:00
|
|
|
Title: "Memos",
|
2023-02-11 14:19:26 +08:00
|
|
|
Link: &feeds.Link{Href: baseURL},
|
2024-01-29 21:04:35 +08:00
|
|
|
Description: "An open source, lightweight note-taking service. Easily capture and share your great thoughts.",
|
2023-02-11 14:19:26 +08:00
|
|
|
Created: time.Now(),
|
|
|
|
}
|
|
|
|
|
2023-07-06 22:53:38 +08:00
|
|
|
var itemCountLimit = util.Min(len(memoList), maxRSSItemCount)
|
2023-02-18 22:20:28 +08:00
|
|
|
feed.Items = make([]*feeds.Item, itemCountLimit)
|
|
|
|
for i := 0; i < itemCountLimit; i++ {
|
2024-01-29 21:04:35 +08:00
|
|
|
memo := memoList[i]
|
|
|
|
description, err := getRSSItemDescription(memo.Content)
|
2023-12-14 22:33:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-02-11 14:19:26 +08:00
|
|
|
feed.Items[i] = &feeds.Item{
|
2024-01-29 21:04:35 +08:00
|
|
|
Title: getRSSItemTitle(memo.Content),
|
|
|
|
Link: &feeds.Link{Href: baseURL + "/m/" + memo.ResourceName},
|
2023-12-14 22:33:20 +08:00
|
|
|
Description: description,
|
2024-01-29 21:04:35 +08:00
|
|
|
Created: time.Unix(memo.CreatedTs, 0),
|
2023-04-17 23:26:56 +08:00
|
|
|
}
|
2024-01-29 21:04:35 +08:00
|
|
|
resources, err := s.Store.ListResources(ctx, &store.FindResource{
|
|
|
|
MemoID: &memo.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(resources) > 0 {
|
|
|
|
resource := resources[0]
|
2023-04-17 23:26:56 +08:00
|
|
|
enclosure := feeds.Enclosure{}
|
|
|
|
if resource.ExternalLink != "" {
|
|
|
|
enclosure.Url = resource.ExternalLink
|
|
|
|
} else {
|
2024-01-29 21:04:35 +08:00
|
|
|
enclosure.Url = baseURL + "/o/r/" + resource.ResourceName
|
2023-04-17 23:26:56 +08:00
|
|
|
}
|
|
|
|
enclosure.Length = strconv.Itoa(int(resource.Size))
|
|
|
|
enclosure.Type = resource.Type
|
|
|
|
feed.Items[i].Enclosure = &enclosure
|
2023-02-11 14:19:26 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rss, err := feed.ToRss()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return rss, nil
|
|
|
|
}
|
|
|
|
|
2023-02-18 22:20:28 +08:00
|
|
|
func getRSSItemTitle(content string) string {
|
2024-01-29 21:04:35 +08:00
|
|
|
nodes, _ := gomark.Parse(content)
|
2023-12-23 17:58:49 +08:00
|
|
|
if len(nodes) > 0 {
|
|
|
|
firstNode := nodes[0]
|
|
|
|
title := renderer.NewStringRenderer().Render([]ast.Node{firstNode})
|
|
|
|
return title
|
|
|
|
}
|
|
|
|
|
|
|
|
title := strings.Split(content, "\n")[0]
|
|
|
|
var titleLengthLimit = util.Min(len(title), maxRSSItemTitleLength)
|
|
|
|
if titleLengthLimit < len(title) {
|
|
|
|
title = title[:titleLengthLimit] + "..."
|
2023-02-18 22:20:28 +08:00
|
|
|
}
|
|
|
|
return title
|
|
|
|
}
|
|
|
|
|
2023-12-14 22:33:20 +08:00
|
|
|
func getRSSItemDescription(content string) (string, error) {
|
2024-01-29 21:04:35 +08:00
|
|
|
nodes, err := gomark.Parse(content)
|
2023-12-14 22:33:20 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2023-05-20 10:00:21 +08:00
|
|
|
}
|
2023-12-16 11:57:36 +08:00
|
|
|
result := renderer.NewHTMLRenderer().Render(nodes)
|
2023-12-14 22:33:20 +08:00
|
|
|
return result, nil
|
2023-02-18 22:20:28 +08:00
|
|
|
}
|