2016-11-22 03:34:59 +00:00
|
|
|
package search
|
|
|
|
|
|
|
|
import (
|
2017-11-11 15:16:25 +00:00
|
|
|
"strings"
|
|
|
|
|
2018-04-22 14:12:00 +00:00
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
|
|
|
|
2018-03-17 19:41:18 +00:00
|
|
|
"github.com/aerogo/flow"
|
|
|
|
|
2016-11-22 03:34:59 +00:00
|
|
|
"github.com/aerogo/aero"
|
2017-06-20 20:54:45 +00:00
|
|
|
"github.com/animenotifier/arn"
|
2018-04-19 20:01:28 +00:00
|
|
|
"github.com/animenotifier/arn/search"
|
2017-06-20 20:54:45 +00:00
|
|
|
"github.com/animenotifier/notify.moe/components"
|
2016-11-22 03:34:59 +00:00
|
|
|
)
|
|
|
|
|
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.
|
2016-11-22 03:34:59 +00:00
|
|
|
func Get(ctx *aero.Context) string {
|
2017-11-11 15:16:25 +00:00
|
|
|
term := ctx.Get("term")
|
|
|
|
term = strings.TrimPrefix(term, "/")
|
2018-04-22 14:12:00 +00:00
|
|
|
user := utils.GetUser(ctx)
|
2017-06-20 20:54:45 +00:00
|
|
|
|
2018-04-19 20:01:28 +00:00
|
|
|
users, animes, posts, threads, tracks, characters, companies := search.All(
|
2018-03-17 21:09:17 +00:00
|
|
|
term,
|
|
|
|
maxUsers,
|
|
|
|
maxAnime,
|
|
|
|
maxPosts,
|
|
|
|
maxThreads,
|
|
|
|
maxSoundTracks,
|
|
|
|
maxCharacters,
|
|
|
|
maxCompanies,
|
|
|
|
)
|
|
|
|
|
2018-04-22 14:12:00 +00:00
|
|
|
return ctx.HTML(components.SearchResults(term, users, animes, posts, threads, tracks, characters, companies, nil, user))
|
2016-11-22 03:34:59 +00:00
|
|
|
}
|
2018-03-17 19:41:18 +00:00
|
|
|
|
|
|
|
// GetEmptySearch renders the search page with no contents.
|
|
|
|
func GetEmptySearch(ctx *aero.Context) string {
|
2018-04-22 14:12:00 +00:00
|
|
|
return ctx.HTML(components.SearchResults("", nil, nil, nil, nil, nil, nil, nil, nil, utils.GetUser(ctx)))
|
2018-03-17 19:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Anime search.
|
|
|
|
func Anime(ctx *aero.Context) string {
|
|
|
|
term := ctx.Get("term")
|
|
|
|
term = strings.TrimPrefix(term, "/")
|
2018-04-23 08:30:25 +00:00
|
|
|
user := utils.GetUser(ctx)
|
2018-03-17 19:41:18 +00:00
|
|
|
|
2018-04-19 20:01:28 +00:00
|
|
|
animes := search.Anime(term, maxAnime)
|
2018-04-23 08:30:25 +00:00
|
|
|
return ctx.HTML(components.AnimeSearchResults(animes, user))
|
2018-03-17 19:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Characters search.
|
|
|
|
func Characters(ctx *aero.Context) string {
|
|
|
|
term := ctx.Get("term")
|
|
|
|
term = strings.TrimPrefix(term, "/")
|
2018-04-22 14:12:00 +00:00
|
|
|
user := utils.GetUser(ctx)
|
2018-03-17 19:41:18 +00:00
|
|
|
|
2018-04-19 20:01:28 +00:00
|
|
|
characters := search.Characters(term, maxCharacters)
|
2018-04-22 14:12:00 +00:00
|
|
|
return ctx.HTML(components.CharacterSearchResults(characters, user))
|
2018-03-17 19:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Forum search.
|
|
|
|
func Forum(ctx *aero.Context) string {
|
|
|
|
term := ctx.Get("term")
|
|
|
|
term = strings.TrimPrefix(term, "/")
|
2018-10-28 23:12:34 +00:00
|
|
|
user := utils.GetUser(ctx)
|
2018-03-17 19:41:18 +00:00
|
|
|
|
|
|
|
var posts []*arn.Post
|
|
|
|
var threads []*arn.Thread
|
|
|
|
|
|
|
|
flow.Parallel(func() {
|
2018-04-19 20:01:28 +00:00
|
|
|
posts = search.Posts(term, maxPosts)
|
2018-03-17 19:41:18 +00:00
|
|
|
}, func() {
|
2018-04-19 20:01:28 +00:00
|
|
|
threads = search.Threads(term, maxThreads)
|
2018-03-17 19:41:18 +00:00
|
|
|
})
|
|
|
|
|
2018-10-28 23:12:34 +00:00
|
|
|
return ctx.HTML(components.ForumSearchResults(posts, threads, user))
|
2018-03-17 19:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SoundTracks search.
|
|
|
|
func SoundTracks(ctx *aero.Context) string {
|
|
|
|
term := ctx.Get("term")
|
|
|
|
term = strings.TrimPrefix(term, "/")
|
2018-10-29 02:30:23 +00:00
|
|
|
user := utils.GetUser(ctx)
|
2018-03-17 19:41:18 +00:00
|
|
|
|
2018-04-19 20:01:28 +00:00
|
|
|
tracks := search.SoundTracks(term, maxSoundTracks)
|
2018-10-29 02:30:23 +00:00
|
|
|
return ctx.HTML(components.SoundTrackSearchResults(tracks, user))
|
2018-03-17 19:41:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Users search.
|
|
|
|
func Users(ctx *aero.Context) string {
|
|
|
|
term := ctx.Get("term")
|
|
|
|
term = strings.TrimPrefix(term, "/")
|
|
|
|
|
2018-04-19 20:01:28 +00:00
|
|
|
users := search.Users(term, maxUsers)
|
2018-03-17 19:41:18 +00:00
|
|
|
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, "/")
|
|
|
|
|
2018-04-19 20:01:28 +00:00
|
|
|
companies := search.Companies(term, maxCompanies)
|
2018-03-17 21:09:17 +00:00
|
|
|
return ctx.HTML(components.CompanySearchResults(companies))
|
|
|
|
}
|