mirror of
https://github.com/usememos/memos.git
synced 2024-09-21 23:05:59 +08:00
chore: add enclosure to rss (#1559)
This commit is contained in:
parent
994d5dd891
commit
0aa7085303
|
@ -17,8 +17,7 @@ import (
|
||||||
func (s *Server) registerRSSRoutes(g *echo.Group) {
|
func (s *Server) registerRSSRoutes(g *echo.Group) {
|
||||||
g.GET("/explore/rss.xml", func(c echo.Context) error {
|
g.GET("/explore/rss.xml", func(c echo.Context) error {
|
||||||
ctx := c.Request().Context()
|
ctx := c.Request().Context()
|
||||||
|
systemCustomizedProfile, err := s.getSystemCustomizedProfile(ctx)
|
||||||
systemCustomizedProfile, err := getSystemCustomizedProfile(ctx, s)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
|
||||||
}
|
}
|
||||||
|
@ -26,9 +25,7 @@ func (s *Server) registerRSSRoutes(g *echo.Group) {
|
||||||
normalStatus := api.Normal
|
normalStatus := api.Normal
|
||||||
memoFind := api.MemoFind{
|
memoFind := api.MemoFind{
|
||||||
RowStatus: &normalStatus,
|
RowStatus: &normalStatus,
|
||||||
VisibilityList: []api.Visibility{
|
VisibilityList: []api.Visibility{api.Public},
|
||||||
api.Public,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
memoList, err := s.Store.FindMemoList(ctx, &memoFind)
|
memoList, err := s.Store.FindMemoList(ctx, &memoFind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -36,35 +33,31 @@ func (s *Server) registerRSSRoutes(g *echo.Group) {
|
||||||
}
|
}
|
||||||
|
|
||||||
baseURL := c.Scheme() + "://" + c.Request().Host
|
baseURL := c.Scheme() + "://" + c.Request().Host
|
||||||
rss, err := generateRSSFromMemoList(memoList, baseURL, systemCustomizedProfile)
|
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL, systemCustomizedProfile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
|
||||||
return c.String(http.StatusOK, rss)
|
return c.String(http.StatusOK, rss)
|
||||||
})
|
})
|
||||||
|
|
||||||
g.GET("/u/:id/rss.xml", func(c echo.Context) error {
|
g.GET("/u/:id/rss.xml", func(c echo.Context) error {
|
||||||
ctx := c.Request().Context()
|
ctx := c.Request().Context()
|
||||||
|
|
||||||
systemCustomizedProfile, err := getSystemCustomizedProfile(ctx, s)
|
|
||||||
if err != nil {
|
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
id, err := strconv.Atoi(c.Param("id"))
|
id, err := strconv.Atoi(c.Param("id"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusBadRequest, "User id is not a number").SetInternal(err)
|
return echo.NewHTTPError(http.StatusBadRequest, "User id is not a number").SetInternal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
systemCustomizedProfile, err := s.getSystemCustomizedProfile(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
|
||||||
|
}
|
||||||
|
|
||||||
normalStatus := api.Normal
|
normalStatus := api.Normal
|
||||||
memoFind := api.MemoFind{
|
memoFind := api.MemoFind{
|
||||||
CreatorID: &id,
|
CreatorID: &id,
|
||||||
RowStatus: &normalStatus,
|
RowStatus: &normalStatus,
|
||||||
VisibilityList: []api.Visibility{
|
VisibilityList: []api.Visibility{api.Public},
|
||||||
api.Public,
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
memoList, err := s.Store.FindMemoList(ctx, &memoFind)
|
memoList, err := s.Store.FindMemoList(ctx, &memoFind)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -72,8 +65,7 @@ func (s *Server) registerRSSRoutes(g *echo.Group) {
|
||||||
}
|
}
|
||||||
|
|
||||||
baseURL := c.Scheme() + "://" + c.Request().Host
|
baseURL := c.Scheme() + "://" + c.Request().Host
|
||||||
|
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL, systemCustomizedProfile)
|
||||||
rss, err := generateRSSFromMemoList(memoList, baseURL, systemCustomizedProfile)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate rss").SetInternal(err)
|
||||||
}
|
}
|
||||||
|
@ -85,7 +77,7 @@ func (s *Server) registerRSSRoutes(g *echo.Group) {
|
||||||
const MaxRSSItemCount = 100
|
const MaxRSSItemCount = 100
|
||||||
const MaxRSSItemTitleLength = 100
|
const MaxRSSItemTitleLength = 100
|
||||||
|
|
||||||
func generateRSSFromMemoList(memoList []*api.Memo, baseURL string, profile *api.CustomizedProfile) (string, error) {
|
func (s *Server) generateRSSFromMemoList(ctx context.Context, memoList []*api.Memo, baseURL string, profile *api.CustomizedProfile) (string, error) {
|
||||||
feed := &feeds.Feed{
|
feed := &feeds.Feed{
|
||||||
Title: profile.Name,
|
Title: profile.Name,
|
||||||
Link: &feeds.Link{Href: baseURL},
|
Link: &feeds.Link{Href: baseURL},
|
||||||
|
@ -102,6 +94,25 @@ func generateRSSFromMemoList(memoList []*api.Memo, baseURL string, profile *api.
|
||||||
Link: &feeds.Link{Href: baseURL + "/m/" + strconv.Itoa(memo.ID)},
|
Link: &feeds.Link{Href: baseURL + "/m/" + strconv.Itoa(memo.ID)},
|
||||||
Description: getRSSItemDescription(memo.Content),
|
Description: getRSSItemDescription(memo.Content),
|
||||||
Created: time.Unix(memo.CreatedTs, 0),
|
Created: time.Unix(memo.CreatedTs, 0),
|
||||||
|
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 {
|
||||||
|
enclosure.Url = baseURL + "/o/r/" + strconv.Itoa(memo.ID) + "/" + resource.PublicID + "/" + resource.Filename
|
||||||
|
}
|
||||||
|
enclosure.Length = strconv.Itoa(int(resource.Size))
|
||||||
|
enclosure.Type = resource.Type
|
||||||
|
feed.Items[i].Enclosure = &enclosure
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,7 +123,7 @@ func generateRSSFromMemoList(memoList []*api.Memo, baseURL string, profile *api.
|
||||||
return rss, nil
|
return rss, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSystemCustomizedProfile(ctx context.Context, s *Server) (*api.CustomizedProfile, error) {
|
func (s *Server) getSystemCustomizedProfile(ctx context.Context) (*api.CustomizedProfile, error) {
|
||||||
systemSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
systemSetting, err := s.Store.FindSystemSetting(ctx, &api.SystemSettingFind{
|
||||||
Name: api.SystemSettingCustomizedProfileName,
|
Name: api.SystemSettingCustomizedProfileName,
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue