diff --git a/internal/actions/detail.go b/internal/actions/detail.go index 981c681..1270b5d 100644 --- a/internal/actions/detail.go +++ b/internal/actions/detail.go @@ -67,10 +67,13 @@ func Detail(c *gin.Context) { showComment = true } user := cache.GetUserById(c, post.PostAuthor) - plugins.PasswordProjectTitle(&post) - if post.PostPassword != "" && pw != post.PostPassword { - plugins.PasswdProjectContent(&post) - showComment = false + + if post.PostPassword != "" { + plugins.PasswordProjectTitle(&post) + if pw != post.PostPassword { + plugins.PasswdProjectContent(&post) + showComment = false + } } else if s, ok := cache.NewCommentCache().Get(c, c.Request.URL.RawQuery); ok && s != "" && (post.PostPassword == "" || post.PostPassword != "" && pw == post.PostPassword) { c.Writer.WriteHeader(http.StatusOK) c.Writer.Header().Set("Content-Type", "text/html; charset=utf-8") diff --git a/internal/actions/index.go b/internal/actions/index.go index a0034b5..bc77bc6 100644 --- a/internal/actions/index.go +++ b/internal/actions/index.go @@ -277,18 +277,16 @@ func Index(c *gin.Context) { pw := h.session.Get("post_password") plug := plugins.NewPostPlugin(c, h.scene) for i, post := range posts { - plugins.PasswordProjectTitle(&posts[i]) - if post.PostPassword != "" && pw != post.PostPassword { - plugins.PasswdProjectContent(&posts[i]) + if post.PostPassword != "" { + plugins.PasswordProjectTitle(&posts[i]) + if pw != post.PostPassword { + plugins.PasswdProjectContent(&posts[i]) + } } else { plugins.ApplyPlugin(plug, &posts[i]) } } - for i, post := range recent { - if post.PostPassword != "" && pw != post.PostPassword { - plugins.PasswdProjectContent(&recent[i]) - } - } + q := c.Request.URL.Query().Encode() if q != "" { q = fmt.Sprintf("?%s", q) diff --git a/internal/pkg/cache/feed.go b/internal/pkg/cache/feed.go index bc10635..e4a5518 100644 --- a/internal/pkg/cache/feed.go +++ b/internal/pkg/cache/feed.go @@ -54,21 +54,22 @@ func feed(arg ...any) (xml []string, err error) { if err != nil { return } + site := wpconfig.Options.Value("siteurl") rs := templateRss rs.LastBuildDate = time.Now().Format(timeFormat) rs.Items = slice.Map(posts, func(t models.Posts) rss2.Item { desc := "无法提供摘要。这是一篇受保护的文章。" - plugins.PasswordProjectTitle(&t) if t.PostPassword != "" { + plugins.PasswordProjectTitle(&t) plugins.PasswdProjectContent(&t) } else { desc = digest.Raw(t.PostContent, 55, fmt.Sprintf("/p/%d", t.Id)) } l := "" if t.CommentStatus == "open" && t.CommentCount > 0 { - l = fmt.Sprintf("%s/p/%d#comments", wpconfig.Options.Value("siteurl"), t.Id) + l = fmt.Sprintf("%s/p/%d#comments", site, t.Id) } else if t.CommentStatus == "open" && t.CommentCount == 0 { - l = fmt.Sprintf("%s/p/%d#respond", wpconfig.Options.Value("siteurl"), t.Id) + l = fmt.Sprintf("%s/p/%d#respond", site, t.Id) } user := GetUserById(c, t.PostAuthor) @@ -80,8 +81,8 @@ func feed(arg ...any) (xml []string, err error) { Content: t.PostContent, Category: strings.Join(t.Categories, "、"), CommentLink: l, - CommentRss: fmt.Sprintf("%s/p/%d/feed", wpconfig.Options.Value("siteurl"), t.Id), - Link: fmt.Sprintf("%s/p/%d", wpconfig.Options.Value("siteurl"), t.Id), + CommentRss: fmt.Sprintf("%s/p/%d/feed", site, t.Id), + Link: fmt.Sprintf("%s/p/%d", site, t.Id), Description: desc, PubDate: t.PostDateGmt.Format(timeFormat), } @@ -103,25 +104,26 @@ func postFeed(arg ...any) (x string, err error) { if post.Id == 0 || err != nil { return } - plugins.PasswordProjectTitle(&post) comments, err := PostComments(c, post.Id) if err != nil { return } rs := templateRss + site := wpconfig.Options.Value("siteurl") rs.Title = fmt.Sprintf("《%s》的评论", post.PostTitle) - rs.AtomLink = fmt.Sprintf("%s/p/%d/feed", wpconfig.Options.Value("siteurl"), post.Id) - rs.Link = fmt.Sprintf("%s/p/%d", wpconfig.Options.Value("siteurl"), post.Id) + rs.AtomLink = fmt.Sprintf("%s/p/%d/feed", site, post.Id) + rs.Link = fmt.Sprintf("%s/p/%d", site, post.Id) rs.LastBuildDate = time.Now().Format(timeFormat) if post.PostPassword != "" { + plugins.PasswordProjectTitle(&post) + plugins.PasswdProjectContent(&post) if len(comments) > 0 { - plugins.PasswdProjectContent(&post) t := comments[len(comments)-1] rs.Items = []rss2.Item{ { Title: fmt.Sprintf("评价者:%s", t.CommentAuthor), - Link: fmt.Sprintf("%s/p/%d#comment-%d", wpconfig.Options.Value("siteurl"), post.Id, t.CommentId), + Link: fmt.Sprintf("%s/p/%d#comment-%d", site, post.Id, t.CommentId), Creator: t.CommentAuthor, PubDate: t.CommentDateGmt.Format(timeFormat), Guid: fmt.Sprintf("%s#comment-%d", post.Guid, t.CommentId), @@ -134,7 +136,7 @@ func postFeed(arg ...any) (x string, err error) { rs.Items = slice.Map(comments, func(t models.Comments) rss2.Item { return rss2.Item{ Title: fmt.Sprintf("评价者:%s", t.CommentAuthor), - Link: fmt.Sprintf("%s/p/%d#comment-%d", wpconfig.Options.Value("siteurl"), post.Id, t.CommentId), + Link: fmt.Sprintf("%s/p/%d#comment-%d", site, post.Id, t.CommentId), Creator: t.CommentAuthor, PubDate: t.CommentDateGmt.Format(timeFormat), Guid: fmt.Sprintf("%s#comment-%d", post.Guid, t.CommentId), @@ -153,7 +155,8 @@ func commentsFeed(args ...any) (r []string, err error) { rs := templateRss rs.Title = fmt.Sprintf("\"%s\"的评论", wpconfig.Options.Value("blogname")) rs.LastBuildDate = time.Now().Format(timeFormat) - rs.AtomLink = fmt.Sprintf("%s/comments/feed", wpconfig.Options.Value("siteurl")) + site := wpconfig.Options.Value("siteurl") + rs.AtomLink = fmt.Sprintf("%s/comments/feed", site) com, err := GetCommentByIds(c, slice.Map(commens, func(t models.Comments) uint64 { return t.CommentId })) @@ -162,10 +165,10 @@ func commentsFeed(args ...any) (r []string, err error) { } rs.Items = slice.Map(com, func(t models.Comments) rss2.Item { post, _ := GetPostById(c, t.CommentPostId) - plugins.PasswordProjectTitle(&post) desc := "评论受保护:要查看请输入密码。" content := t.CommentContent if post.PostPassword != "" { + plugins.PasswordProjectTitle(&post) plugins.PasswdProjectContent(&post) content = post.PostContent } else { @@ -174,7 +177,7 @@ func commentsFeed(args ...any) (r []string, err error) { } return rss2.Item{ Title: fmt.Sprintf("%s对《%s》的评论", t.CommentAuthor, post.PostTitle), - Link: fmt.Sprintf("%s/p/%d#comment-%d", wpconfig.Options.Value("siteurl"), post.Id, t.CommentId), + Link: fmt.Sprintf("%s/p/%d#comment-%d", site, post.Id, t.CommentId), Creator: t.CommentAuthor, Description: desc, PubDate: t.CommentDateGmt.Format(timeFormat), diff --git a/internal/pkg/cache/posts.go b/internal/pkg/cache/posts.go index 928ce5b..d4ea374 100644 --- a/internal/pkg/cache/posts.go +++ b/internal/pkg/cache/posts.go @@ -43,7 +43,7 @@ func GetMaxPostId(ctx *gin.Context) (uint64, error) { return maxPostIdCache.GetCache(ctx, time.Second, ctx) } -func RecentPosts(ctx context.Context, n int, password bool) (r []models.Posts) { +func RecentPosts(ctx context.Context, n int, project bool) (r []models.Posts) { nn := n if nn <= 5 { nn = 10 @@ -52,7 +52,7 @@ func RecentPosts(ctx context.Context, n int, password bool) (r []models.Posts) { if n < len(r) { r = r[:n] } - if password { + if project { r = slice.Map(r, func(t models.Posts) models.Posts { if t.PostPassword != "" { plugins.PasswordProjectTitle(&t) diff --git a/internal/plugins/posts.go b/internal/plugins/posts.go index fde5ea0..711728d 100644 --- a/internal/plugins/posts.go +++ b/internal/plugins/posts.go @@ -19,18 +19,14 @@ func ApplyPlugin(p *Plugin[models.Posts], post *models.Posts) { } func PasswordProjectTitle(post *models.Posts) { - if post.PostPassword != "" { - post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle) - } + post.PostTitle = fmt.Sprintf("密码保护:%s", post.PostTitle) } func PasswdProjectContent(post *models.Posts) { - if post.PostContent != "" { - format := ` + format := `
` - post.PostContent = fmt.Sprintf(format, post.Id, post.Id) - } + post.PostContent = fmt.Sprintf(format, post.Id, post.Id) }