| -rw-r--r-- | internal/router/api-v1-like.go | 15 | ||||
| -rw-r--r-- | internal/router/client-cache.go | 7 | ||||
| -rw-r--r-- | locale/localization.en.yaml | 1 | ||||
| -rw-r--r-- | locale/localization.go | 1 | ||||
| -rw-r--r-- | locale/localization.ru.yaml | 1 | ||||
| -rw-r--r-- | main.go | 4 | ||||
| -rw-r--r-- | views/partials/blog-page-like-button.html | 5 |
7 files changed, 9 insertions, 25 deletions
diff --git a/internal/router/api-v1-like.go b/internal/router/api-v1-like.go index 327dafa..1732c77 100644 --- a/internal/router/api-v1-like.go +++ b/internal/router/api-v1-like.go @@ -8,7 +8,6 @@ import ( "strings" "github.com/SayaAndy/saya-today-web/internal/b2" - "github.com/SayaAndy/saya-today-web/locale" "github.com/gofiber/fiber/v2" ) @@ -16,7 +15,7 @@ func init() { tm.Add("blog-page-like-button", "views/partials/blog-page-like-button.html") } -func Api_V1_Like_Put(l map[string]*locale.LocaleConfig, b2 *b2.B2Client) func(c *fiber.Ctx) error { +func Api_V1_Like_Put(b2 *b2.B2Client) func(c *fiber.Ctx) error { return func(c *fiber.Ctx) error { c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8) @@ -42,12 +41,12 @@ func Api_V1_Like_Put(l map[string]*locale.LocaleConfig, b2 *b2.B2Client) func(c 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 { @@ -57,9 +56,7 @@ func Api_V1_Like_Put(l map[string]*locale.LocaleConfig, b2 *b2.B2Client) func(c 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{ - "L": l[lang], - "Liked": newLikeStatus, - "LikedCount": CCache.GetLikeCount(page), + "Liked": newLikeStatus, }) if err != nil { slog.Warn("failed to generate div", slog.String("path", path), slog.String("error", err.Error())) @@ -73,7 +70,7 @@ func Api_V1_Like_Put(l map[string]*locale.LocaleConfig, b2 *b2.B2Client) func(c } } -func Api_V1_Like_Get(l map[string]*locale.LocaleConfig, b2 *b2.B2Client) func(c *fiber.Ctx) error { +func Api_V1_Like_Get(b2 *b2.B2Client) func(c *fiber.Ctx) error { return func(c *fiber.Ctx) error { c.Set(fiber.HeaderContentType, fiber.MIMETextPlainCharsetUTF8) @@ -105,9 +102,7 @@ func Api_V1_Like_Get(l map[string]*locale.LocaleConfig, b2 *b2.B2Client) func(c 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{ - "L": l[lang], - "Liked": likeStatus, - "LikedCount": CCache.GetLikeCount(page), + "Liked": likeStatus, }) 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 7d276b1..3104a66 100644 --- a/internal/router/client-cache.go +++ b/internal/router/client-cache.go @@ -58,13 +58,6 @@ 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{} diff --git a/locale/localization.en.yaml b/locale/localization.en.yaml index 48ef9da..f5ee6c9 100644 --- a/locale/localization.en.yaml +++ b/locale/localization.en.yaml @@ -9,4 +9,3 @@ BlogSearch: ChooseAllTags: 'Choose All' GlobalMap: Header: 'Global Map' -LikeButton: 'Like!' diff --git a/locale/localization.go b/locale/localization.go index b557e08..56b6462 100644 --- a/locale/localization.go +++ b/locale/localization.go @@ -11,7 +11,6 @@ type LocaleConfig struct { TagsLabel string `yaml:"TagsLabel" json:"TagsLabel"` BlogSearch BlogSearchConfig `yaml:"BlogSearch" json:"BlogSearch"` GlobalMap GlobalMapConfig `yaml:"GlobalMap" json:"GlobalMap"` - LikeButton string `yaml:"LikeButton" json:"LikeButton"` } type BlogSearchConfig struct { diff --git a/locale/localization.ru.yaml b/locale/localization.ru.yaml index d6e9fe4..c236d1e 100644 --- a/locale/localization.ru.yaml +++ b/locale/localization.ru.yaml @@ -9,4 +9,3 @@ BlogSearch: ChooseAllTags: 'Выбрать все' GlobalMap: Header: 'Глобальная карта' -LikeButton: 'Нраица!' @@ -88,8 +88,8 @@ func main() { app.Get("/:lang/blog/:title", router.Lang_Blog_Title(localization, availableLanguages, b2Client, md)) app.Get("/api/v1/tz", router.Api_V1_TZ()) app.Get("/api/v1/blog-search", router.Api_V1_BlogSearch(localization, availableLanguages, b2Client)) - app.Get("/api/v1/like", router.Api_V1_Like_Get(localization, b2Client)) - app.Put("/api/v1/like", router.Api_V1_Like_Put(localization, b2Client)) + app.Get("/api/v1/like", router.Api_V1_Like_Get(b2Client)) + app.Put("/api/v1/like", router.Api_V1_Like_Put(b2Client)) app.Static("/", "./static") diff --git a/views/partials/blog-page-like-button.html b/views/partials/blog-page-like-button.html index afc3027..50a9d7a 100644 --- a/views/partials/blog-page-like-button.html +++ b/views/partials/blog-page-like-button.html @@ -1,7 +1,6 @@ <button hx-put="/api/v1/like" hx-vals='{"like": {{ not .Liked }}}' hx-target="this" hx-swap="outerHTML" hx-trigger="click" - class="text-[0.8vmax]/[0.9] font-spectral text-left px-[0.4vmax] py-[0.2vmax] {{ if .Liked }}bg-main-light hover:bg-main-medium text-background-dark border-inset{{ else }}bg-main-dark hover:bg-main-medium text-background-light border-outset{{ end }} cursor-pointer border-[0.2vmax] border-background-dark"> + class="text-[0.8vmax]/[0.9] font-spectral text-left px-[0.4vmax] py-[0.2vmax] {{ if .Liked }}bg-main-light hover:bg-main-medium{{ else }}bg-main-dark hover:bg-main-medium{{ end }} text-background-dark cursor-pointer {{ if .Liked }}border-inset{{ else }}border-outset{{ end }} border-[0.3vmax] border-background-dark"> <i class="fas fa-thumbs-up w-[0.8vmax] h-[0.8vmax] mr-[0.4vmax]"></i> - <span>{{ .L.LikeButton }}</span> - <span class="italic ml-[0.2vmax] text-background-light">({{ .LikedCount }})</span> + <span>Нраица!</span> </button>
\ No newline at end of file |