2018-03-22 21:26:52 +00:00
|
|
|
package filteranime
|
|
|
|
|
|
|
|
import (
|
2018-03-23 00:43:45 +00:00
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2018-03-22 21:26:52 +00:00
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
"github.com/animenotifier/notify.moe/components"
|
2018-03-23 00:43:45 +00:00
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2018-03-22 21:26:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const maxAnimeEntries = 70
|
|
|
|
|
|
|
|
// editorList renders the anime list with the given title and filter.
|
2019-06-01 04:55:49 +00:00
|
|
|
func editorList(ctx aero.Context, title string, filter func(*arn.Anime) bool, searchLink func(*arn.Anime) string) error {
|
2018-03-23 00:43:45 +00:00
|
|
|
user := utils.GetUser(ctx)
|
|
|
|
|
|
|
|
if user == nil || (user.Role != "admin" && user.Role != "editor") {
|
2018-07-07 03:42:00 +00:00
|
|
|
return ctx.Error(http.StatusUnauthorized, "Not authorized")
|
2018-03-23 00:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
animes, count := filterAnime(ctx, user, filter)
|
|
|
|
|
|
|
|
// Determine URL
|
2019-06-01 04:55:49 +00:00
|
|
|
url := strings.TrimPrefix(ctx.Path(), "/_")
|
2018-03-23 00:43:45 +00:00
|
|
|
urlParts := strings.Split(url, "/")
|
2018-04-13 15:55:52 +00:00
|
|
|
urlParts = urlParts[:len(urlParts)-4]
|
2018-03-23 00:43:45 +00:00
|
|
|
url = strings.Join(urlParts, "/")
|
2018-03-22 21:26:52 +00:00
|
|
|
|
|
|
|
return ctx.HTML(components.AnimeEditorListFull(
|
|
|
|
title,
|
|
|
|
animes,
|
|
|
|
count,
|
2018-03-23 00:43:45 +00:00
|
|
|
url,
|
2018-03-22 21:26:52 +00:00
|
|
|
searchLink,
|
2018-03-23 00:43:45 +00:00
|
|
|
user,
|
2018-03-22 21:26:52 +00:00
|
|
|
))
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterAnime filters anime by the given filter function and
|
|
|
|
// additionally applies year and types filters if specified.
|
2019-06-01 04:55:49 +00:00
|
|
|
func filterAnime(ctx aero.Context, user *arn.User, filter func(*arn.Anime) bool) ([]*arn.Anime, int) {
|
2018-03-23 00:43:45 +00:00
|
|
|
year := ctx.Get("year")
|
|
|
|
status := ctx.Get("status")
|
2018-04-13 15:55:52 +00:00
|
|
|
season := ctx.Get("season")
|
2018-03-23 00:43:45 +00:00
|
|
|
typ := ctx.Get("type")
|
|
|
|
|
|
|
|
if year == "any" {
|
|
|
|
year = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if status == "any" {
|
|
|
|
status = ""
|
|
|
|
}
|
|
|
|
|
2018-04-13 15:55:52 +00:00
|
|
|
if season == "any" {
|
|
|
|
season = ""
|
|
|
|
}
|
|
|
|
|
2018-03-23 00:43:45 +00:00
|
|
|
if typ == "any" {
|
|
|
|
typ = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
settings := user.Settings()
|
|
|
|
settings.Editor.Filter.Year = year
|
2018-04-13 15:55:52 +00:00
|
|
|
settings.Editor.Filter.Season = season
|
2018-03-23 00:43:45 +00:00
|
|
|
settings.Editor.Filter.Status = status
|
|
|
|
settings.Editor.Filter.Type = typ
|
|
|
|
settings.Save()
|
2018-03-22 21:26:52 +00:00
|
|
|
|
|
|
|
// Filter
|
|
|
|
animes := arn.FilterAnime(func(anime *arn.Anime) bool {
|
2018-03-23 00:43:45 +00:00
|
|
|
if year != "" && (len(anime.StartDate) < 4 || anime.StartDate[:4] != year) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-04-13 15:55:52 +00:00
|
|
|
if season != "" && anime.Season() != season {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-23 00:43:45 +00:00
|
|
|
if status != "" && anime.Status != status {
|
2018-03-22 21:26:52 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-03-23 00:43:45 +00:00
|
|
|
if typ != "" && anime.Type != typ {
|
2018-03-22 21:26:52 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return filter(anime)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Sort
|
|
|
|
arn.SortAnimeByQuality(animes)
|
|
|
|
|
|
|
|
// Limit
|
|
|
|
count := len(animes)
|
|
|
|
|
|
|
|
if count > maxAnimeEntries {
|
|
|
|
animes = animes[:maxAnimeEntries]
|
|
|
|
}
|
|
|
|
|
|
|
|
return animes, count
|
|
|
|
}
|