54 lines
1.2 KiB
Go
Raw Normal View History

2018-04-13 11:09:46 +00:00
package filtersoundtracks
import (
"net/http"
"strings"
"github.com/aerogo/aero"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2018-04-13 11:09:46 +00:00
"github.com/animenotifier/notify.moe/components"
)
const maxSoundTrackEntries = 70
// editorList renders the soundtrack 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.SoundTrack) bool, searchLink func(*arn.SoundTrack) string) error {
2019-11-17 07:59:34 +00:00
user := arn.GetUserFromContext(ctx)
2018-04-13 11:09:46 +00:00
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-04-13 11:09:46 +00:00
}
2019-06-05 07:18:04 +00:00
tracks, count := filterSoundTracks(filter)
2019-06-01 04:55:49 +00:00
url := strings.TrimPrefix(ctx.Path(), "/_")
2018-04-13 11:09:46 +00:00
return ctx.HTML(components.SoundTracksEditorListFull(
title,
tracks,
count,
url,
searchLink,
user,
))
}
// filterSoundTracks filters soundtracks by the given filter function.
2019-06-05 07:18:04 +00:00
func filterSoundTracks(filter func(*arn.SoundTrack) bool) ([]*arn.SoundTrack, int) {
2018-04-13 11:09:46 +00:00
// Filter
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
return !track.IsDraft && filter(track)
})
// Sort
arn.SortSoundTracksPopularFirst(tracks)
// Limit
count := len(tracks)
if count > maxSoundTrackEntries {
tracks = tracks[:maxSoundTrackEntries]
}
return tracks, count
}