Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed highlight button not working in document detail #82

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions internal/webserver/controller/highlight/highlight.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package highlight

import (
"github.com/gofiber/fiber/v2"
"github.com/svera/coreander/v3/internal/webserver/jwtclaimsreader"
"github.com/svera/coreander/v3/internal/webserver/model"
)

func (h *Controller) Highlight(c *fiber.Ctx) error {
session := jwtclaimsreader.SessionData(c)

user, err := h.usrRepository.FindByUuid(session.Uuid)
if err != nil {
return fiber.ErrBadRequest
}
user := c.Locals("Session").(model.User)

document, err := h.idx.Document(c.FormValue("slug"))
if err != nil {
Expand Down
9 changes: 2 additions & 7 deletions internal/webserver/controller/highlight/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@ package highlight

import (
"github.com/gofiber/fiber/v2"
"github.com/svera/coreander/v3/internal/webserver/jwtclaimsreader"
"github.com/svera/coreander/v3/internal/webserver/model"
)

func (h *Controller) Remove(c *fiber.Ctx) error {
session := jwtclaimsreader.SessionData(c)

user, err := h.usrRepository.FindByUuid(session.Uuid)
if err != nil {
return fiber.ErrBadRequest
}
user := c.Locals("Session").(model.User)

document, err := h.idx.Document(c.FormValue("slug"))
if err != nil {
Expand Down
5 changes: 3 additions & 2 deletions internal/webserver/embedded/views/document.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ <h3 class="card-text text-center mx-1 mt-3">
</a>

{{if and (.Session) (ne .Session.Name "")}}
<a href="/highlights" class='btn btn-outline-secondary highlight {{if .Document.Highlighted}}visually-hidden{{end}}' onclick='highlightToggle("{{.Document.Slug}}", this, "POST")'>
<a href="/highlights" class="btn btn-outline-secondary highlight {{if .Document.Highlighted}}visually-hidden{{end}}" data-slug="{{.Document.Slug}}" data-dehighlight="{{.OnDehighlight}}" data-method="POST">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-star" viewBox="0 0 16 16">
<path d="M2.866 14.85c-.078.444.36.791.746.593l4.39-2.256 4.389 2.256c.386.198.824-.149.746-.592l-.83-4.73 3.522-3.356c.33-.314.16-.888-.282-.95l-4.898-.696L8.465.792a.513.513 0 0 0-.927 0L5.354 5.12l-4.898.696c-.441.062-.612.636-.283.95l3.523 3.356-.83 4.73zm4.905-2.767-3.686 1.894.694-3.957a.565.565 0 0 0-.163-.505L1.71 6.745l4.052-.576a.525.525 0 0 0 .393-.288L8 2.223l1.847 3.658a.525.525 0 0 0 .393.288l4.052.575-2.906 2.77a.565.565 0 0 0-.163.506l.694 3.957-3.686-1.894a.503.503 0 0 0-.461 0z"/>
</svg>
&nbsp;&nbsp;{{t .Lang "Highlight"}}
</a>
<a href="/highlights" class='btn btn-outline-secondary dehighlight {{if not .Document.Highlighted}}visually-hidden{{end}}' onclick='highlightToggle("{{.Document.Slug}}", this, "DELETE")'>

<a href="/highlights" class="btn btn-outline-secondary dehighlight {{if not .Document.Highlighted}}visually-hidden{{end}}" data-slug="{{.Document.Slug}}" data-dehighlight="{{.OnDehighlight}}" data-method="DELETE">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-star-fill" viewBox="0 0 16 16">
<path d="M3.612 15.443c-.386.198-.824-.149-.746-.592l.83-4.73L.173 6.765c-.329-.314-.158-.888.283-.95l4.898-.696L7.538.792c.197-.39.73-.39.927 0l2.184 4.327 4.898.696c.441.062.612.636.282.95l-3.522 3.356.83 4.73c.078.443-.36.79-.746.592L8 13.187l-4.389 2.256z"/>
</svg>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package jwtclaimsreader
package webserver

import (
"github.com/gofiber/fiber/v2"
"github.com/golang-jwt/jwt/v4"
"github.com/svera/coreander/v3/internal/webserver/model"
)

func SessionData(c *fiber.Ctx) model.User {
func sessionData(c *fiber.Ctx) model.User {
var user model.User
if t, ok := c.Locals("user").(*jwt.Token); ok {
claims := t.Claims.(jwt.MapClaims)
Expand Down
5 changes: 2 additions & 3 deletions internal/webserver/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/gofiber/fiber/v2"
jwtware "github.com/gofiber/jwt/v3"
"github.com/svera/coreander/v3/internal/webserver/infrastructure"
"github.com/svera/coreander/v3/internal/webserver/jwtclaimsreader"
"github.com/svera/coreander/v3/internal/webserver/model"
)

Expand Down Expand Up @@ -79,7 +78,7 @@ func AlwaysRequireAuthentication(jwtSecret []byte, sender Sender) func(*fiber.Ct
SigningMethod: "HS256",
TokenLookup: "cookie:coreander",
SuccessHandler: func(c *fiber.Ctx) error {
c.Locals("Session", jwtclaimsreader.SessionData(c))
c.Locals("Session", sessionData(c))
return c.Next()
},
ErrorHandler: func(c *fiber.Ctx, err error) error {
Expand All @@ -95,7 +94,7 @@ func ConfigurableAuthentication(jwtSecret []byte, sender Sender, requireAuth boo
SigningMethod: "HS256",
TokenLookup: "cookie:coreander",
SuccessHandler: func(c *fiber.Ctx) error {
c.Locals("Session", jwtclaimsreader.SessionData(c))
c.Locals("Session", sessionData(c))
return c.Next()
},
ErrorHandler: func(c *fiber.Ctx, err error) error {
Expand Down
2 changes: 0 additions & 2 deletions internal/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/svera/coreander/v3/internal/i18n"
"github.com/svera/coreander/v3/internal/index"
"github.com/svera/coreander/v3/internal/webserver/infrastructure"
"github.com/svera/coreander/v3/internal/webserver/jwtclaimsreader"
"golang.org/x/exp/slices"
"golang.org/x/text/message"
)
Expand Down Expand Up @@ -182,7 +181,6 @@ func errorHandler(c *fiber.Ctx, err error) error {
fiber.Map{
"Lang": chooseBestLanguage(c),
"Title": "Coreander",
"Session": jwtclaimsreader.SessionData(c),
"Version": c.App().Config().AppName,
},
"layout")
Expand Down