2023-01-19 13:02:39 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/fthvgb1/wp-go/cache"
|
2023-01-21 11:31:23 +00:00
|
|
|
"github.com/fthvgb1/wp-go/helper/slice"
|
2023-02-06 04:13:38 +00:00
|
|
|
str "github.com/fthvgb1/wp-go/helper/strings"
|
2023-01-19 13:02:39 +00:00
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/logs"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/pkg/models"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/plugins"
|
|
|
|
"github.com/fthvgb1/wp-go/internal/wpconfig"
|
|
|
|
"github.com/fthvgb1/wp-go/plugin/digest"
|
|
|
|
"github.com/fthvgb1/wp-go/rss2"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var timeFormat = "Mon, 02 Jan 2006 15:04:05 +0000"
|
|
|
|
var templateRss rss2.Rss2
|
|
|
|
|
|
|
|
func InitFeed() {
|
|
|
|
templateRss = rss2.Rss2{
|
|
|
|
Title: wpconfig.Options.Value("blogname"),
|
|
|
|
AtomLink: fmt.Sprintf("%s/feed", wpconfig.Options.Value("home")),
|
|
|
|
Link: wpconfig.Options.Value("siteurl"),
|
|
|
|
Description: wpconfig.Options.Value("blogdescription"),
|
2023-02-08 15:49:48 +00:00
|
|
|
Language: wpconfig.GetLang(),
|
2023-01-19 13:02:39 +00:00
|
|
|
UpdatePeriod: "hourly",
|
|
|
|
UpdateFrequency: 1,
|
|
|
|
Generator: wpconfig.Options.Value("home"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-20 10:10:13 +00:00
|
|
|
func CommentsFeedCache() *cache.VarCache[[]string] {
|
2023-01-19 13:02:39 +00:00
|
|
|
return commentsFeedCache
|
|
|
|
}
|
|
|
|
|
2023-01-20 10:10:13 +00:00
|
|
|
func FeedCache() *cache.VarCache[[]string] {
|
2023-01-19 13:02:39 +00:00
|
|
|
return feedCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func PostFeedCache() *cache.MapCache[string, string] {
|
|
|
|
return postFeedCache
|
|
|
|
}
|
|
|
|
|
|
|
|
func feed(arg ...any) (xml []string, err error) {
|
|
|
|
c := arg[0].(*gin.Context)
|
2023-02-09 07:43:20 +00:00
|
|
|
r := RecentPosts(c, 10)
|
2023-01-21 11:31:23 +00:00
|
|
|
ids := slice.Map(r, func(t models.Posts) uint64 {
|
2023-01-19 13:02:39 +00:00
|
|
|
return t.Id
|
|
|
|
})
|
|
|
|
posts, err := GetPostsByIds(c, ids)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-02-06 14:56:36 +00:00
|
|
|
site := wpconfig.Options.Value("siteurl")
|
2023-01-19 13:02:39 +00:00
|
|
|
rs := templateRss
|
|
|
|
rs.LastBuildDate = time.Now().Format(timeFormat)
|
2023-01-21 11:31:23 +00:00
|
|
|
rs.Items = slice.Map(posts, func(t models.Posts) rss2.Item {
|
2023-01-19 13:02:39 +00:00
|
|
|
desc := "无法提供摘要。这是一篇受保护的文章。"
|
|
|
|
if t.PostPassword != "" {
|
2023-02-06 14:56:36 +00:00
|
|
|
plugins.PasswordProjectTitle(&t)
|
2023-01-19 13:02:39 +00:00
|
|
|
plugins.PasswdProjectContent(&t)
|
|
|
|
} else {
|
|
|
|
desc = digest.Raw(t.PostContent, 55, fmt.Sprintf("/p/%d", t.Id))
|
|
|
|
}
|
|
|
|
l := ""
|
|
|
|
if t.CommentStatus == "open" && t.CommentCount > 0 {
|
2023-02-06 14:56:36 +00:00
|
|
|
l = fmt.Sprintf("%s/p/%d#comments", site, t.Id)
|
2023-01-19 13:02:39 +00:00
|
|
|
} else if t.CommentStatus == "open" && t.CommentCount == 0 {
|
2023-02-06 14:56:36 +00:00
|
|
|
l = fmt.Sprintf("%s/p/%d#respond", site, t.Id)
|
2023-01-19 13:02:39 +00:00
|
|
|
}
|
|
|
|
user := GetUserById(c, t.PostAuthor)
|
|
|
|
|
|
|
|
return rss2.Item{
|
|
|
|
Title: t.PostTitle,
|
|
|
|
Creator: user.DisplayName,
|
|
|
|
Guid: t.Guid,
|
|
|
|
SlashComments: int(t.CommentCount),
|
|
|
|
Content: t.PostContent,
|
|
|
|
Category: strings.Join(t.Categories, "、"),
|
|
|
|
CommentLink: l,
|
2023-02-06 14:56:36 +00:00
|
|
|
CommentRss: fmt.Sprintf("%s/p/%d/feed", site, t.Id),
|
|
|
|
Link: fmt.Sprintf("%s/p/%d", site, t.Id),
|
2023-01-19 13:02:39 +00:00
|
|
|
Description: desc,
|
|
|
|
PubDate: t.PostDateGmt.Format(timeFormat),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
xml = []string{rs.GetXML()}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func postFeed(arg ...any) (x string, err error) {
|
|
|
|
c := arg[0].(*gin.Context)
|
|
|
|
id := arg[1].(string)
|
2023-02-06 04:13:38 +00:00
|
|
|
ID := str.ToInteger[uint64](id, 0)
|
2023-01-19 13:02:39 +00:00
|
|
|
maxId, err := GetMaxPostId(c)
|
|
|
|
logs.ErrPrintln(err, "get max post id")
|
2023-02-06 04:13:38 +00:00
|
|
|
if ID < 1 || ID > maxId || err != nil {
|
2023-01-19 13:02:39 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
post, err := GetPostById(c, ID)
|
|
|
|
if post.Id == 0 || err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
comments, err := PostComments(c, post.Id)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
rs := templateRss
|
2023-02-06 14:56:36 +00:00
|
|
|
site := wpconfig.Options.Value("siteurl")
|
2023-01-19 13:02:39 +00:00
|
|
|
|
|
|
|
rs.Title = fmt.Sprintf("《%s》的评论", post.PostTitle)
|
2023-02-06 14:56:36 +00:00
|
|
|
rs.AtomLink = fmt.Sprintf("%s/p/%d/feed", site, post.Id)
|
|
|
|
rs.Link = fmt.Sprintf("%s/p/%d", site, post.Id)
|
2023-01-19 13:02:39 +00:00
|
|
|
rs.LastBuildDate = time.Now().Format(timeFormat)
|
|
|
|
if post.PostPassword != "" {
|
2023-02-06 14:56:36 +00:00
|
|
|
plugins.PasswordProjectTitle(&post)
|
|
|
|
plugins.PasswdProjectContent(&post)
|
2023-01-19 13:02:39 +00:00
|
|
|
if len(comments) > 0 {
|
|
|
|
t := comments[len(comments)-1]
|
|
|
|
rs.Items = []rss2.Item{
|
|
|
|
{
|
|
|
|
Title: fmt.Sprintf("评价者:%s", t.CommentAuthor),
|
2023-02-06 14:56:36 +00:00
|
|
|
Link: fmt.Sprintf("%s/p/%d#comment-%d", site, post.Id, t.CommentId),
|
2023-01-19 13:02:39 +00:00
|
|
|
Creator: t.CommentAuthor,
|
|
|
|
PubDate: t.CommentDateGmt.Format(timeFormat),
|
|
|
|
Guid: fmt.Sprintf("%s#comment-%d", post.Guid, t.CommentId),
|
|
|
|
Description: "评论受保护:要查看请输入密码。",
|
|
|
|
Content: post.PostContent,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-21 11:31:23 +00:00
|
|
|
rs.Items = slice.Map(comments, func(t models.Comments) rss2.Item {
|
2023-01-19 13:02:39 +00:00
|
|
|
return rss2.Item{
|
|
|
|
Title: fmt.Sprintf("评价者:%s", t.CommentAuthor),
|
2023-02-06 14:56:36 +00:00
|
|
|
Link: fmt.Sprintf("%s/p/%d#comment-%d", site, post.Id, t.CommentId),
|
2023-01-19 13:02:39 +00:00
|
|
|
Creator: t.CommentAuthor,
|
|
|
|
PubDate: t.CommentDateGmt.Format(timeFormat),
|
|
|
|
Guid: fmt.Sprintf("%s#comment-%d", post.Guid, t.CommentId),
|
|
|
|
Content: t.CommentContent,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
x = rs.GetXML()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func commentsFeed(args ...any) (r []string, err error) {
|
|
|
|
c := args[0].(*gin.Context)
|
|
|
|
commens := RecentComments(c, 10)
|
|
|
|
rs := templateRss
|
|
|
|
rs.Title = fmt.Sprintf("\"%s\"的评论", wpconfig.Options.Value("blogname"))
|
|
|
|
rs.LastBuildDate = time.Now().Format(timeFormat)
|
2023-02-06 14:56:36 +00:00
|
|
|
site := wpconfig.Options.Value("siteurl")
|
|
|
|
rs.AtomLink = fmt.Sprintf("%s/comments/feed", site)
|
2023-01-21 11:31:23 +00:00
|
|
|
com, err := GetCommentByIds(c, slice.Map(commens, func(t models.Comments) uint64 {
|
2023-01-19 13:02:39 +00:00
|
|
|
return t.CommentId
|
|
|
|
}))
|
|
|
|
if nil != err {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
2023-01-21 11:31:23 +00:00
|
|
|
rs.Items = slice.Map(com, func(t models.Comments) rss2.Item {
|
2023-01-19 13:02:39 +00:00
|
|
|
post, _ := GetPostById(c, t.CommentPostId)
|
|
|
|
desc := "评论受保护:要查看请输入密码。"
|
|
|
|
content := t.CommentContent
|
|
|
|
if post.PostPassword != "" {
|
2023-02-06 14:56:36 +00:00
|
|
|
plugins.PasswordProjectTitle(&post)
|
2023-01-19 13:02:39 +00:00
|
|
|
plugins.PasswdProjectContent(&post)
|
|
|
|
content = post.PostContent
|
|
|
|
} else {
|
|
|
|
desc = digest.ClearHtml(t.CommentContent)
|
|
|
|
content = desc
|
|
|
|
}
|
|
|
|
return rss2.Item{
|
|
|
|
Title: fmt.Sprintf("%s对《%s》的评论", t.CommentAuthor, post.PostTitle),
|
2023-02-06 14:56:36 +00:00
|
|
|
Link: fmt.Sprintf("%s/p/%d#comment-%d", site, post.Id, t.CommentId),
|
2023-01-19 13:02:39 +00:00
|
|
|
Creator: t.CommentAuthor,
|
|
|
|
Description: desc,
|
|
|
|
PubDate: t.CommentDateGmt.Format(timeFormat),
|
|
|
|
Guid: fmt.Sprintf("%s#commment-%d", post.Guid, t.CommentId),
|
|
|
|
Content: content,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
r = []string{rs.GetXML()}
|
|
|
|
return
|
|
|
|
}
|