小优化

This commit is contained in:
xing 2023-02-06 22:56:36 +08:00
parent 562520c080
commit fda83e5d1a
5 changed files with 35 additions and 35 deletions

View File

@ -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")

View File

@ -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)

View File

@ -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),

View File

@ -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)

View File

@ -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 := `
<form action="/login" class="post-password-form" method="post">
<p>此内容受密码保护如需查阅请在下列字段中输入您的密码</p>
<p><label for="pwbox-%d">密码 <input name="post_password" id="pwbox-%d" type="password" size="20"></label> <input type="submit" name="Submit" value="提交"></p>
</form>`
post.PostContent = fmt.Sprintf(format, post.Id, post.Id)
}
post.PostContent = fmt.Sprintf(format, post.Id, post.Id)
}