154 lines
3.6 KiB
Go
Raw Normal View History

package search
import (
2019-08-31 07:52:42 +00:00
"net/http"
2017-11-11 15:16:25 +00:00
"github.com/animenotifier/notify.moe/utils"
"github.com/aerogo/aero"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn/search"
2017-06-20 20:54:45 +00:00
"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
2018-11-12 07:52:07 +00:00
maxAMVs = 3
2018-03-17 21:09:17 +00:00
maxCharacters = 22
maxCompanies = 3
)
2017-06-20 20:54:45 +00:00
2017-06-16 23:25:02 +00:00
// Get search page.
2019-06-01 04:55:49 +00:00
func Get(ctx aero.Context) error {
2017-11-11 15:16:25 +00:00
term := ctx.Get("term")
user := utils.GetUser(ctx)
2017-06-20 20:54:45 +00:00
2019-08-31 07:52:42 +00:00
if len(term) > search.MaxSearchTermLength {
ctx.SetStatus(http.StatusRequestEntityTooLarge)
}
2018-11-12 07:52:07 +00:00
users, animes, posts, threads, tracks, characters, amvs, companies := search.All(
2018-03-17 21:09:17 +00:00
term,
maxUsers,
maxAnime,
maxPosts,
maxThreads,
maxSoundTracks,
maxCharacters,
2018-11-12 07:52:07 +00:00
maxAMVs,
2018-03-17 21:09:17 +00:00
maxCompanies,
)
2018-11-12 07:52:07 +00:00
return ctx.HTML(components.SearchResults(term, users, animes, posts, threads, tracks, characters, amvs, companies, nil, user))
}
2018-03-17 19:41:18 +00:00
// GetEmptySearch renders the search page with no contents.
2019-06-01 04:55:49 +00:00
func GetEmptySearch(ctx aero.Context) error {
2018-11-12 07:52:07 +00:00
return ctx.HTML(components.SearchResults("", nil, nil, nil, nil, nil, nil, nil, nil, nil, utils.GetUser(ctx)))
2018-03-17 19:41:18 +00:00
}
// Anime search.
2019-06-01 04:55:49 +00:00
func Anime(ctx aero.Context) error {
2018-03-17 19:41:18 +00:00
term := ctx.Get("term")
2018-04-23 08:30:25 +00:00
user := utils.GetUser(ctx)
2018-03-17 19:41:18 +00:00
2019-08-31 07:52:42 +00:00
if len(term) > search.MaxSearchTermLength {
ctx.SetStatus(http.StatusRequestEntityTooLarge)
}
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.
2019-06-01 04:55:49 +00:00
func Characters(ctx aero.Context) error {
2018-03-17 19:41:18 +00:00
term := ctx.Get("term")
user := utils.GetUser(ctx)
2018-03-17 19:41:18 +00:00
2019-08-31 07:52:42 +00:00
if len(term) > search.MaxSearchTermLength {
ctx.SetStatus(http.StatusRequestEntityTooLarge)
}
characters := search.Characters(term, maxCharacters)
return ctx.HTML(components.CharacterSearchResults(characters, user))
2018-03-17 19:41:18 +00:00
}
2018-12-06 04:27:01 +00:00
// Posts search.
2019-06-01 04:55:49 +00:00
func Posts(ctx aero.Context) error {
2018-03-17 19:41:18 +00:00
term := ctx.Get("term")
2018-10-28 23:12:34 +00:00
user := utils.GetUser(ctx)
2018-03-17 19:41:18 +00:00
2019-08-31 07:52:42 +00:00
if len(term) > search.MaxSearchTermLength {
ctx.SetStatus(http.StatusRequestEntityTooLarge)
}
posts := search.Posts(term, maxPosts)
2018-12-06 04:27:01 +00:00
return ctx.HTML(components.PostsSearchResults(posts, user))
}
2018-03-17 19:41:18 +00:00
2018-12-06 04:27:01 +00:00
// Threads search.
2019-06-01 04:55:49 +00:00
func Threads(ctx aero.Context) error {
2018-12-06 04:27:01 +00:00
term := ctx.Get("term")
user := utils.GetUser(ctx)
2018-03-17 19:41:18 +00:00
2019-08-31 07:52:42 +00:00
if len(term) > search.MaxSearchTermLength {
ctx.SetStatus(http.StatusRequestEntityTooLarge)
}
threads := search.Threads(term, maxThreads)
2018-12-06 04:27:01 +00:00
return ctx.HTML(components.ThreadsSearchResults(threads, user))
2018-03-17 19:41:18 +00:00
}
// SoundTracks search.
2019-06-01 04:55:49 +00:00
func SoundTracks(ctx aero.Context) error {
2018-03-17 19:41:18 +00:00
term := ctx.Get("term")
2018-10-29 02:30:23 +00:00
user := utils.GetUser(ctx)
2018-03-17 19:41:18 +00:00
2019-08-31 07:52:42 +00:00
if len(term) > search.MaxSearchTermLength {
ctx.SetStatus(http.StatusRequestEntityTooLarge)
}
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
}
2018-11-12 07:52:07 +00:00
// AMVs search.
2019-06-01 04:55:49 +00:00
func AMVs(ctx aero.Context) error {
2018-11-12 07:52:07 +00:00
term := ctx.Get("term")
user := utils.GetUser(ctx)
2019-08-31 07:52:42 +00:00
if len(term) > search.MaxSearchTermLength {
ctx.SetStatus(http.StatusRequestEntityTooLarge)
}
2018-11-12 07:52:07 +00:00
amvs := search.AMVs(term, maxAMVs)
return ctx.HTML(components.AMVSearchResults(amvs, user))
}
2018-03-17 19:41:18 +00:00
// Users search.
2019-06-01 04:55:49 +00:00
func Users(ctx aero.Context) error {
2018-03-17 19:41:18 +00:00
term := ctx.Get("term")
2019-08-31 07:52:42 +00:00
if len(term) > search.MaxSearchTermLength {
ctx.SetStatus(http.StatusRequestEntityTooLarge)
}
2018-03-17 19:41:18 +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.
2019-06-01 04:55:49 +00:00
func Companies(ctx aero.Context) error {
2018-03-17 21:09:17 +00:00
term := ctx.Get("term")
2019-08-31 07:52:42 +00:00
if len(term) > search.MaxSearchTermLength {
ctx.SetStatus(http.StatusRequestEntityTooLarge)
}
2018-03-17 21:09:17 +00:00
companies := search.Companies(term, maxCompanies)
2018-03-17 21:09:17 +00:00
return ctx.HTML(components.CompanySearchResults(companies))
}