108 lines
2.5 KiB
Go
Raw Normal View History

package search
import (
2017-11-11 15:16:25 +00:00
"strings"
2018-03-17 19:41:18 +00:00
"github.com/aerogo/flow"
"github.com/aerogo/aero"
2017-06-20 20:54:45 +00:00
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
)
2018-03-17 21:09:17 +00:00
const (
maxUsers = 25
maxAnime = 25
maxPosts = 2
maxThreads = 2
maxSoundTracks = 3
maxCharacters = 22
maxCompanies = 3
)
2017-06-20 20:54:45 +00:00
2017-06-16 23:25:02 +00:00
// Get search page.
func Get(ctx *aero.Context) string {
2017-11-11 15:16:25 +00:00
term := ctx.Get("term")
term = strings.TrimPrefix(term, "/")
2017-06-20 20:54:45 +00:00
2018-03-17 21:09:17 +00:00
users, animes, posts, threads, tracks, characters, companies := arn.Search(
term,
maxUsers,
maxAnime,
maxPosts,
maxThreads,
maxSoundTracks,
maxCharacters,
maxCompanies,
)
return ctx.HTML(components.SearchResults(term, users, animes, posts, threads, tracks, characters, companies, nil))
}
2018-03-17 19:41:18 +00:00
// GetEmptySearch renders the search page with no contents.
func GetEmptySearch(ctx *aero.Context) string {
2018-03-17 21:09:17 +00:00
return ctx.HTML(components.SearchResults("", nil, nil, nil, nil, nil, nil, nil, nil))
2018-03-17 19:41:18 +00:00
}
// Anime search.
func Anime(ctx *aero.Context) string {
term := ctx.Get("term")
term = strings.TrimPrefix(term, "/")
animes := arn.SearchAnime(term, maxAnime)
return ctx.HTML(components.AnimeSearchResults(animes))
}
// Characters search.
func Characters(ctx *aero.Context) string {
term := ctx.Get("term")
term = strings.TrimPrefix(term, "/")
characters := arn.SearchCharacters(term, maxCharacters)
return ctx.HTML(components.CharacterSearchResults(characters))
}
// Forum search.
func Forum(ctx *aero.Context) string {
term := ctx.Get("term")
term = strings.TrimPrefix(term, "/")
var posts []*arn.Post
var threads []*arn.Thread
flow.Parallel(func() {
posts = arn.SearchPosts(term, maxPosts)
}, func() {
threads = arn.SearchThreads(term, maxThreads)
})
return ctx.HTML(components.ForumSearchResults(posts, threads))
}
// SoundTracks search.
func SoundTracks(ctx *aero.Context) string {
term := ctx.Get("term")
term = strings.TrimPrefix(term, "/")
tracks := arn.SearchSoundTracks(term, maxSoundTracks)
return ctx.HTML(components.SoundTrackSearchResults(tracks))
}
// Users search.
func Users(ctx *aero.Context) string {
term := ctx.Get("term")
term = strings.TrimPrefix(term, "/")
users := arn.SearchUsers(term, maxUsers)
return ctx.HTML(components.UserSearchResults(users))
}
2018-03-17 21:09:17 +00:00
// Companies search.
func Companies(ctx *aero.Context) string {
term := ctx.Get("term")
term = strings.TrimPrefix(term, "/")
companies := arn.SearchCompanies(term, maxCompanies)
return ctx.HTML(components.CompanySearchResults(companies))
}