memos/api/rss/rss.go

170 lines
4.7 KiB
Go
Raw Normal View History

2024-01-29 21:04:35 +08:00
package rss
import (
"context"
"net/http"
"strconv"
"strings"
"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
"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"
)
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
}
2024-01-29 21:04:35 +08:00
func NewRSSService(profile *profile.Profile, store *store.Store) *RSSService {
return &RSSService{
Profile: profile,
Store: store,
}
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)
}
2024-01-29 21:04:35 +08:00
func (s *RSSService) GetExploreRSS(c echo.Context) error {
ctx := c.Request().Context()
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)
}
baseURL := c.Scheme() + "://" + c.Request().Host
2024-01-29 21:04:35 +08:00
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL)
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)
}
2024-01-29 21:04:35 +08:00
func (s *RSSService) GetUserRSS(c echo.Context) error {
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,
})
if err != nil {
2024-01-29 21:04:35 +08:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
}
2024-01-29 21:04:35 +08:00
if user == nil {
return echo.NewHTTPError(http.StatusNotFound, "User not found")
}
normalStatus := store.Normal
memoFind := store.FindMemo{
2024-01-29 21:04:35 +08:00
CreatorID: &user.ID,
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)
}
baseURL := c.Scheme() + "://" + c.Request().Host
2024-01-29 21:04:35 +08:00
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL)
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)
}
2024-01-29 21:04:35 +08:00
func (s *RSSService) generateRSSFromMemoList(ctx context.Context, memoList []*store.Memo, baseURL string) (string, error) {
feed := &feeds.Feed{
2024-01-29 21:04:35 +08:00
Title: "Memos",
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.",
Created: time.Now(),
}
2023-07-06 22:53:38 +08:00
var itemCountLimit = util.Min(len(memoList), maxRSSItemCount)
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
}
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
}
}
rss, err := feed.ToRss()
if err != nil {
return "", err
}
return rss, nil
}
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] + "..."
}
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-12-16 11:57:36 +08:00
result := renderer.NewHTMLRenderer().Render(nodes)
2023-12-14 22:33:20 +08:00
return result, nil
}