diff --git a/jobs/search-index/main.go b/jobs/search-index/main.go new file mode 100644 index 00000000..e81b7134 --- /dev/null +++ b/jobs/search-index/main.go @@ -0,0 +1,56 @@ +package main + +import ( + "strings" + + "github.com/aerogo/aero" + "github.com/animenotifier/arn" +) + +func main() { + aero.Parallel(updateAnimeIndex, updateUserIndex) +} + +func updateAnimeIndex() { + animeSearchIndex := arn.NewSearchIndex() + + // Anime + animeStream, err := arn.AllAnime() + + if err != nil { + panic(err) + } + + for anime := range animeStream { + animeSearchIndex.TextToID[strings.ToLower(anime.Title.Canonical)] = anime.ID + } + + // Save in database + err = arn.DB.Set("SearchIndex", "Anime", animeSearchIndex) + + if err != nil { + panic(err) + } +} + +func updateUserIndex() { + userSearchIndex := arn.NewSearchIndex() + + // Users + userStream, err := arn.AllUsers() + + if err != nil { + panic(err) + } + + for user := range userStream { + userSearchIndex.TextToID[strings.ToLower(user.Nick)] = user.ID + } + + // Save in database + err = arn.DB.Set("SearchIndex", "User", userSearchIndex) + + if err != nil { + panic(err) + } +} diff --git a/pages/search/search.go b/pages/search/search.go index ff0f10e4..6266698a 100644 --- a/pages/search/search.go +++ b/pages/search/search.go @@ -8,49 +8,8 @@ import ( "github.com/animenotifier/notify.moe/components" ) -const maxUsers = 18 -const maxAnime = 18 - -type AnimeID = string -type UserID = string - -var animeSearchIndex = make(map[string]AnimeID) -var userSearchIndex = make(map[string]UserID) - -func init() { - updateSearchIndex() -} - -func updateSearchIndex() { - updateAnimeIndex() - updateUserIndex() -} - -func updateAnimeIndex() { - // Anime - animeStream, err := arn.AllAnime() - - if err != nil { - panic(err) - } - - for anime := range animeStream { - animeSearchIndex[strings.ToLower(anime.Title.Canonical)] = anime.ID - } -} - -func updateUserIndex() { - // Users - userStream, err := arn.AllUsers() - - if err != nil { - panic(err) - } - - for user := range userStream { - userSearchIndex[strings.ToLower(user.Nick)] = user.ID - } -} +const maxUsers = 9 * 7 +const maxAnime = 9 * 7 // Get search page. func Get(ctx *aero.Context) string { @@ -59,10 +18,20 @@ func Get(ctx *aero.Context) string { var users []*arn.User var animeResults []*arn.Anime + // Search everything in parallel aero.Parallel(func() { - for name, id := range userSearchIndex { + // Search users + var user *arn.User + + userSearchIndex, err := arn.GetSearchIndex("User") + + if err != nil { + return + } + + for name, id := range userSearchIndex.TextToID { if strings.Index(name, term) != -1 { - user, err := arn.GetUser(id) + user, err = arn.GetUser(id) if err != nil { continue @@ -76,9 +45,18 @@ func Get(ctx *aero.Context) string { } } }, func() { - for title, id := range animeSearchIndex { + // Search anime + var anime *arn.Anime + + animeSearchIndex, err := arn.GetSearchIndex("Anime") + + if err != nil { + return + } + + for title, id := range animeSearchIndex.TextToID { if strings.Index(title, term) != -1 { - anime, err := arn.GetAnime(id) + anime, err = arn.GetAnime(id) if err != nil { continue diff --git a/scripts/actions.ts b/scripts/actions.ts index 4648ee50..60053f5e 100644 --- a/scripts/actions.ts +++ b/scripts/actions.ts @@ -21,7 +21,14 @@ export function search(arn: AnimeNotifier, search: HTMLInputElement, e: Keyboard return } - arn.app.content.innerHTML = "

" + term + "

" + var results = arn.app.find("results") + + if(!results) { + results = document.createElement("div") + results.id = "results" + arn.app.content.innerHTML = "" + arn.app.content.appendChild(results) + } arn.app.get("/_/search/" + encodeURI(term)) .then(html => { @@ -29,7 +36,7 @@ export function search(arn: AnimeNotifier, search: HTMLInputElement, e: Keyboard return } - arn.app.find("results").innerHTML = html + results.innerHTML = html arn.app.emit("DOMContentLoaded") }) }