Skip to content

Commit

Permalink
Ability to highlight documents
Browse files Browse the repository at this point in the history
  • Loading branch information
svera authored Oct 23, 2023
1 parent 63f09d9 commit cc00c9b
Show file tree
Hide file tree
Showing 38 changed files with 821 additions and 357 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ A personal documents server, Coreander indexes the documents (EPUBs and PDFs wit
Binaries for Windows 64 bit, Linux 32 and 64 bits for both X86 and ARM (Raspberry Pi and other SBCs) and Macs with Intel and Apple processors are available at [releases](https://github.com/svera/coreander/releases/latest). Just download and unzip the one appropriate for your system.

### Building from source
Coreander's only requirement is Go 1.18.
Coreander's only requirement is Go 1.21.

There are two possibilities for building Coreander from source:
* If you have [Mage](https://magefile.org) installed in your system, just type `mage install` from the source code folder.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/svera/coreander/v3

go 1.18
go 1.21

require (
github.com/blevesearch/bleve v1.0.14
Expand Down
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0=
Expand All @@ -308,6 +309,7 @@ github.com/google/pprof v0.0.0-20210601050228-01bbb1931b22/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20210609004039-a478d1d731e9/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26 h1:Xim43kblpZXfIBQsbuBVKCudVG457BR2GZFIz3uw3hQ=
github.com/google/pprof v0.0.0-20221118152302-e6195bd50e26/go.mod h1:dDKJzRmX4S37WGHujM7tX//fmj1uioxKzKxz3lo4HJo=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -544,6 +546,7 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/svera/epub v0.3.1 h1:wpXg8tIV4rOcj5xBeYAFybT4mDVX88VhnHQeoT784O8=
github.com/svera/epub v0.3.1/go.mod h1:AfxSPsCf8J28Tgqo8L0si2mN2ZFNB/fWkvBhNnqswZk=
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/svera/coreander/v3/internal/infrastructure"
"github.com/svera/coreander/v3/internal/model"
"golang.org/x/text/message"
"gorm.io/gorm"
)

type authRepository interface {
Expand Down Expand Up @@ -113,6 +114,9 @@ func (a *Auth) SignIn(c *fiber.Ctx) error {
// Send back JWT as a cookie.
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"userdata": model.User{
Model: gorm.Model{
ID: user.ID,
},
Name: user.Name,
Email: user.Email,
Role: user.Role,
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (

"github.com/gofiber/fiber/v2"
"github.com/spf13/afero"
"github.com/svera/coreander/v3/internal/index"
"github.com/svera/coreander/v3/internal/jwtclaimsreader"
"github.com/svera/coreander/v3/internal/model"
"github.com/svera/coreander/v3/internal/search"
)

type IdxWriter interface {
Document(ID string) (index.Document, error)
Document(ID string) (search.Document, error)
RemoveFile(file string) error
}

Expand Down
8 changes: 7 additions & 1 deletion internal/controller/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ import (
"github.com/gofiber/fiber/v2"
"github.com/svera/coreander/v3/internal/infrastructure"
"github.com/svera/coreander/v3/internal/jwtclaimsreader"
"github.com/svera/coreander/v3/internal/model"
"github.com/svera/coreander/v3/internal/search"
)

const relatedDocuments = 4

func Document(c *fiber.Ctx, libraryPath string, sender Sender, idx IdxReader, wordsPerMinute float64) error {
func Document(c *fiber.Ctx, libraryPath string, sender Sender, idx IdxReader, wordsPerMinute float64, highlights model.HighlightRepository) error {
emailSendingConfigured := true
if _, ok := sender.(*infrastructure.NoEmail); ok {
emailSendingConfigured = false
Expand Down Expand Up @@ -56,6 +58,10 @@ func Document(c *fiber.Ctx, libraryPath string, sender Sender, idx IdxReader, wo
fmt.Println(err)
}

if session.ID > 0 {
document = highlights.Highlighted(int(session.ID), []search.Document{document})[0]
}

return c.Render("document", fiber.Map{
"Lang": lang,
"Title": title,
Expand Down
114 changes: 114 additions & 0 deletions internal/controller/highlights.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package controller

import (
"strconv"

"github.com/gofiber/fiber/v2"
"github.com/svera/coreander/v3/internal/infrastructure"
"github.com/svera/coreander/v3/internal/jwtclaimsreader"
"github.com/svera/coreander/v3/internal/model"
"github.com/svera/coreander/v3/internal/search"
)

type highlightsRepository interface {
Highlights(userID int, page int, resultsPerPage int) (search.PaginatedResult, error)
Highlight(userID int, documentPath string) error
Remove(userID int, documentPath string) error
}

type Highlights struct {
hlRepository highlightsRepository
usrRepository usersRepository
idx IdxReader
sender Sender
wordsPerMinute float64
}

func NewHighlights(hlRepository highlightsRepository, usrRepository usersRepository, sender Sender, wordsPerMinute float64, idx IdxReader) *Highlights {
return &Highlights{
hlRepository: hlRepository,
usrRepository: usrRepository,
idx: idx,
sender: sender,
wordsPerMinute: wordsPerMinute,
}
}

func (h *Highlights) Highlights(c *fiber.Ctx) error {
emailSendingConfigured := true
if _, ok := h.sender.(*infrastructure.NoEmail); ok {
emailSendingConfigured = false
}

page, err := strconv.Atoi(c.Query("page"))
if err != nil {
page = 1
}

session := jwtclaimsreader.SessionData(c)
if session.WordsPerMinute > 0 {
h.wordsPerMinute = session.WordsPerMinute
}

user, err := h.usrRepository.FindByUuid(c.Params("uuid"))
if err != nil {
return fiber.ErrBadRequest
}

highlights, err := h.hlRepository.Highlights(int(user.ID), page, model.ResultsPerPage)
if err != nil {
return fiber.ErrInternalServerError
}
for i, highlight := range highlights.Hits {
docs, err := h.idx.Documents([]string{highlight.ID})
if err != nil {
return fiber.ErrInternalServerError
}
highlights.Hits[i] = docs[0]
highlights.Hits[i].Highlighted = true
}

return c.Render("highlights", fiber.Map{
"Results": highlights.Hits,
"Total": highlights.TotalHits,
"Paginator": pagination(model.MaxPagesNavigator, highlights.TotalPages, page, nil),
"Title": "Highlights",
"Version": c.App().Config().AppName,
"EmailSendingConfigured": emailSendingConfigured,
"EmailFrom": h.sender.From(),
"Session": session,
"WordsPerMinute": h.wordsPerMinute,
}, "layout")
}

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

user, err := h.usrRepository.FindByUuid(session.Uuid)
if err != nil {
return fiber.ErrBadRequest
}

document, err := h.idx.Document(c.FormValue("slug"))
if err != nil {
return fiber.ErrBadRequest
}

return h.hlRepository.Highlight(int(user.ID), document.ID)
}

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

user, err := h.usrRepository.FindByUuid(session.Uuid)
if err != nil {
return fiber.ErrBadRequest
}

document, err := h.idx.Document(c.FormValue("slug"))
if err != nil {
return fiber.ErrBadRequest
}

return h.hlRepository.Remove(int(user.ID), document.ID)
}
21 changes: 13 additions & 8 deletions internal/controller/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"strconv"

"github.com/gofiber/fiber/v2"
"github.com/svera/coreander/v3/internal/index"
"github.com/svera/coreander/v3/internal/infrastructure"
"github.com/svera/coreander/v3/internal/jwtclaimsreader"
"github.com/svera/coreander/v3/internal/model"
"github.com/svera/coreander/v3/internal/search"
)

type Sender interface {
Expand All @@ -17,16 +17,17 @@ type Sender interface {

// IdxReader defines a set of reading operations over an index
type IdxReader interface {
Search(keywords string, page, resultsPerPage int) (*index.PaginatedResult, error)
Search(keywords string, page, resultsPerPage int) (*search.PaginatedResult, error)
Count() (uint64, error)
Close() error
Document(ID string) (index.Document, error)
SameSubjects(slug string, quantity int) ([]index.Document, error)
SameAuthors(slug string, quantity int) ([]index.Document, error)
SameSeries(slug string, quantity int) ([]index.Document, error)
Document(Slug string) (search.Document, error)
Documents(IDs []string) ([]search.Document, error)
SameSubjects(slug string, quantity int) ([]search.Document, error)
SameAuthors(slug string, quantity int) ([]search.Document, error)
SameSeries(slug string, quantity int) ([]search.Document, error)
}

func Search(c *fiber.Ctx, idx IdxReader, sender Sender, wordsPerMinute float64) error {
func Search(c *fiber.Ctx, idx IdxReader, sender Sender, wordsPerMinute float64, highlights model.HighlightRepository) error {
emailSendingConfigured := true
if _, ok := sender.(*infrastructure.NoEmail); ok {
emailSendingConfigured = false
Expand All @@ -42,13 +43,17 @@ func Search(c *fiber.Ctx, idx IdxReader, sender Sender, wordsPerMinute float64)
wordsPerMinute = session.WordsPerMinute
}

var searchResults *index.PaginatedResult
var searchResults *search.PaginatedResult

if keywords := c.Query("search"); keywords != "" {
if searchResults, err = idx.Search(keywords, page, model.ResultsPerPage); err != nil {
return fiber.ErrInternalServerError
}

if session.ID > 0 {
searchResults.Hits = highlights.Highlighted(int(session.ID), searchResults.Hits)
}

return c.Render("results", fiber.Map{
"Keywords": keywords,
"Results": searchResults.Hits,
Expand Down
Loading

0 comments on commit cc00c9b

Please sign in to comment.