feat: generate RSS item title and limit item count (#1117)

* feat: generate RSS item title and limit item count

* Update server/rss.go

Co-authored-by: boojack <stevenlgtm@gmail.com>

* Update server/rss.go

---------

Co-authored-by: boojack <stevenlgtm@gmail.com>
pull/1120/head
Stephen Zhou 2 years ago committed by GitHub
parent cbbd284e7a
commit 65aa51d525
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -81,6 +81,9 @@ 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 generateRSSFromMemoList(memoList []*api.Memo, baseURL string, profile *api.CustomizedProfile) (string, error) {
feed := &feeds.Feed{ feed := &feeds.Feed{
Title: profile.Name, Title: profile.Name,
@ -89,29 +92,17 @@ func generateRSSFromMemoList(memoList []*api.Memo, baseURL string, profile *api.
Created: time.Now(), Created: time.Now(),
} }
feed.Items = make([]*feeds.Item, len(memoList)) var itemCountLimit = min(len(memoList), MaxRSSItemCount)
for i, memo := range memoList {
var useTitle = strings.HasPrefix(memo.Content, "# ")
var title string feed.Items = make([]*feeds.Item, itemCountLimit)
if useTitle {
title = strings.Split(memo.Content, "\n")[0][2:]
} else {
title = memo.Creator.Username + "-memos-" + strconv.Itoa(memo.ID)
}
var description string for i := 0; i < itemCountLimit; i++ {
if useTitle { memo := memoList[i]
var firstLineEnd = strings.Index(memo.Content, "\n")
description = memo.Content[firstLineEnd+1:]
} else {
description = memo.Content
}
feed.Items[i] = &feeds.Item{ feed.Items[i] = &feeds.Item{
Title: title, Title: getRSSItemTitle(memo.Content),
Link: &feeds.Link{Href: baseURL + "/m/" + strconv.Itoa(memo.ID)}, Link: &feeds.Link{Href: baseURL + "/m/" + strconv.Itoa(memo.ID)},
Description: description, Description: getRSSItemDescription(memo.Content),
Created: time.Unix(memo.CreatedTs, 0), Created: time.Unix(memo.CreatedTs, 0),
} }
} }
@ -175,3 +166,39 @@ func getSystemCustomizedProfile(ctx context.Context, s *Server) (api.CustomizedP
} }
return systemStatus.CustomizedProfile, nil return systemStatus.CustomizedProfile, nil
} }
func min(a, b int) int {
if a < b {
return a
}
return b
}
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]
var titleLengthLimit = 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
}
return description
}
func isTitleDefined(content string) bool {
return strings.HasPrefix(content, "# ")
}

Loading…
Cancel
Save