2018-03-13 00:49:54 +00:00
|
|
|
package soundtracks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
"github.com/animenotifier/notify.moe/components"
|
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2018-03-14 03:57:08 +00:00
|
|
|
"github.com/animenotifier/notify.moe/utils/infinitescroll"
|
2018-03-13 00:49:54 +00:00
|
|
|
)
|
|
|
|
|
2018-03-14 03:57:08 +00:00
|
|
|
// FilterByTag renders the best soundtracks filtered by tag.
|
2018-03-13 00:49:54 +00:00
|
|
|
func FilterByTag(ctx *aero.Context) string {
|
|
|
|
user := utils.GetUser(ctx)
|
|
|
|
tag := ctx.Get("tag")
|
2018-03-14 03:57:08 +00:00
|
|
|
index, _ := ctx.GetInt("index")
|
2018-03-13 00:49:54 +00:00
|
|
|
|
2018-03-14 03:57:08 +00:00
|
|
|
// Fetch all eligible tracks
|
2018-03-13 00:49:54 +00:00
|
|
|
allTracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
|
|
|
return !track.IsDraft && len(track.Media) > 0 && track.HasTag(tag)
|
|
|
|
})
|
|
|
|
|
2018-03-14 03:57:08 +00:00
|
|
|
// Sort the tracks by number of likes
|
2018-03-13 00:49:54 +00:00
|
|
|
arn.SortSoundTracksPopularFirst(allTracks)
|
|
|
|
|
2018-03-14 03:57:08 +00:00
|
|
|
// Slice the part that we need
|
2018-03-13 00:49:54 +00:00
|
|
|
tracks := allTracks[index:]
|
|
|
|
|
|
|
|
if len(tracks) > maxTracks {
|
|
|
|
tracks = tracks[:maxTracks]
|
|
|
|
}
|
|
|
|
|
2018-03-14 03:57:08 +00:00
|
|
|
// Next index
|
|
|
|
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxTracks, index)
|
2018-03-13 00:49:54 +00:00
|
|
|
|
2018-03-14 03:57:08 +00:00
|
|
|
// In case we're scrolling, send soundtracks only (without the page frame)
|
|
|
|
if index > 0 {
|
|
|
|
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
2018-03-13 00:49:54 +00:00
|
|
|
}
|
|
|
|
|
2018-03-14 03:57:08 +00:00
|
|
|
// Otherwise, send the full page
|
|
|
|
return ctx.HTML(components.SoundTracks(tracks, nextIndex, "", user))
|
2018-03-13 00:49:54 +00:00
|
|
|
}
|