2017-10-02 04:29:58 +00:00
|
|
|
package soundtracks
|
2017-06-27 10:39:41 +00:00
|
|
|
|
2017-06-27 12:01:32 +00:00
|
|
|
import (
|
2017-06-27 14:23:57 +00:00
|
|
|
"net/http"
|
2017-10-16 09:53:47 +00:00
|
|
|
"strconv"
|
2017-06-27 12:01:32 +00:00
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
"github.com/animenotifier/notify.moe/components"
|
2017-10-15 19:11:12 +00:00
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2017-06-27 12:01:32 +00:00
|
|
|
)
|
2017-06-27 10:39:41 +00:00
|
|
|
|
2017-10-16 10:56:46 +00:00
|
|
|
const maxTracks = 12
|
2017-06-27 12:04:25 +00:00
|
|
|
|
2017-10-09 13:47:40 +00:00
|
|
|
// Get renders the soundtracks page.
|
2017-06-27 10:39:41 +00:00
|
|
|
func Get(ctx *aero.Context) string {
|
2017-10-15 19:11:12 +00:00
|
|
|
user := utils.GetUser(ctx)
|
|
|
|
|
2017-11-18 10:37:29 +00:00
|
|
|
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
2017-10-15 18:19:45 +00:00
|
|
|
return !track.IsDraft && len(track.Media) > 0
|
2017-10-09 13:47:40 +00:00
|
|
|
})
|
2017-06-27 11:46:29 +00:00
|
|
|
|
2017-06-27 14:54:16 +00:00
|
|
|
arn.SortSoundTracksLatestFirst(tracks)
|
|
|
|
|
2017-06-27 12:04:25 +00:00
|
|
|
if len(tracks) > maxTracks {
|
|
|
|
tracks = tracks[:maxTracks]
|
|
|
|
}
|
|
|
|
|
2017-10-16 10:56:46 +00:00
|
|
|
return ctx.HTML(components.SoundTracks(tracks, maxTracks, user))
|
2017-06-27 10:39:41 +00:00
|
|
|
}
|
2017-10-16 09:53:47 +00:00
|
|
|
|
|
|
|
// From renders the soundtracks from the given index.
|
|
|
|
func From(ctx *aero.Context) string {
|
|
|
|
user := utils.GetUser(ctx)
|
|
|
|
index, err := ctx.GetInt("index")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Error(http.StatusBadRequest, "Invalid start index", err)
|
|
|
|
}
|
|
|
|
|
2017-11-18 10:37:29 +00:00
|
|
|
allTracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
2017-10-16 09:53:47 +00:00
|
|
|
return !track.IsDraft && len(track.Media) > 0
|
|
|
|
})
|
|
|
|
|
2017-10-16 10:56:46 +00:00
|
|
|
if index < 0 || index >= len(allTracks) {
|
|
|
|
return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(allTracks))+")", nil)
|
2017-10-16 09:53:47 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 10:56:46 +00:00
|
|
|
arn.SortSoundTracksLatestFirst(allTracks)
|
2017-10-16 09:53:47 +00:00
|
|
|
|
2017-10-16 10:56:46 +00:00
|
|
|
tracks := allTracks[index:]
|
2017-10-16 09:53:47 +00:00
|
|
|
|
|
|
|
if len(tracks) > maxTracks {
|
|
|
|
tracks = tracks[:maxTracks]
|
|
|
|
}
|
|
|
|
|
2017-10-16 10:56:46 +00:00
|
|
|
nextIndex := index + maxTracks
|
|
|
|
|
|
|
|
if nextIndex >= len(allTracks) {
|
|
|
|
// End of data - no more scrolling
|
|
|
|
ctx.Response().Header().Set("X-LoadMore-Index", "-1")
|
|
|
|
} else {
|
|
|
|
// Send the index for the next request
|
|
|
|
ctx.Response().Header().Set("X-LoadMore-Index", strconv.Itoa(nextIndex))
|
|
|
|
}
|
|
|
|
|
2017-10-16 09:53:47 +00:00
|
|
|
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
|
|
|
}
|