From 19e9e3f958224b112c29b5d6a857686e165a79d8 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 14 Mar 2018 18:07:58 +0100 Subject: [PATCH] Abstracted soundtracks code (less code now) --- pages/soundtracks/best.go | 29 ++++-------------------- pages/soundtracks/render.go | 38 ++++++++++++++++++++++++++++++++ pages/soundtracks/soundtracks.go | 34 +++++++--------------------- pages/soundtracks/tag.go | 28 ++++------------------- 4 files changed, 54 insertions(+), 75 deletions(-) create mode 100644 pages/soundtracks/render.go diff --git a/pages/soundtracks/best.go b/pages/soundtracks/best.go index e2b4316d..1abcca67 100644 --- a/pages/soundtracks/best.go +++ b/pages/soundtracks/best.go @@ -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) } diff --git a/pages/soundtracks/render.go b/pages/soundtracks/render.go new file mode 100644 index 00000000..04a21075 --- /dev/null +++ b/pages/soundtracks/render.go @@ -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)) +} diff --git a/pages/soundtracks/soundtracks.go b/pages/soundtracks/soundtracks.go index a8f47ffa..0d905c13 100644 --- a/pages/soundtracks/soundtracks.go +++ b/pages/soundtracks/soundtracks.go @@ -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) } diff --git a/pages/soundtracks/tag.go b/pages/soundtracks/tag.go index 2114e514..097110e4 100644 --- a/pages/soundtracks/tag.go +++ b/pages/soundtracks/tag.go @@ -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) }