Abstracted soundtracks code (less code now)

This commit is contained in:
Eduard Urbach 2018-03-14 18:07:58 +01:00
parent 5ea75b210a
commit 19e9e3f958
4 changed files with 54 additions and 75 deletions

View File

@ -3,37 +3,16 @@ 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"
)
// Best renders the best soundtracks.
func Best(ctx *aero.Context) string {
user := utils.GetUser(ctx)
index, _ := ctx.GetInt("index")
// Fetch all eligible tracks
allTracks := fetchAll()
tracks := fetchAll()
// Sort the tracks by number of likes
arn.SortSoundTracksPopularFirst(allTracks)
arn.SortSoundTracksPopularFirst(tracks)
// Slice the part that we need
tracks := allTracks[index:]
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))
// Render
return render(ctx, tracks)
}

View 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))
}

View File

@ -3,39 +3,21 @@ 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"
)
const maxTracks = 12
const (
tracksFirstLoad = 12
tracksPerScroll = 3
)
// Latest renders the latest soundtracks.
func Latest(ctx *aero.Context) string {
user := utils.GetUser(ctx)
index, _ := ctx.GetInt("index")
// Fetch all eligible tracks
allTracks := fetchAll()
tracks := fetchAll()
// Sort the tracks by date
arn.SortSoundTracksLatestFirst(allTracks)
arn.SortSoundTracksLatestFirst(tracks)
// Slice the part that we need
tracks := allTracks[index:]
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))
// Render
return render(ctx, tracks)
}

View File

@ -3,40 +3,20 @@ 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"
)
// 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")
// 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)
})
// Sort the tracks by number of likes
arn.SortSoundTracksPopularFirst(allTracks)
arn.SortSoundTracksPopularFirst(tracks)
// Slice the part that we need
tracks := allTracks[index:]
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))
// Render
return render(ctx, tracks)
}