From e3dc2a156c527f028d3adcb1ce7401c5ed7470bd Mon Sep 17 00:00:00 2001 From: xing Date: Sat, 8 Oct 2022 16:37:27 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actions/common/posts.go | 6 +----- actions/detail.go | 2 +- actions/feed.go | 26 ++++++++++++++------------ actions/index.go | 4 +--- plugins/digest.go | 8 ++++++++ 5 files changed, 25 insertions(+), 21 deletions(-) diff --git a/actions/common/posts.go b/actions/common/posts.go index 988bad3..5856996 100644 --- a/actions/common/posts.go +++ b/actions/common/posts.go @@ -12,14 +12,10 @@ import ( "time" ) -func GetPostAndCache(ctx context.Context, id uint64) (models.WpPosts, error) { +func GetPostById(ctx context.Context, id uint64) (models.WpPosts, error) { return postsCache.GetCache(ctx, id, time.Second, id) } -func GetPostById(id uint64) models.WpPosts { - return postsCache.Get(id) -} - func GetPostsByIds(ctx context.Context, ids []uint64) ([]models.WpPosts, error) { return postsCache.GetCacheBatch(ctx, ids, time.Second, ids) } diff --git a/actions/detail.go b/actions/detail.go index d252a61..7ec67e5 100644 --- a/actions/detail.go +++ b/actions/detail.go @@ -61,7 +61,7 @@ func Detail(c *gin.Context) { if ID > maxId || err != nil { return } - post, err := common.GetPostAndCache(c, ID) + post, err := common.GetPostById(c, ID) if post.Id == 0 || err != nil { return } diff --git a/actions/feed.go b/actions/feed.go index 39fca9f..a82d2ea 100644 --- a/actions/feed.go +++ b/actions/feed.go @@ -19,6 +19,7 @@ import ( var feedCache = cache.NewSliceCache(feed, time.Hour) var postFeedCache = cache.NewMapCacheByFn[string, string](postFeed, time.Hour) var tmp = "Mon, 02 Jan 2006 15:04:05 GMT" +var timeFormat = "Mon, 02 Jan 2006 15:04:05 +0000" var templateRss rss2.Rss2 var commentsFeedCache = cache.NewSliceCache(commentsFeed, time.Hour) @@ -79,7 +80,7 @@ func feed(arg ...any) (xml []string, err error) { return } rs := templateRss - rs.LastBuildDate = time.Now().Format(time.RFC1123Z) + rs.LastBuildDate = time.Now().Format(timeFormat) rs.Items = helper.SliceMap(posts, func(t models.WpPosts) rss2.Item { desc := "无法提供摘要。这是一篇受保护的文章。" common.PasswordProjectTitle(&t) @@ -107,7 +108,7 @@ func feed(arg ...any) (xml []string, err error) { CommentRss: fmt.Sprintf("%s/p/%d/feed", models.Options["siteurl"], t.Id), Link: fmt.Sprintf("%s/p/%d", models.Options["siteurl"], t.Id), Description: desc, - PubDate: t.PostDateGmt.Format(time.RFC1123Z), + PubDate: t.PostDateGmt.Format(timeFormat), } }) xml = []string{rs.GetXML()} @@ -155,7 +156,7 @@ func postFeed(arg ...any) (x string, err error) { if ID > maxId || err != nil { return } - post, err := common.GetPostAndCache(c, ID) + post, err := common.GetPostById(c, ID) if post.Id == 0 || err != nil { return } @@ -169,7 +170,7 @@ func postFeed(arg ...any) (x string, err error) { rs.Title = fmt.Sprintf("《%s》的评论", post.PostTitle) rs.AtomLink = fmt.Sprintf("%s/p/%d/feed", models.Options["siteurl"], post.Id) rs.Link = fmt.Sprintf("%s/p/%d", models.Options["siteurl"], post.Id) - rs.LastBuildDate = time.Now().Format(time.RFC1123Z) + rs.LastBuildDate = time.Now().Format(timeFormat) if post.PostPassword != "" { if len(comments) > 0 { common.PasswdProjectContent(&post) @@ -179,7 +180,7 @@ func postFeed(arg ...any) (x string, err error) { Title: fmt.Sprintf("评价者:%s", t.CommentAuthor), Link: fmt.Sprintf("%s/p/%d#comment-%d", models.Options["siteurl"], post.Id, t.CommentId), Creator: t.CommentAuthor, - PubDate: t.CommentDateGmt.Format(time.RFC1123Z), + PubDate: t.CommentDateGmt.Format(timeFormat), Guid: fmt.Sprintf("%s#comment-%d", post.Guid, t.CommentId), Description: "评论受保护:要查看请输入密码。", Content: post.PostContent, @@ -192,7 +193,7 @@ func postFeed(arg ...any) (x string, err error) { Title: fmt.Sprintf("评价者:%s", t.CommentAuthor), Link: fmt.Sprintf("%s/p/%d#comment-%d", models.Options["siteurl"], post.Id, t.CommentId), Creator: t.CommentAuthor, - PubDate: t.CommentDateGmt.Format(time.RFC1123Z), + PubDate: t.CommentDateGmt.Format(timeFormat), Guid: fmt.Sprintf("%s#comment-%d", post.Guid, t.CommentId), Content: t.CommentContent, } @@ -222,7 +223,8 @@ func commentsFeed(args ...any) (r []string, err error) { c := args[0].(*gin.Context) commens := common.RecentComments(c, 10) rs := templateRss - rs.LastBuildDate = time.Now().Format(time.RFC1123Z) + rs.Title = fmt.Sprintf("\"%s\"的评论", models.Options["blogname"]) + rs.LastBuildDate = time.Now().Format(timeFormat) rs.AtomLink = fmt.Sprintf("%s/comments/feed", models.Options["siteurl"]) com, err := common.GetCommentByIds(c, helper.SliceMap(commens, func(t models.WpComments) uint64 { return t.CommentId @@ -231,7 +233,7 @@ func commentsFeed(args ...any) (r []string, err error) { return []string{}, err } rs.Items = helper.SliceMap(com, func(t models.WpComments) rss2.Item { - post, _ := common.GetPostAndCache(c, t.CommentPostId) + post, _ := common.GetPostById(c, t.CommentPostId) common.PasswordProjectTitle(&post) desc := "评论受保护:要查看请输入密码。" content := t.CommentContent @@ -239,15 +241,15 @@ func commentsFeed(args ...any) (r []string, err error) { common.PasswdProjectContent(&post) content = post.PostContent } else { - desc = plugins.DigestRaw(t.CommentContent, 55, fmt.Sprintf("%s/p/%d#comment-%d", models.Options["siteurl"], post.Id, t.CommentId)) - content = t.CommentContent + desc = plugins.ClearHtml(t.CommentContent) + content = desc } return rss2.Item{ - Title: fmt.Sprintf("《%s》的评论", post.PostTitle), + Title: fmt.Sprintf("%s对《%s》的评论", t.CommentAuthor, post.PostTitle), Link: fmt.Sprintf("%s/p/%d#comment-%d", models.Options["siteurl"], post.Id, t.CommentId), Creator: t.CommentAuthor, Description: desc, - PubDate: t.CommentDateGmt.Format(time.RFC1123Z), + PubDate: t.CommentDateGmt.Format(timeFormat), Guid: fmt.Sprintf("%s#commment-%d", post.Guid, t.CommentId), Content: content, } diff --git a/actions/index.go b/actions/index.go index 66a9766..6af901d 100644 --- a/actions/index.go +++ b/actions/index.go @@ -204,9 +204,7 @@ func Index(c *gin.Context) { pw := h.session.Get("post_password") plug := plugins.NewPostPlugin(c, h.scene) - for i, v := range postIds { - post := common.GetPostById(v.Id) - postIds[i] = post + for i, post := range postIds { common.PasswordProjectTitle(&postIds[i]) if post.PostPassword != "" && pw != post.PostPassword { common.PasswdProjectContent(&postIds[i]) diff --git a/plugins/digest.go b/plugins/digest.go index 6183099..d67b485 100644 --- a/plugins/digest.go +++ b/plugins/digest.go @@ -43,6 +43,14 @@ func DigestCache(ctx *gin.Context, id uint64, str string) string { return content } +func ClearHtml(str string) string { + content := removeWpBlock.ReplaceAllString(str, "") + content = strings.Trim(content, " \t\n\r\000\x0B") + content = strings.Replace(content, "]]>", "]]>", -1) + content = helper.StripTagsX(content, "

      • ") + return str +} + func DigestRaw(str string, limit int, u string) string { if r := more.FindString(str); r != "" { m := strings.Split(str, r)