Abstracted soundtracks code (less code now)
This commit is contained in:
parent
5ea75b210a
commit
19e9e3f958
@ -3,37 +3,16 @@ package soundtracks
|
|||||||
import (
|
import (
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
"github.com/animenotifier/arn"
|
"github.com/animenotifier/arn"
|
||||||
"github.com/animenotifier/notify.moe/components"
|
|
||||||
"github.com/animenotifier/notify.moe/utils"
|
|
||||||
"github.com/animenotifier/notify.moe/utils/infinitescroll"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Best renders the best soundtracks.
|
// Best renders the best soundtracks.
|
||||||
func Best(ctx *aero.Context) string {
|
func Best(ctx *aero.Context) string {
|
||||||
user := utils.GetUser(ctx)
|
|
||||||
index, _ := ctx.GetInt("index")
|
|
||||||
|
|
||||||
// Fetch all eligible tracks
|
// Fetch all eligible tracks
|
||||||
allTracks := fetchAll()
|
tracks := fetchAll()
|
||||||
|
|
||||||
// Sort the tracks by number of likes
|
// Sort the tracks by number of likes
|
||||||
arn.SortSoundTracksPopularFirst(allTracks)
|
arn.SortSoundTracksPopularFirst(tracks)
|
||||||
|
|
||||||
// Slice the part that we need
|
// Render
|
||||||
tracks := allTracks[index:]
|
return render(ctx, tracks)
|
||||||
|
|
||||||
if len(tracks) > maxTracks {
|
|
||||||
tracks = tracks[:maxTracks]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next index
|
|
||||||
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxTracks, index)
|
|
||||||
|
|
||||||
// In case we're scrolling, send soundtracks only (without the page frame)
|
|
||||||
if index > 0 {
|
|
||||||
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, send the full page
|
|
||||||
return ctx.HTML(components.SoundTracks(tracks, nextIndex, "", user))
|
|
||||||
}
|
}
|
||||||
|
38
pages/soundtracks/render.go
Normal file
38
pages/soundtracks/render.go
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
package soundtracks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"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"
|
||||||
|
)
|
||||||
|
|
||||||
|
// render renders the soundracks page with the given tracks.
|
||||||
|
func render(ctx *aero.Context, allTracks []*arn.SoundTrack) string {
|
||||||
|
user := utils.GetUser(ctx)
|
||||||
|
index, _ := ctx.GetInt("index")
|
||||||
|
|
||||||
|
// Slice the part that we need
|
||||||
|
tracks := allTracks[index:]
|
||||||
|
maxLength := tracksFirstLoad
|
||||||
|
|
||||||
|
if index > 0 {
|
||||||
|
maxLength = tracksPerScroll
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(tracks) > maxLength {
|
||||||
|
tracks = tracks[:maxLength]
|
||||||
|
}
|
||||||
|
|
||||||
|
// Next index
|
||||||
|
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxLength, index)
|
||||||
|
|
||||||
|
// In case we're scrolling, send soundtracks only (without the page frame)
|
||||||
|
if index > 0 {
|
||||||
|
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
||||||
|
}
|
||||||
|
|
||||||
|
// Otherwise, send the full page
|
||||||
|
return ctx.HTML(components.SoundTracks(tracks, nextIndex, "", user))
|
||||||
|
}
|
@ -3,39 +3,21 @@ package soundtracks
|
|||||||
import (
|
import (
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
"github.com/animenotifier/arn"
|
"github.com/animenotifier/arn"
|
||||||
"github.com/animenotifier/notify.moe/components"
|
|
||||||
"github.com/animenotifier/notify.moe/utils"
|
|
||||||
"github.com/animenotifier/notify.moe/utils/infinitescroll"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const maxTracks = 12
|
const (
|
||||||
|
tracksFirstLoad = 12
|
||||||
|
tracksPerScroll = 3
|
||||||
|
)
|
||||||
|
|
||||||
// Latest renders the latest soundtracks.
|
// Latest renders the latest soundtracks.
|
||||||
func Latest(ctx *aero.Context) string {
|
func Latest(ctx *aero.Context) string {
|
||||||
user := utils.GetUser(ctx)
|
|
||||||
index, _ := ctx.GetInt("index")
|
|
||||||
|
|
||||||
// Fetch all eligible tracks
|
// Fetch all eligible tracks
|
||||||
allTracks := fetchAll()
|
tracks := fetchAll()
|
||||||
|
|
||||||
// Sort the tracks by date
|
// Sort the tracks by date
|
||||||
arn.SortSoundTracksLatestFirst(allTracks)
|
arn.SortSoundTracksLatestFirst(tracks)
|
||||||
|
|
||||||
// Slice the part that we need
|
// Render
|
||||||
tracks := allTracks[index:]
|
return render(ctx, tracks)
|
||||||
|
|
||||||
if len(tracks) > maxTracks {
|
|
||||||
tracks = tracks[:maxTracks]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next index
|
|
||||||
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxTracks, index)
|
|
||||||
|
|
||||||
// In case we're scrolling, send soundtracks only (without the page frame)
|
|
||||||
if index > 0 {
|
|
||||||
return ctx.HTML(components.SoundTracksScrollable(tracks, user))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Otherwise, send the full page
|
|
||||||
return ctx.HTML(components.SoundTracks(tracks, nextIndex, "", user))
|
|
||||||
}
|
}
|
||||||
|
@ -3,40 +3,20 @@ package soundtracks
|
|||||||
import (
|
import (
|
||||||
"github.com/aerogo/aero"
|
"github.com/aerogo/aero"
|
||||||
"github.com/animenotifier/arn"
|
"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 best soundtracks filtered by tag.
|
// FilterByTag renders the best soundtracks filtered by tag.
|
||||||
func FilterByTag(ctx *aero.Context) string {
|
func FilterByTag(ctx *aero.Context) string {
|
||||||
user := utils.GetUser(ctx)
|
|
||||||
tag := ctx.Get("tag")
|
tag := ctx.Get("tag")
|
||||||
index, _ := ctx.GetInt("index")
|
|
||||||
|
|
||||||
// Fetch all eligible tracks
|
// Fetch all eligible tracks
|
||||||
allTracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
tracks := arn.FilterSoundTracks(func(track *arn.SoundTrack) bool {
|
||||||
return !track.IsDraft && len(track.Media) > 0 && track.HasTag(tag)
|
return !track.IsDraft && len(track.Media) > 0 && track.HasTag(tag)
|
||||||
})
|
})
|
||||||
|
|
||||||
// Sort the tracks by number of likes
|
// Sort the tracks by number of likes
|
||||||
arn.SortSoundTracksPopularFirst(allTracks)
|
arn.SortSoundTracksPopularFirst(tracks)
|
||||||
|
|
||||||
// Slice the part that we need
|
// Render
|
||||||
tracks := allTracks[index:]
|
return render(ctx, tracks)
|
||||||
|
|
||||||
if len(tracks) > maxTracks {
|
|
||||||
tracks = tracks[:maxTracks]
|
|
||||||
}
|
|
||||||
|
|
||||||
// Next index
|
|
||||||
nextIndex := infinitescroll.NextIndex(ctx, len(allTracks), maxTracks, index)
|
|
||||||
|
|
||||||
// In case we're scrolling, send soundtracks only (without the page frame)
|
|
||||||
if index > 0 {
|
|
||||||
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