chore: add enclosure to rss (#1559)

This commit is contained in:
boojack 2023-04-17 23:26:56 +08:00 committed by GitHub
parent 994d5dd891
commit 0aa7085303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,18 +17,15 @@ import (
func (s *Server) registerRSSRoutes(g *echo.Group) {
g.GET("/explore/rss.xml", func(c echo.Context) error {
ctx := c.Request().Context()
systemCustomizedProfile, err := getSystemCustomizedProfile(ctx, s)
systemCustomizedProfile, err := s.getSystemCustomizedProfile(ctx)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to get system customized profile").SetInternal(err)
}
normalStatus := api.Normal
memoFind := api.MemoFind{
RowStatus: &normalStatus,
VisibilityList: []api.Visibility{
api.Public,
},
RowStatus: &normalStatus,
VisibilityList: []api.Visibility{api.Public},
}
memoList, err := s.Store.FindMemoList(ctx, &memoFind)
if err != nil {
@ -36,35 +33,31 @@ func (s *Server) registerRSSRoutes(g *echo.Group) {
}
baseURL := c.Scheme() + "://" + c.Request().Host
rss, err := generateRSSFromMemoList(memoList, baseURL, systemCustomizedProfile)
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()
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"))
if err != nil {
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
memoFind := api.MemoFind{
CreatorID: &id,
RowStatus: &normalStatus,
VisibilityList: []api.Visibility{
api.Public,
},
CreatorID: &id,
RowStatus: &normalStatus,
VisibilityList: []api.Visibility{api.Public},
}
memoList, err := s.Store.FindMemoList(ctx, &memoFind)
if err != nil {
@ -72,8 +65,7 @@ func (s *Server) registerRSSRoutes(g *echo.Group) {
}
baseURL := c.Scheme() + "://" + c.Request().Host
rss, err := generateRSSFromMemoList(memoList, baseURL, systemCustomizedProfile)
rss, err := s.generateRSSFromMemoList(ctx, memoList, baseURL, systemCustomizedProfile)
if err != nil {
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 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{
Title: profile.Name,
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)},
Description: getRSSItemDescription(memo.Content),
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
}
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{
Name: api.SystemSettingCustomizedProfileName,
})