Bugfix for tag filtering on soundtracks
This commit is contained in:
parent
c366396470
commit
f71dc3aa5a
@ -149,7 +149,7 @@ func Configure(app *aero.Application) {
|
||||
l.Page("/soundtracks/best", soundtracks.Best)
|
||||
l.Page("/soundtracks/best/from/:index", soundtracks.Best)
|
||||
l.Page("/soundtracks/tag/:tag", soundtracks.FilterByTag)
|
||||
l.Page("/soundtracks/tag/:tag/from/:index", soundtracks.FilterByTagFrom)
|
||||
l.Page("/soundtracks/tag/:tag/from/:index", soundtracks.FilterByTag)
|
||||
l.Page("/soundtrack/:id", soundtrack.Get)
|
||||
l.Page("/soundtrack/:id/edit", soundtrack.Edit)
|
||||
l.Page("/soundtrack/:id/history", soundtrack.History)
|
||||
|
@ -1,72 +1,42 @@
|
||||
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"
|
||||
"github.com/animenotifier/notify.moe/utils/infinitescroll"
|
||||
)
|
||||
|
||||
// FilterByTag renders the soundtracks with the given tag.
|
||||
// FilterByTag renders the best soundtracks filtered by tag.
|
||||
func FilterByTag(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
tag := ctx.Get("tag")
|
||||
index, _ := ctx.GetInt("index")
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// Fetch all eligible tracks
|
||||
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)
|
||||
}
|
||||
|
||||
// Sort the tracks by number of likes
|
||||
arn.SortSoundTracksPopularFirst(allTracks)
|
||||
|
||||
// Slice the part that we need
|
||||
tracks := allTracks[index:]
|
||||
|
||||
if len(tracks) > maxTracks {
|
||||
tracks = tracks[:maxTracks]
|
||||
}
|
||||
|
||||
nextIndex := index + maxTracks
|
||||
// Next index
|
||||
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxTracks, index)
|
||||
|
||||
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))
|
||||
// In case we're scrolling, send soundtracks only (without the page frame)
|
||||
if index > 0 {
|
||||
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
||||
}
|
||||
|
||||
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
||||
// Otherwise, send the full page
|
||||
return ctx.HTML(components.SoundTracks(tracks, nextIndex, "", user))
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user