summaryrefslogtreecommitdiffci
path: root/internal
diff options
from:
to:
context:
space:
mode:
authorGravatar SayaAndy <saya.andy@posteo.com> 2025-08-27 19:11:16 +0700
committerGravatar SayaAndy <saya.andy@posteo.com> 2025-08-27 19:11:16 +0700
commit26c77e33356a519b3caaed44fd9d7f8722f41918 (patch)
tree467bc15626f59cf69846fe36c0e2786a30e383d2 /internal
parentea8c6da9b8bafa1d4e5fe5a77fb306006db7d6f6 (diff)
downloadweb-26c77e33356a519b3caaed44fd9d7f8722f41918.tar.gz
web-26c77e33356a519b3caaed44fd9d7f8722f41918.zip
feat: add and show like count
feat: localize like button
Diffstat (limited to 'internal')
-rw-r--r--internal/router/api-v1-like.go15
-rw-r--r--internal/router/client-cache.go7
2 files changed, 17 insertions, 5 deletions
diff --git a/internal/router/api-v1-like.go b/internal/router/api-v1-like.go
index 1732c77..327dafa 100644
--- a/internal/router/api-v1-like.go
+++ b/internal/router/api-v1-like.go
@@ -8,6 +8,7 @@ import (
"strings"
"github.com/SayaAndy/saya-today-web/internal/b2"
+ "github.com/SayaAndy/saya-today-web/locale"
"github.com/gofiber/fiber/v2"
)
@@ -15,7 +16,7 @@ func init() {
tm.Add("blog-page-like-button", "views/partials/blog-page-like-button.html")
}
-func Api_V1_Like_Put(b2 *b2.B2Client) func(c *fiber.Ctx) error {
+func Api_V1_Like_Put(l map[string]*locale.LocaleConfig, b2 *b2.B2Client) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
@@ -41,12 +42,12 @@ func Api_V1_Like_Put(b2 *b2.B2Client) func(c *fiber.Ctx) error {
return c.Status(fiber.ErrNotFound.Code).SendString(fmt.Sprintf("server did not find '%s' article", pageLink))
}
- ip := c.IP()
newLikeStatus, err := strconv.ParseBool(c.FormValue("like", "true"))
if err != nil {
return c.Status(fiber.ErrBadRequest.Code).SendString("invalid 'like' value")
}
+ ip := c.IP()
if newLikeStatus {
CCache.LikeOn(ip, page)
} else {
@@ -56,7 +57,9 @@ func Api_V1_Like_Put(b2 *b2.B2Client) func(c *fiber.Ctx) error {
slog.Debug("someone pressed the like button!", slog.String("ip", ip), slog.String("page", page), slog.String("new_like_status", fmt.Sprint(newLikeStatus)))
if c.Get("HX-Request", "false") == "true" {
content, err := tm.Render("blog-page-like-button", fiber.Map{
- "Liked": newLikeStatus,
+ "L": l[lang],
+ "Liked": newLikeStatus,
+ "LikedCount": CCache.GetLikeCount(page),
})
if err != nil {
slog.Warn("failed to generate div", slog.String("path", path), slog.String("error", err.Error()))
@@ -70,7 +73,7 @@ func Api_V1_Like_Put(b2 *b2.B2Client) func(c *fiber.Ctx) error {
}
}
-func Api_V1_Like_Get(b2 *b2.B2Client) func(c *fiber.Ctx) error {
+func Api_V1_Like_Get(l map[string]*locale.LocaleConfig, b2 *b2.B2Client) func(c *fiber.Ctx) error {
return func(c *fiber.Ctx) error {
c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8)
@@ -102,7 +105,9 @@ func Api_V1_Like_Get(b2 *b2.B2Client) func(c *fiber.Ctx) error {
slog.Debug("someone requested the like status!", slog.String("ip", ip), slog.String("page", page), slog.Bool("like_status", likeStatus))
if c.Get("HX-Request", "false") == "true" {
content, err := tm.Render("blog-page-like-button", fiber.Map{
- "Liked": likeStatus,
+ "L": l[lang],
+ "Liked": likeStatus,
+ "LikedCount": CCache.GetLikeCount(page),
})
if err != nil {
slog.Warn("failed to generate div", slog.String("path", path), slog.String("error", err.Error()))
diff --git a/internal/router/client-cache.go b/internal/router/client-cache.go
index 3104a66..7d276b1 100644
--- a/internal/router/client-cache.go
+++ b/internal/router/client-cache.go
@@ -58,6 +58,13 @@ func (c *ClientCache) GetLikeStatus(id string, page string) bool {
return ok
}
+func (c *ClientCache) GetLikeCount(page string) int {
+ if userSet, ok := c.likePageMap[page]; ok {
+ return len(userSet)
+ }
+ return 0
+}
+
func (c *ClientCache) LikeOn(id string, page string) (alreadyLiked bool) {
if _, ok := c.mutexLikeMap[id]; !ok {
c.mutexLikeMap[id] = &sync.Mutex{}