2022-10-26 15:13:02 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2023-02-09 16:17:15 +03:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2022-10-26 15:13:02 +03:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
2023-02-09 16:17:15 +03:00
|
|
|
"strings"
|
2022-10-26 15:13:02 +03:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/feeds"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
"github.com/usememos/memos/api"
|
2023-04-06 02:42:39 +03:00
|
|
|
"github.com/usememos/memos/common"
|
2022-10-26 15:13:02 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerRSSRoutes(g *echo.Group) {
|
2023-02-09 16:17:15 +03:00
|
|
|
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)
|
2023-02-09 16:17:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
normalStatus := api.Normal
|
|
|
|
memoFind := api.MemoFind{
|
2023-04-17 18:26:56 +03:00
|
|
|
RowStatus: &normalStatus,
|
|
|
|
VisibilityList: []api.Visibility{api.Public},
|
2023-02-09 16:17:15 +03:00
|
|
|
}
|
|
|
|
memoList, err := s.Store.FindMemoList(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)
|
2023-02-09 16:17:15 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
|
|
|
|
}
|
2023-02-10 03:28:14 +03:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
|
|
|
|
return c.String(http.StatusOK, rss)
|
2023-02-09 16:17:15 +03:00
|
|
|
})
|
|
|
|
|
2022-10-26 15:13:02 +03:00
|
|
|
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"))
|
2023-02-09 16:17:15 +03:00
|
|
|
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-02-09 16:17:15 +03:00
|
|
|
}
|
|
|
|
|
2023-04-17 18:26:56 +03:00
|
|
|
systemCustomizedProfile, err := s.getSystemCustomizedProfile(ctx)
|
2022-10-26 15:13:02 +03:00
|
|
|
if err != nil {
|
2023-04-17 18:26:56 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
|
2022-10-26 15:13:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
normalStatus := api.Normal
|
|
|
|
memoFind := api.MemoFind{
|
2023-04-17 18:26:56 +03:00
|
|
|
CreatorID: &id,
|
|
|
|
RowStatus: &normalStatus,
|
|
|
|
VisibilityList: []api.Visibility{api.Public},
|
2022-10-26 15:13:02 +03:00
|
|
|
}
|
|
|
|
memoList, err := s.Store.FindMemoList(ctx, &memoFind)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find memo list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2023-02-09 16:17:15 +03:00
|
|
|
baseURL := c.Scheme() + "://" + c.Request().Host
|
2023-04-17 18:26:56 +03:00
|
|
|
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL, systemCustomizedProfile)
|
2022-10-26 15:13:02 +03:00
|
|
|
if err != nil {
|
2023-02-09 16:17:15 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
|
2022-10-26 15:13:02 +03:00
|
|
|
}
|
2023-02-10 03:28:14 +03:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
|
|
|
|
return c.String(http.StatusOK, rss)
|
2023-02-09 16:17:15 +03:00
|
|
|
})
|
|
|
|
}
|
2022-10-26 15:13:02 +03:00
|
|
|
|
2023-02-18 17:20:28 +03:00
|
|
|
const MaxRSSItemCount = 100
|
|
|
|
const MaxRSSItemTitleLength = 100
|
|
|
|
|
2023-04-17 18:26:56 +03:00
|
|
|
func (s *Server) generateRSSFromMemoList(ctx context.Context, memoList []*api.Memo, baseURL string, profile *api.CustomizedProfile) (string, error) {
|
2023-02-11 09:19:26 +03:00
|
|
|
feed := &feeds.Feed{
|
|
|
|
Title: profile.Name,
|
|
|
|
Link: &feeds.Link{Href: baseURL},
|
|
|
|
Description: profile.Description,
|
|
|
|
Created: time.Now(),
|
|
|
|
}
|
|
|
|
|
2023-04-06 02:42:39 +03:00
|
|
|
var itemCountLimit = common.Min(len(memoList), MaxRSSItemCount)
|
2023-02-18 17:20:28 +03:00
|
|
|
feed.Items = make([]*feeds.Item, itemCountLimit)
|
|
|
|
for i := 0; i < itemCountLimit; i++ {
|
|
|
|
memo := memoList[i]
|
2023-02-11 09:19:26 +03:00
|
|
|
feed.Items[i] = &feeds.Item{
|
2023-02-18 17:20:28 +03:00
|
|
|
Title: getRSSItemTitle(memo.Content),
|
2023-02-11 09:19:26 +03:00
|
|
|
Link: &feeds.Link{Href: baseURL + "/m/" + strconv.Itoa(memo.ID)},
|
2023-02-18 17:20:28 +03:00
|
|
|
Description: getRSSItemDescription(memo.Content),
|
2023-02-11 09:19:26 +03:00
|
|
|
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"},
|
|
|
|
}
|
|
|
|
resourceList, err := s.Store.FindResourceList(ctx, &api.ResourceFind{
|
|
|
|
MemoID: &memo.ID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if len(resourceList) > 0 {
|
|
|
|
enclosure := feeds.Enclosure{}
|
|
|
|
resource := resourceList[0]
|
|
|
|
if resource.ExternalLink != "" {
|
|
|
|
enclosure.Url = resource.ExternalLink
|
|
|
|
} else {
|
2023-05-18 01:53:20 +03:00
|
|
|
enclosure.Url = baseURL + "/o/r/" + strconv.Itoa(resource.ID) + "/" + resource.PublicID + "/" + resource.Filename
|
2023-04-17 18:26:56 +03:00
|
|
|
}
|
|
|
|
enclosure.Length = strconv.Itoa(int(resource.Size))
|
|
|
|
enclosure.Type = resource.Type
|
|
|
|
feed.Items[i].Enclosure = &enclosure
|
2023-02-11 09:19:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rss, err := feed.ToRss()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return rss, nil
|
|
|
|
}
|
|
|
|
|
2023-04-17 18:26:56 +03:00
|
|
|
func (s *Server) getSystemCustomizedProfile(ctx context.Context) (*api.CustomizedProfile, error) {
|
2023-04-16 04:51:03 +03:00
|
|
|
systemSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
|
|
|
Name: api.SystemSettingCustomizedProfileName,
|
|
|
|
})
|
|
|
|
if err != nil && common.ErrorCode(err) != common.NotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-04-03 04:36:34 +03:00
|
|
|
customizedProfile := &api.CustomizedProfile{
|
|
|
|
Name: "memos",
|
|
|
|
LogoURL: "",
|
|
|
|
Description: "",
|
|
|
|
Locale: "en",
|
|
|
|
Appearance: "system",
|
|
|
|
ExternalURL: "",
|
2023-02-09 16:17:15 +03:00
|
|
|
}
|
2023-04-16 04:51:03 +03:00
|
|
|
if systemSetting != nil {
|
|
|
|
if err := json.Unmarshal([]byte(systemSetting.Value), customizedProfile); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-02-09 16:17:15 +03:00
|
|
|
}
|
2023-04-03 04:36:34 +03:00
|
|
|
return customizedProfile, nil
|
2022-10-26 15:13:02 +03:00
|
|
|
}
|
2023-02-18 17:20:28 +03:00
|
|
|
|
|
|
|
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-04-06 02:42:39 +03:00
|
|
|
var titleLengthLimit = common.Min(len(title), MaxRSSItemTitleLength)
|
2023-02-18 17:20:28 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
return description
|
|
|
|
}
|
|
|
|
|
|
|
|
func isTitleDefined(content string) bool {
|
|
|
|
return strings.HasPrefix(content, "# ")
|
|
|
|
}
|