42 lines
1.0 KiB
Go
Raw Normal View History

2017-10-02 06:29:58 +02:00
package soundtracks
2017-06-27 12:39:41 +02:00
2017-06-27 14:01:32 +02:00
import (
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
2017-10-15 21:11:12 +02:00
"github.com/animenotifier/notify.moe/utils"
2018-03-13 22:38:26 +01:00
"github.com/animenotifier/notify.moe/utils/infinitescroll"
2017-06-27 14:01:32 +02:00
)
2017-06-27 12:39:41 +02:00
2017-10-16 12:56:46 +02:00
const maxTracks = 12
2017-06-27 14:04:25 +02:00
2018-03-13 22:38:26 +01:00
// Latest renders the latest soundtracks.
2017-11-19 17:02:20 +01:00
func Latest(ctx *aero.Context) string {
2017-10-15 21:11:12 +02:00
user := utils.GetUser(ctx)
2018-03-13 22:38:26 +01:00
index, _ := ctx.GetInt("index")
2017-10-15 21:11:12 +02:00
2018-03-13 22:38:26 +01:00
// Fetch all eligible tracks
allTracks := fetchAll()
2017-10-16 11:53:47 +02:00
2018-03-13 22:38:26 +01:00
// Sort the tracks by date
2017-10-16 12:56:46 +02:00
arn.SortSoundTracksLatestFirst(allTracks)
2017-10-16 11:53:47 +02:00
2018-03-13 22:38:26 +01:00
// Slice the part that we need
2017-10-16 12:56:46 +02:00
tracks := allTracks[index:]
2017-10-16 11:53:47 +02:00
if len(tracks) > maxTracks {
tracks = tracks[:maxTracks]
}
2018-03-13 22:38:26 +01:00
// Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxTracks, index)
2017-10-16 12:56:46 +02:00
2018-03-13 22:38:26 +01:00
// In case we're scrolling, send soundtracks only (without the page frame)
if index > 0 {
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
2017-10-16 12:56:46 +02:00
}
2018-03-13 22:38:26 +01:00
// Otherwise, send the full page
return ctx.HTML(components.SoundTracks(tracks, nextIndex, "", user))
2017-10-16 11:53:47 +02:00
}