Profile tracks is now a separate package
This commit is contained in:
57
pages/profile/profiletracks/render.go
Normal file
57
pages/profile/profiletracks/render.go
Normal file
@ -0,0 +1,57 @@
|
||||
package profiletracks
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"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 (
|
||||
tracksFirstLoad = 12
|
||||
tracksPerScroll = 3
|
||||
)
|
||||
|
||||
// render renders the soundtracks on user profiles.
|
||||
func render(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:]
|
||||
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.TrackList(tracks, viewUser, nextIndex, user, ctx.URI()))
|
||||
}
|
Reference in New Issue
Block a user