Added tag filter for soundtracks

This commit is contained in:
2018-03-13 01:49:54 +01:00
parent e67a2023c7
commit 634bbb151e
10 changed files with 114 additions and 25 deletions

View File

@ -20,11 +20,15 @@ func Best(ctx *aero.Context) string {
arn.SortSoundTracksPopularFirst(tracks)
// Limit the number of displayed tracks
loadMoreIndex := 0
if len(tracks) > maxTracks {
tracks = tracks[:maxTracks]
loadMoreIndex = maxTracks
}
return ctx.HTML(components.SoundTracks(tracks, maxTracks, user))
return ctx.HTML(components.SoundTracks(tracks, loadMoreIndex, "", user))
}
// BestFrom renders the soundtracks from the given index.

View File

@ -30,7 +30,7 @@ func Latest(ctx *aero.Context) string {
loadMoreIndex = maxTracks
}
return ctx.HTML(components.SoundTracks(tracks, loadMoreIndex, user))
return ctx.HTML(components.SoundTracks(tracks, loadMoreIndex, "", user))
}
// LatestFrom renders the soundtracks from the given index.

View File

@ -1,7 +1,7 @@
component SoundTracks(tracks []*arn.SoundTrack, loadMoreIndex int, user *arn.User)
component SoundTracks(tracks []*arn.SoundTrack, loadMoreIndex int, tag string, user *arn.User)
h1.page-title Soundtracks
SoundTracksTabs
SoundTracksTabs(tag)
.corner-buttons
if user != nil
@ -25,7 +25,10 @@ component SoundTracksScrollable(tracks []*arn.SoundTrack, user *arn.User)
each track in tracks
SoundTrack(track)
component SoundTracksTabs
component SoundTracksTabs(tag string)
.tabs
Tab("Latest", "music", "/soundtracks")
Tab("Best", "heart", "/soundtracks/best")
Tab("Best", "heart", "/soundtracks/best")
if tag != ""
Tab(tag, "tag", "/soundtracks/tag/" + tag)

72
pages/soundtracks/tag.go Normal file
View File

@ -0,0 +1,72 @@
package soundtracks
import (
"net/http"
"strconv"
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
// FilterByTag renders the soundtracks with the given tag.
func FilterByTag(ctx *aero.Context) string {
user := utils.GetUser(ctx)
tag := ctx.Get("tag")
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
return !track.IsDraft && len(track.Media) > 0 && track.HasTag(tag)
})
arn.SortSoundTracksPopularFirst(tracks)
// Limit the number of displayed tracks
loadMoreIndex := 0
if len(tracks) > maxTracks {
tracks = tracks[:maxTracks]
loadMoreIndex = maxTracks
}
return ctx.HTML(components.SoundTracks(tracks, loadMoreIndex, tag, user))
}
// FilterByTagFrom renders the soundtracks from the given index.
func FilterByTagFrom(ctx *aero.Context) string {
user := utils.GetUser(ctx)
tag := ctx.Get("tag")
index, err := ctx.GetInt("index")
if err != nil {
return ctx.Error(http.StatusBadRequest, "Invalid start index", err)
}
allTracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
return !track.IsDraft && len(track.Media) > 0 && track.HasTag(tag)
})
if index < 0 || index >= len(allTracks) {
return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(allTracks))+")", nil)
}
arn.SortSoundTracksPopularFirst(allTracks)
tracks := allTracks[index:]
if len(tracks) > maxTracks {
tracks = tracks[:maxTracks]
}
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))
}
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
}