memos/api/v1/rss.go

189 lines
5.6 KiB
Go
Raw Normal View History

2023-07-06 17:53:38 +03:00
package v1
import (
"bytes"
"context"
"encoding/json"
2023-07-06 17:53:38 +03:00
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/gorilla/feeds"
"github.com/labstack/echo/v4"
2023-07-06 17:53:38 +03:00
"github.com/usememos/memos/common/util"
2023-05-25 16:50:37 +03:00
"github.com/usememos/memos/store"
"github.com/yuin/goldmark"
)
2023-07-06 17:53:38 +03:00
const maxRSSItemCount = 100
const maxRSSItemTitleLength = 100
func (s *APIV1Service) registerRSSRoutes(g *echo.Group) {
g.GET("/explore/rss.xml", func(c echo.Context) error {
ctx := c.Request().Context()
2023-04-17 18:26:56 +03:00
systemCustomizedProfile, err := s.getSystemCustomizedProfile(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
normalStatus := store.Normal
memoFind := store.FindMemo{
2023-04-17 18:26:56 +03:00
RowStatus: &normalStatus,
2023-05-25 16:50:37 +03:00
VisibilityList: []store.Visibility{store.Public},
}
2023-05-25 16:50:37 +03:00
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
2023-04-17 18:26:56 +03:00
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL, systemCustomizedProfile)
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)
})
g.GET("/u/:id/rss.xml", func(c echo.Context) error {
ctx := c.Request().Context()
2023-04-17 18:26:56 +03:00
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
2023-04-17 18:26:56 +03:00
return echo.NewHTTPError(http.StatusBadRequest, "User id is not a number").SetInternal(err)
}
2023-04-17 18:26:56 +03:00
systemCustomizedProfile, err := s.getSystemCustomizedProfile(ctx)
if err != nil {
2023-04-17 18:26:56 +03:00
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
}
2023-05-25 16:50:37 +03:00
normalStatus := store.Normal
memoFind := store.FindMemo{
2023-04-17 18:26:56 +03:00
CreatorID: &id,
RowStatus: &normalStatus,
2023-05-25 16:50:37 +03:00
VisibilityList: []store.Visibility{store.Public},
}
2023-05-25 16:50:37 +03:00
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
2023-04-17 18:26:56 +03:00
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL, systemCustomizedProfile)
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-07-06 17:53:38 +03:00
func (s *APIV1Service) generateRSSFromMemoList(ctx context.Context, memoList []*store.Memo, baseURL string, profile *CustomizedProfile) (string, error) {
feed := &feeds.Feed{
Title: profile.Name,
Link: &feeds.Link{Href: baseURL},
Description: profile.Description,
Created: time.Now(),
}
2023-07-06 17:53:38 +03:00
var itemCountLimit = util.Min(len(memoList), maxRSSItemCount)
feed.Items = make([]*feeds.Item, itemCountLimit)
for i := 0; i < itemCountLimit; i++ {
memo := memoList[i]
feed.Items[i] = &feeds.Item{
Title: getRSSItemTitle(memo.Content),
Link: &feeds.Link{Href: baseURL + "/m/" + strconv.Itoa(memo.ID)},
Description: getRSSItemDescription(memo.Content),
Created: time.Unix(memo.CreatedTs, 0),
2023-04-17 18:26:56 +03:00
Enclosure: &feeds.Enclosure{Url: baseURL + "/m/" + strconv.Itoa(memo.ID) + "/image"},
}
2023-05-25 16:50:37 +03:00
if len(memo.ResourceIDList) > 0 {
resourceID := memo.ResourceIDList[0]
resource, err := s.Store.GetResource(ctx, &store.FindResource{
2023-05-25 16:50:37 +03:00
ID: &resourceID,
})
if err != nil {
return "", err
}
2023-07-06 17:53:38 +03:00
if resource == nil {
return "", fmt.Errorf("Resource not found: %d", resourceID)
}
2023-04-17 18:26:56 +03:00
enclosure := feeds.Enclosure{}
if resource.ExternalLink != "" {
enclosure.Url = resource.ExternalLink
} else {
enclosure.Url = baseURL + "/o/r/" + strconv.Itoa(resource.ID)
2023-04-17 18:26:56 +03: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
}
2023-07-06 17:53:38 +03:00
func (s *APIV1Service) getSystemCustomizedProfile(ctx context.Context) (*CustomizedProfile, error) {
systemSetting, err := s.Store.GetSystemSetting(ctx, &store.FindSystemSetting{
2023-07-06 17:53:38 +03:00
Name: SystemSettingCustomizedProfileName.String(),
2023-04-16 04:51:03 +03:00
})
if err != nil {
2023-04-16 04:51:03 +03:00
return nil, err
}
2023-07-06 17:53:38 +03:00
customizedProfile := &CustomizedProfile{
Name: "memos",
LogoURL: "",
Description: "",
Locale: "en",
Appearance: "system",
ExternalURL: "",
}
2023-04-16 04:51:03 +03:00
if systemSetting != nil {
if err := json.Unmarshal([]byte(systemSetting.Value), customizedProfile); err != nil {
return nil, err
}
}
return customizedProfile, nil
}
func getRSSItemTitle(content string) string {
var title string
if isTitleDefined(content) {
title = strings.Split(content, "\n")[0][2:]
} else {
title = strings.Split(content, "\n")[0]
2023-07-06 17:53:38 +03:00
var titleLengthLimit = util.Min(len(title), maxRSSItemTitleLength)
if titleLengthLimit < len(title) {
title = title[:titleLengthLimit] + "..."
}
}
return title
}
func getRSSItemDescription(content string) string {
var description string
if isTitleDefined(content) {
var firstLineEnd = strings.Index(content, "\n")
description = strings.Trim(content[firstLineEnd+1:], " ")
} else {
description = content
}
// TODO: use our `./plugin/gomark` parser to handle markdown-like content.
var buf bytes.Buffer
if err := goldmark.Convert([]byte(description), &buf); err != nil {
panic(err)
}
return buf.String()
}
func isTitleDefined(content string) bool {
return strings.HasPrefix(content, "# ")
}