summaryrefslogtreecommitdiffci
path: root/internal/router/handlers/api-v1-blog-search.go
diff refs
from: back
to: back
| flip
diff options
context:
space:
mode:
Diffstat (limited to 'internal/router/handlers/api-v1-blog-search.go')
-rw-r--r--internal/router/handlers/api-v1-blog-search.go61
1 files changed, 21 insertions, 40 deletions
diff --git a/internal/router/handlers/api-v1-blog-search.go b/internal/router/handlers/api-v1-blog-search.go
index b59f09a..a0316e2 100644
--- a/internal/router/handlers/api-v1-blog-search.go
+++ b/internal/router/handlers/api-v1-blog-search.go
@@ -1,16 +1,16 @@
package handlers
import (
- "cmp"
"encoding/json"
"fmt"
"log/slog"
"net/url"
"regexp"
"slices"
+ "strings"
"time"
- "github.com/SayaAndy/saya-today-web/internal/blog"
+ "github.com/SayaAndy/saya-today-web/internal/b2"
"github.com/SayaAndy/saya-today-web/internal/router"
"github.com/gofiber/fiber/v2"
)
@@ -44,17 +44,9 @@ func (r *BlogSearchHandler) ToValidateLang() router.LangSetting {
return router.InForm
}
-func (r *BlogSearchHandler) RateLimiter() *fiber.Handler {
- return &router.RateLimiterLoose
-}
-
func (r *BlogSearchHandler) Render(c *fiber.Ctx, supplements *router.Supplements, lang string, templateMap fiber.Map) (statusCode int, err error) {
sort := c.Query("sort")
tz := c.Query("tz")
- medley := c.Query("medley")
- highlight := c.Query("highlight")
- hideTags := c.QueryBool("hideTags", false)
- hidePublishedTime := c.QueryBool("hidePublishedTime", false)
loc, err := time.LoadLocation(tz)
if err != nil {
@@ -64,11 +56,11 @@ func (r *BlogSearchHandler) Render(c *fiber.Ctx, supplements *router.Supplements
cacheKey := "blog-search." + lang + ".pages-list"
- var pages []*blog.Page
+ var pages []*b2.BlogPage
if pagesBytes, ok := supplements.PageCache.Get(cacheKey); pagesBytes != nil || ok {
json.Unmarshal(pagesBytes, &pages)
} else {
- pages, err = supplements.BlogClient.Scan(lang + "/")
+ pages, err = supplements.B2Client.Scan(lang + "/")
if err != nil {
return fiber.StatusInternalServerError, fmt.Errorf("failed to scan pages for '%s' lang: %w", lang, err)
}
@@ -89,26 +81,19 @@ func (r *BlogSearchHandler) Render(c *fiber.Ctx, supplements *router.Supplements
for _, page := range pages {
for _, tag := range page.Metadata.Tags {
if len(tags) == 0 || slices.Contains(tags, tag) {
- if medley == "" || medley == page.Metadata.Medley {
- pageMeta = append(pageMeta, fiber.Map{
- "Link": page.Link,
- "ArticleLink": "/" + lang + "/blog/" + page.FileName,
- "Title": page.Metadata.Title,
- "PublishedTime": page.Metadata.PublishedTime.In(loc).Format("2006-01-02 15:04:05 -07:00"),
- "ActionDate": page.Metadata.ActionDate,
- "ShortDescription": page.Metadata.ShortDescription,
- "Thumbnail": page.Metadata.Thumbnail,
- "Tags": page.Metadata.Tags,
- "LikeCount": supplements.ClientCache.GetLikeCount(page.FileName),
- "Liked": supplements.ClientCache.GetLikeStatus(c.IP(), page.FileName),
- "ViewCount": supplements.ClientCache.GetViewCount(page.FileName),
- "Viewed": supplements.ClientCache.GetViewStatus(c.IP(), page.FileName),
- "Medley": page.Metadata.Medley,
- "MedleyPart": page.Metadata.MedleyPart,
- "ToHighlight": page.FileName == highlight,
- })
- break
- }
+ pageMeta = append(pageMeta, fiber.Map{
+ "Link": page.Link,
+ "ArticleLink": "/" + lang + "/blog/" + page.FileName,
+ "Title": page.Metadata.Title,
+ "PublishedTime": page.Metadata.PublishedTime.In(loc).Format("2006-01-02 15:04:05 -07:00"),
+ "ActionDate": page.Metadata.ActionDate,
+ "ShortDescription": page.Metadata.ShortDescription,
+ "Thumbnail": page.Metadata.Thumbnail,
+ "Tags": page.Metadata.Tags,
+ "LikeCount": supplements.ClientCache.GetLikeCount(page.FileName),
+ "ViewCount": supplements.ClientCache.GetViewCount(page.FileName),
+ })
+ break
}
}
}
@@ -116,13 +101,13 @@ func (r *BlogSearchHandler) Render(c *fiber.Ctx, supplements *router.Supplements
slices.SortFunc(pageMeta, func(a, b fiber.Map) int {
switch sort {
case "titleAsc":
- return cmp.Compare(a["Title"].(string), b["Title"].(string))
+ return strings.Compare(a["Title"].(string), b["Title"].(string))
case "titleDesc":
- return cmp.Compare(b["Title"].(string), a["Title"].(string))
+ return strings.Compare(b["Title"].(string), a["Title"].(string))
case "actionDateAsc":
- return cmp.Compare(a["ActionDate"].(string), b["ActionDate"].(string))
+ return strings.Compare(a["ActionDate"].(string), b["ActionDate"].(string))
case "actionDateDesc":
- return cmp.Compare(b["ActionDate"].(string), a["ActionDate"].(string))
+ return strings.Compare(b["ActionDate"].(string), a["ActionDate"].(string))
case "publicationDateAsc":
publishedTimeA, _ := time.Parse("2006-01-02 15:04:05 -07:00", a["PublishedTime"].(string))
publishedTimeB, _ := time.Parse("2006-01-02 15:04:05 -07:00", b["PublishedTime"].(string))
@@ -131,15 +116,11 @@ func (r *BlogSearchHandler) Render(c *fiber.Ctx, supplements *router.Supplements
publishedTimeA, _ := time.Parse("2006-01-02 15:04:05 -07:00", a["PublishedTime"].(string))
publishedTimeB, _ := time.Parse("2006-01-02 15:04:05 -07:00", b["PublishedTime"].(string))
return publishedTimeB.Compare(publishedTimeA)
- case "medley":
- return cmp.Compare(a["MedleyPart"].(int), b["MedleyPart"].(int))
}
return 0
})
templateMap["BlogPages"] = pageMeta
- templateMap["HideTags"] = hideTags
- templateMap["HidePublishedTime"] = hidePublishedTime
return fiber.StatusOK, nil
}