Skip to content

Commit

Permalink
Fixed crash in highlights, missing recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
svera authored Mar 26, 2024
1 parent 61c32ba commit 4fd08de
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 22 deletions.
21 changes: 12 additions & 9 deletions internal/index/bleve_read.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,12 @@ func (b *BleveIndexer) SameSubjects(slugID string, quantity int) ([]Document, er
subjectsCompoundQuery.AddQuery(qu)
}

series := strings.ReplaceAll(slug.Make(doc.Series), "-", "")
sq := bleve.NewTermQuery(series)
sq.SetField("SeriesEq")
bq.AddMustNot(sq)
if doc.Series != "" {
series := strings.ReplaceAll(slug.Make(doc.Series), "-", "")
sq := bleve.NewTermQuery(series)
sq.SetField("SeriesEq")
bq.AddMustNot(sq)
}

bq.AddMust(subjectsCompoundQuery)
bq.AddMustNot(bleve.NewDocIDQuery([]string{doc.ID}))
Expand Down Expand Up @@ -330,11 +332,12 @@ func (b *BleveIndexer) SameAuthors(slugID string, quantity int) ([]Document, err
bq.AddMust(authorsCompoundQuery)
bq.AddMustNot(bleve.NewDocIDQuery([]string{doc.ID}))

series := strings.ReplaceAll(slug.Make(doc.Series), "-", "")
sq := bleve.NewTermQuery(series)
sq.SetField("SeriesEq")

bq.AddMustNot(sq)
if doc.Series != "" {
series := strings.ReplaceAll(slug.Make(doc.Series), "-", "")
sq := bleve.NewTermQuery(series)
sq.SetField("SeriesEq")
bq.AddMustNot(sq)
}

return b.runQuery(bq, quantity)
}
Expand Down
1 change: 1 addition & 0 deletions internal/webserver/controller/document/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type highlightsRepository interface {
Highlights(userID int, page int, resultsPerPage int) (result.Paginated[[]string], error)
Highlighted(userID int, doc index.Document) index.Document
HighlightedPaginatedResult(userID int, results result.Paginated[[]index.Document]) result.Paginated[[]index.Document]
RemoveDocument(documentPath string) error
}

type Config struct {
Expand Down
4 changes: 4 additions & 0 deletions internal/webserver/controller/document/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ func (d *Controller) Delete(c *fiber.Ctx) error {
log.Printf("error removing file %s", fullPath)
}

if err := d.hlRepository.RemoveDocument(document.ID); err != nil {
log.Printf("error removing file %s from highlights", document.ID)
}

return nil
}
5 changes: 4 additions & 1 deletion internal/webserver/controller/highlight/highlights.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ func (h *Controller) Highlights(c *fiber.Ctx) error {
}

docsSortedByHighlightedDate := make([]index.Document, len(docs))
for i, path := range highlights.Hits() {

i := 0
for path := range docs {
docsSortedByHighlightedDate[i] = docs[path]
docsSortedByHighlightedDate[i].Highlighted = true
i++
}

paginatedResults := result.NewPaginated[[]index.Document](
Expand Down
2 changes: 1 addition & 1 deletion internal/webserver/embedded/css/display.css
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ a.collapse-control.collapsed:after {
}
}

.card-img-overlay a, .card-img-overlay p, .card-img-overlay h2, .card-img-overlay h3 {
.card-img-overlay a, .card-img-overlay p, .card-img-overlay h2, .card-img-overlay h3, .card-img-overlay h4, .card-img-overlay h5 {
color: rgba(255,255,255,0.8);
}
75 changes: 64 additions & 11 deletions internal/webserver/highlights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,32 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/svera/coreander/v3/internal/webserver/infrastructure"
"github.com/svera/coreander/v3/internal/webserver/model"
"gorm.io/gorm"
)

func TestHighlights(t *testing.T) {
db := infrastructure.Connect("file::memory:", 250)
var (
db *gorm.DB
app *fiber.App
adminCookie *http.Cookie
data url.Values
adminUser model.User
)

func setup(t *testing.T) {
var err error

db = infrastructure.Connect("file::memory:", 250)
appFS := loadFilesInMemoryFs([]string{"fixtures/library/metadata.epub"})
app := bootstrapApp(db, &infrastructure.NoEmail{}, appFS)
data := url.Values{
app = bootstrapApp(db, &infrastructure.NoEmail{}, appFS)
adminCookie, err = login(app, "[email protected]", "admin")
if err != nil {
t.Fatalf("Unexpected error: %v", err.Error())
}

data = url.Values{
"slug": {"john-doe-test-epub"},
}
adminUser := model.User{}
adminUser = model.User{}
db.Where("email = ?", "[email protected]").First(&adminUser)

regularUserData := url.Values{
Expand All @@ -32,17 +48,15 @@ func TestHighlights(t *testing.T) {
"words-per-minute": {"250"},
}

adminCookie, err := login(app, "[email protected]", "admin")
if err != nil {
t.Fatalf("Unexpected error: %v", err.Error())
}

response, err := postRequest(regularUserData, adminCookie, app, "/en/users/new")
if response == nil {
t.Fatalf("Unexpected error: %v", err.Error())
}
}

func TestHighlights(t *testing.T) {
t.Run("Try to highlight a document without an active session", func(t *testing.T) {
setup(t)
response, err := highlight(&http.Cookie{}, app, strings.NewReader(data.Encode()), fiber.MethodPost)
if err != nil {
t.Fatalf("Unexpected error: %v", err.Error())
Expand All @@ -52,6 +66,7 @@ func TestHighlights(t *testing.T) {
})

t.Run("Try to highlight and dehighlight a document with an active session", func(t *testing.T) {
setup(t)
response, err := highlight(adminCookie, app, strings.NewReader(data.Encode()), fiber.MethodPost)
if err != nil {
t.Fatalf("Unexpected error: %v", err.Error())
Expand All @@ -71,7 +86,45 @@ func TestHighlights(t *testing.T) {
assertHighlights(app, t, adminCookie, adminUser.Uuid, 0)
})

t.Run("Deleting a document also removes it from the highlights of all users", func(t *testing.T) {
setup(t)
regularUser := model.User{}
db.Where("email = ?", "[email protected]").First(&regularUser)

regularUserCookie, err := login(app, "[email protected]", "test")
if err != nil {
t.Fatalf("Unexpected error: %v", err.Error())
}

response, err := highlight(regularUserCookie, app, strings.NewReader(data.Encode()), fiber.MethodPost)
if err != nil {
t.Fatalf("Unexpected error: %v", err.Error())
}

mustReturnStatus(response, fiber.StatusOK, t)

assertHighlights(app, t, regularUserCookie, regularUser.Uuid, 1)

adminCookie, err = login(app, "[email protected]", "admin")
if err != nil {
t.Fatalf("Unexpected error: %v", err.Error())
}

_, err = postRequest(data, adminCookie, app, "/delete")
if err != nil {
t.Fatalf("Unexpected error: %v", err.Error())
}

var total int64
db.Table("highlights").Where("user_id = ?", regularUser.ID).Count(&total)
if total != 0 {
t.Errorf("Expected no highlights in DB for user, got %d", total)
}
assertHighlights(app, t, adminCookie, regularUser.Uuid, 0)
})

t.Run("Deleting a user also remove his/her highlights", func(t *testing.T) {
setup(t)
regularUser := model.User{}
db.Where("email = ?", "[email protected]").First(&regularUser)

Expand All @@ -94,7 +147,7 @@ func TestHighlights(t *testing.T) {
t.Fatalf("Unexpected error: %v", err.Error())
}

data = url.Values{
data := url.Values{
"uuid": {regularUser.Uuid},
}

Expand Down
4 changes: 4 additions & 0 deletions internal/webserver/model/highlight_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,7 @@ func (u *HighlightRepository) Remove(userID int, documentPath string) error {
}
return u.DB.Delete(&highlight).Error
}

func (u *HighlightRepository) RemoveDocument(documentPath string) error {
return u.DB.Where("path = ?", documentPath).Delete(&Highlight{}).Error
}

0 comments on commit 4fd08de

Please sign in to comment.