From 5ea75b210a50f5c6ec6ac85eaad735cb7f5dd6ef Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 14 Mar 2018 16:46:45 +0100 Subject: [PATCH] Abstract soundtracks request handler --- pages/index.go | 4 +- pages/profile/tracks.go | 117 +++++++++++++++------------------------- 2 files changed, 46 insertions(+), 75 deletions(-) diff --git a/pages/index.go b/pages/index.go index 635a76c5..b73053de 100644 --- a/pages/index.go +++ b/pages/index.go @@ -169,8 +169,8 @@ func Configure(app *aero.Application) { l.Page("/user/:nick", profile.Get) l.Page("/user/:nick/forum/threads", profile.GetThreadsByUser) l.Page("/user/:nick/forum/posts", profile.GetPostsByUser) - l.Page("/user/:nick/soundtracks/added", profile.GetSoundTracksByUser) - l.Page("/user/:nick/soundtracks/added/from/:index", profile.GetSoundTracksByUser) + l.Page("/user/:nick/soundtracks/added", profile.GetSoundTracksAddedByUser) + l.Page("/user/:nick/soundtracks/added/from/:index", profile.GetSoundTracksAddedByUser) l.Page("/user/:nick/soundtracks/liked", profile.GetSoundTracksLikedByUser) l.Page("/user/:nick/soundtracks/liked/from/:index", profile.GetSoundTracksLikedByUser) l.Page("/user/:nick/stats", profile.GetStatsByUser) diff --git a/pages/profile/tracks.go b/pages/profile/tracks.go index b7384cc6..f4e2592d 100644 --- a/pages/profile/tracks.go +++ b/pages/profile/tracks.go @@ -10,91 +10,62 @@ import ( "github.com/animenotifier/notify.moe/utils/infinitescroll" ) -// GetSoundTracksByUser shows all soundtracks of a particular user. -func GetSoundTracksByUser(ctx *aero.Context) string { - nick := ctx.Get("nick") - user := utils.GetUser(ctx) - viewUser, err := arn.GetUserByNick(nick) - - if err != nil { - return ctx.Error(http.StatusNotFound, "User not found", err) - } - - index, _ := ctx.GetInt("index") - - // Fetch all eligible tracks - allTracks := fetchAllTracksByUser(viewUser.ID) - - // Sort the tracks by publication date - arn.SortSoundTracksLatestFirst(allTracks) - - // 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.TrackList(tracks, viewUser, nextIndex, user, ctx.URI())) - +// GetSoundTracksAddedByUser shows all soundtracks added by a particular user. +func GetSoundTracksAddedByUser(ctx *aero.Context) string { + return getSoundTracks(ctx, addedTracks) } // GetSoundTracksLikedByUser shows all soundtracks liked by a particular user. func GetSoundTracksLikedByUser(ctx *aero.Context) string { - nick := ctx.Get("nick") - user := utils.GetUser(ctx) - viewUser, err := arn.GetUserByNick(nick) - - if err != nil { - return ctx.Error(http.StatusNotFound, "User not found", err) - } - - index, _ := ctx.GetInt("index") - - // Fetch all eligible tracks - allTracks := fetchAllTracksLikedByUser(viewUser.ID) - - // Sort the tracks by publication date - arn.SortSoundTracksLatestFirst(allTracks) - - // 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.TrackList(tracks, viewUser, nextIndex, user, ctx.URI())) + return getSoundTracks(ctx, likedTracks) } -// fetchAllTracksByUser returns all soundtracks that the user with userID published -func fetchAllTracksByUser(userID string) []*arn.SoundTrack { +// addedTracks returns all soundtracks that the user with the given userID published. +func addedTracks(userID string) []*arn.SoundTrack { return arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { return !track.IsDraft && len(track.Media) > 0 && track.CreatedBy == userID }) } -// fetchAllTracksLikedByUser returns all soundtracks that the user with userID liked -func fetchAllTracksLikedByUser(userID string) []*arn.SoundTrack { +// likedTracks returns all soundtracks that the user with the given userID liked. +func likedTracks(userID string) []*arn.SoundTrack { return arn.FilterSoundTracks(func(track *arn.SoundTrack) bool { return !track.IsDraft && len(track.Media) > 0 && track.LikedBy(userID) }) } + +// getSoundTracks is the request handler for profile soundtrack pages. +func getSoundTracks(ctx *aero.Context, fetch func(userID string) []*arn.SoundTrack) string { + nick := ctx.Get("nick") + index, _ := ctx.GetInt("index") + user := utils.GetUser(ctx) + viewUser, err := arn.GetUserByNick(nick) + + if err != nil { + return ctx.Error(http.StatusNotFound, "User not found", err) + } + + // Fetch all eligible tracks + allTracks := fetch(viewUser.ID) + + // Sort the tracks by publication date + arn.SortSoundTracksLatestFirst(allTracks) + + // 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.TrackList(tracks, viewUser, nextIndex, user, ctx.URI())) +}