wp-go/internal/actions/feed.go

267 lines
7.8 KiB
Go
Raw Normal View History

2022-10-06 13:33:04 +00:00
package actions
import (
"fmt"
"github.com/gin-gonic/gin"
2022-10-07 12:37:42 +00:00
"github/fthvgb1/wp-go/cache"
2022-10-06 13:33:04 +00:00
"github/fthvgb1/wp-go/helper"
2023-01-12 12:42:16 +00:00
cache2 "github/fthvgb1/wp-go/internal/cache"
2023-01-13 06:04:32 +00:00
"github/fthvgb1/wp-go/internal/pkg/logs"
models2 "github/fthvgb1/wp-go/internal/pkg/models"
2023-01-12 12:42:16 +00:00
"github/fthvgb1/wp-go/internal/plugins"
2023-01-12 10:17:34 +00:00
"github/fthvgb1/wp-go/internal/wpconfig"
2023-01-12 12:42:16 +00:00
"github/fthvgb1/wp-go/plugin/digest"
2022-10-07 12:37:42 +00:00
"github/fthvgb1/wp-go/rss2"
2022-10-06 13:33:04 +00:00
"net/http"
2022-10-07 14:27:34 +00:00
"strconv"
2022-10-06 13:33:04 +00:00
"strings"
"time"
)
2022-10-08 06:01:05 +00:00
var feedCache = cache.NewSliceCache(feed, time.Hour)
2022-10-07 14:27:34 +00:00
var postFeedCache = cache.NewMapCacheByFn[string, string](postFeed, time.Hour)
2022-10-07 12:37:42 +00:00
var tmp = "Mon, 02 Jan 2006 15:04:05 GMT"
2022-10-08 08:37:27 +00:00
var timeFormat = "Mon, 02 Jan 2006 15:04:05 +0000"
2022-10-07 14:27:34 +00:00
var templateRss rss2.Rss2
2022-10-08 06:01:05 +00:00
var commentsFeedCache = cache.NewSliceCache(commentsFeed, time.Hour)
2022-10-07 12:37:42 +00:00
2022-10-07 14:27:34 +00:00
func InitFeed() {
templateRss = rss2.Rss2{
2022-11-17 08:29:39 +00:00
Title: wpconfig.Options.Value("blogname"),
AtomLink: fmt.Sprintf("%s/feed", wpconfig.Options.Value("home")),
Link: wpconfig.Options.Value("siteurl"),
Description: wpconfig.Options.Value("blogdescription"),
2022-10-07 14:27:34 +00:00
Language: "zh-CN",
UpdatePeriod: "hourly",
UpdateFrequency: 1,
2022-11-17 08:29:39 +00:00
Generator: wpconfig.Options.Value("home"),
2022-10-06 13:33:04 +00:00
}
2022-10-07 12:37:42 +00:00
}
2022-10-06 13:33:04 +00:00
2022-10-07 15:16:25 +00:00
func ClearCache() {
postFeedCache.ClearExpired()
2022-11-08 09:41:01 +00:00
commentCache.ClearExpired()
2022-10-07 15:16:25 +00:00
}
2022-11-15 08:36:21 +00:00
func FlushCache() {
postFeedCache.Flush()
commentCache.Flush()
}
2022-10-07 15:16:25 +00:00
2022-10-07 12:37:42 +00:00
func isCacheExpired(c *gin.Context, lastTime time.Time) bool {
eTag := helper.StringMd5(lastTime.Format(tmp))
since := c.Request.Header.Get("If-Modified-Since")
cTag := c.Request.Header.Get("If-None-Match")
if since != "" && cTag != "" {
cGMT, err := time.Parse(tmp, since)
if err == nil && lastTime.Unix() <= cGMT.Unix() && eTag == cTag {
c.Status(http.StatusNotModified)
return false
2022-10-06 13:33:04 +00:00
}
2022-10-07 12:37:42 +00:00
}
return true
}
func Feed(c *gin.Context) {
2022-11-08 09:41:01 +00:00
if !isCacheExpired(c, feedCache.GetLastSetTime()) {
2022-10-07 14:27:34 +00:00
c.Status(http.StatusNotModified)
} else {
2022-10-07 14:54:53 +00:00
r, err := feedCache.GetCache(c, time.Second, c)
if err != nil {
c.Status(http.StatusInternalServerError)
c.Abort()
c.Error(err)
return
}
2022-11-08 09:41:01 +00:00
setFeed(r[0], c, feedCache.GetLastSetTime())
2022-10-07 12:37:42 +00:00
}
}
2022-10-06 13:33:04 +00:00
2022-10-07 12:37:42 +00:00
func feed(arg ...any) (xml []string, err error) {
c := arg[0].(*gin.Context)
2023-01-12 12:42:16 +00:00
r := cache2.RecentPosts(c, 10)
2023-01-13 06:04:32 +00:00
ids := helper.SliceMap(r, func(t models2.Posts) uint64 {
2022-10-07 12:37:42 +00:00
return t.Id
})
2023-01-12 12:42:16 +00:00
posts, err := cache2.GetPostsByIds(c, ids)
2022-10-07 12:37:42 +00:00
if err != nil {
return
}
2022-10-07 14:27:34 +00:00
rs := templateRss
2022-10-08 08:37:27 +00:00
rs.LastBuildDate = time.Now().Format(timeFormat)
2023-01-13 06:04:32 +00:00
rs.Items = helper.SliceMap(posts, func(t models2.Posts) rss2.Item {
2022-10-07 12:37:42 +00:00
desc := "无法提供摘要。这是一篇受保护的文章。"
2023-01-12 12:42:16 +00:00
plugins.PasswordProjectTitle(&t)
2022-10-07 12:37:42 +00:00
if t.PostPassword != "" {
2023-01-12 12:42:16 +00:00
plugins.PasswdProjectContent(&t)
2022-10-07 12:37:42 +00:00
} else {
2023-01-12 12:42:16 +00:00
desc = digest.Raw(t.PostContent, 55, fmt.Sprintf("/p/%d", t.Id))
2022-10-07 12:37:42 +00:00
}
l := ""
if t.CommentStatus == "open" && t.CommentCount > 0 {
2022-11-17 08:29:39 +00:00
l = fmt.Sprintf("%s/p/%d#comments", wpconfig.Options.Value("siteurl"), t.Id)
2022-10-07 12:37:42 +00:00
} else if t.CommentStatus == "open" && t.CommentCount == 0 {
2022-11-17 08:29:39 +00:00
l = fmt.Sprintf("%s/p/%d#respond", wpconfig.Options.Value("siteurl"), t.Id)
2022-10-07 12:37:42 +00:00
}
2023-01-12 12:42:16 +00:00
user := cache2.GetUserById(c, t.PostAuthor)
2022-10-07 12:37:42 +00:00
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,
2022-11-17 08:29:39 +00:00
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),
2022-10-07 12:37:42 +00:00
Description: desc,
2022-10-08 08:37:27 +00:00
PubDate: t.PostDateGmt.Format(timeFormat),
2022-10-07 12:37:42 +00:00
}
})
xml = []string{rs.GetXML()}
return
2022-10-06 13:33:04 +00:00
}
2022-10-07 14:27:34 +00:00
2022-10-07 14:54:53 +00:00
func setFeed(s string, c *gin.Context, t time.Time) {
lastTimeGMT := t.Format(tmp)
2022-10-07 14:27:34 +00:00
eTag := helper.StringMd5(lastTimeGMT)
c.Header("Content-Type", "application/rss+xml; charset=UTF-8")
c.Header("Last-Modified", lastTimeGMT)
c.Header("ETag", eTag)
2022-10-07 14:54:53 +00:00
c.String(http.StatusOK, s)
2022-10-07 14:27:34 +00:00
}
func PostFeed(c *gin.Context) {
id := c.Param("id")
2022-11-08 09:41:01 +00:00
if !isCacheExpired(c, postFeedCache.GetLastSetTime(id)) {
2022-10-07 14:27:34 +00:00
c.Status(http.StatusNotModified)
} else {
s, err := postFeedCache.GetCache(c, id, time.Second, c, id)
if err != nil {
c.Status(http.StatusInternalServerError)
c.Abort()
c.Error(err)
return
}
2022-11-08 09:41:01 +00:00
setFeed(s, c, postFeedCache.GetLastSetTime(id))
2022-10-07 14:27:34 +00:00
}
}
func postFeed(arg ...any) (x string, err error) {
c := arg[0].(*gin.Context)
id := arg[1].(string)
Id := 0
if id != "" {
Id, err = strconv.Atoi(id)
if err != nil {
return
}
}
ID := uint64(Id)
2023-01-12 12:42:16 +00:00
maxId, err := cache2.GetMaxPostId(c)
2022-10-07 14:27:34 +00:00
logs.ErrPrintln(err, "get max post id")
if ID > maxId || err != nil {
return
}
2023-01-12 12:42:16 +00:00
post, err := cache2.GetPostById(c, ID)
2022-10-07 14:27:34 +00:00
if post.Id == 0 || err != nil {
return
}
2023-01-12 12:42:16 +00:00
plugins.PasswordProjectTitle(&post)
comments, err := cache2.PostComments(c, post.Id)
2022-10-07 14:27:34 +00:00
if err != nil {
return
}
rs := templateRss
rs.Title = fmt.Sprintf("《%s》的评论", post.PostTitle)
2022-11-17 08:29:39 +00:00
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)
2022-10-08 08:37:27 +00:00
rs.LastBuildDate = time.Now().Format(timeFormat)
2022-10-07 14:27:34 +00:00
if post.PostPassword != "" {
if len(comments) > 0 {
2023-01-12 12:42:16 +00:00
plugins.PasswdProjectContent(&post)
2022-10-07 14:27:34 +00:00
t := comments[len(comments)-1]
rs.Items = []rss2.Item{
{
Title: fmt.Sprintf("评价者:%s", t.CommentAuthor),
2022-11-17 08:29:39 +00:00
Link: fmt.Sprintf("%s/p/%d#comment-%d", wpconfig.Options.Value("siteurl"), post.Id, t.CommentId),
2022-10-07 14:27:34 +00:00
Creator: t.CommentAuthor,
2022-10-08 08:37:27 +00:00
PubDate: t.CommentDateGmt.Format(timeFormat),
2022-10-07 14:27:34 +00:00
Guid: fmt.Sprintf("%s#comment-%d", post.Guid, t.CommentId),
Description: "评论受保护:要查看请输入密码。",
Content: post.PostContent,
},
}
}
} else {
2023-01-13 06:04:32 +00:00
rs.Items = helper.SliceMap(comments, func(t models2.Comments) rss2.Item {
2022-10-07 14:27:34 +00:00
return rss2.Item{
Title: fmt.Sprintf("评价者:%s", t.CommentAuthor),
2022-11-17 08:29:39 +00:00
Link: fmt.Sprintf("%s/p/%d#comment-%d", wpconfig.Options.Value("siteurl"), post.Id, t.CommentId),
2022-10-07 14:27:34 +00:00
Creator: t.CommentAuthor,
2022-10-08 08:37:27 +00:00
PubDate: t.CommentDateGmt.Format(timeFormat),
2022-10-07 14:27:34 +00:00
Guid: fmt.Sprintf("%s#comment-%d", post.Guid, t.CommentId),
Content: t.CommentContent,
}
})
}
x = rs.GetXML()
return
}
2022-10-08 06:01:05 +00:00
func CommentsFeed(c *gin.Context) {
2022-11-08 09:41:01 +00:00
if !isCacheExpired(c, commentsFeedCache.GetLastSetTime()) {
2022-10-08 06:01:05 +00:00
c.Status(http.StatusNotModified)
} else {
r, err := commentsFeedCache.GetCache(c, time.Second, c)
if err != nil {
c.Status(http.StatusInternalServerError)
c.Abort()
c.Error(err)
return
}
2022-11-08 09:41:01 +00:00
setFeed(r[0], c, commentsFeedCache.GetLastSetTime())
2022-10-08 06:01:05 +00:00
}
}
func commentsFeed(args ...any) (r []string, err error) {
c := args[0].(*gin.Context)
2023-01-12 12:42:16 +00:00
commens := cache2.RecentComments(c, 10)
2022-10-08 06:01:05 +00:00
rs := templateRss
2022-11-17 08:29:39 +00:00
rs.Title = fmt.Sprintf("\"%s\"的评论", wpconfig.Options.Value("blogname"))
2022-10-08 08:37:27 +00:00
rs.LastBuildDate = time.Now().Format(timeFormat)
2022-11-17 08:29:39 +00:00
rs.AtomLink = fmt.Sprintf("%s/comments/feed", wpconfig.Options.Value("siteurl"))
2023-01-13 06:04:32 +00:00
com, err := cache2.GetCommentByIds(c, helper.SliceMap(commens, func(t models2.Comments) uint64 {
2022-10-08 06:01:05 +00:00
return t.CommentId
}))
if nil != err {
return []string{}, err
}
2023-01-13 06:04:32 +00:00
rs.Items = helper.SliceMap(com, func(t models2.Comments) rss2.Item {
2023-01-12 12:42:16 +00:00
post, _ := cache2.GetPostById(c, t.CommentPostId)
plugins.PasswordProjectTitle(&post)
2022-10-08 06:01:05 +00:00
desc := "评论受保护:要查看请输入密码。"
content := t.CommentContent
if post.PostPassword != "" {
2023-01-12 12:42:16 +00:00
plugins.PasswdProjectContent(&post)
2022-10-08 06:01:05 +00:00
content = post.PostContent
} else {
2023-01-12 12:42:16 +00:00
desc = digest.ClearHtml(t.CommentContent)
2022-10-08 08:37:27 +00:00
content = desc
2022-10-08 06:01:05 +00:00
}
return rss2.Item{
2022-10-08 08:37:27 +00:00
Title: fmt.Sprintf("%s对《%s》的评论", t.CommentAuthor, post.PostTitle),
2022-11-17 08:29:39 +00:00
Link: fmt.Sprintf("%s/p/%d#comment-%d", wpconfig.Options.Value("siteurl"), post.Id, t.CommentId),
2022-10-08 06:01:05 +00:00
Creator: t.CommentAuthor,
Description: desc,
2022-10-08 08:37:27 +00:00
PubDate: t.CommentDateGmt.Format(timeFormat),
2022-10-08 06:01:05 +00:00
Guid: fmt.Sprintf("%s#commment-%d", post.Guid, t.CommentId),
Content: content,
}
})
r = []string{rs.GetXML()}
return
}