Profile tracks is now a separate package
This commit is contained in:
18
pages/profile/profiletracks/added.go
Normal file
18
pages/profile/profiletracks/added.go
Normal file
@ -0,0 +1,18 @@
|
||||
package profiletracks
|
||||
|
||||
import (
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
)
|
||||
|
||||
// Added shows all soundtracks added by a particular user.
|
||||
func Added(ctx *aero.Context) string {
|
||||
return render(ctx, addedTracks)
|
||||
}
|
||||
|
||||
// addedTracks returns all soundtracks that the user with the given user ID 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
|
||||
})
|
||||
}
|
18
pages/profile/profiletracks/liked.go
Normal file
18
pages/profile/profiletracks/liked.go
Normal file
@ -0,0 +1,18 @@
|
||||
package profiletracks
|
||||
|
||||
import (
|
||||
"github.com/aerogo/aero"
|
||||
"github.com/animenotifier/arn"
|
||||
)
|
||||
|
||||
// Liked shows all soundtracks liked by a particular user.
|
||||
func Liked(ctx *aero.Context) string {
|
||||
return render(ctx, likedTracks)
|
||||
}
|
||||
|
||||
// likedTracks returns all soundtracks that the user with the given user ID 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)
|
||||
})
|
||||
}
|
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()))
|
||||
}
|
21
pages/profile/profiletracks/tracks.pixy
Normal file
21
pages/profile/profiletracks/tracks.pixy
Normal file
@ -0,0 +1,21 @@
|
||||
component TrackList(tracks []*arn.SoundTrack, viewUser *arn.User, nextIndex int, user *arn.User, uri string)
|
||||
ProfileHeader(viewUser, user, uri)
|
||||
|
||||
if strings.Contains(uri, "/added")
|
||||
h1.page-title= "Tracks added by " + viewUser.Nick
|
||||
else
|
||||
h1.page-title= "Tracks liked by " + viewUser.Nick
|
||||
|
||||
if len(tracks) == 0
|
||||
if strings.Contains(uri, "/added")
|
||||
p.no-data.mountable= viewUser.Nick + " hasn't added any tracks yet."
|
||||
else
|
||||
p.no-data.mountable= viewUser.Nick + " hasn't liked any tracks yet."
|
||||
else
|
||||
#load-more-target.soundtracks
|
||||
SoundTracksScrollable(tracks, user)
|
||||
|
||||
if nextIndex != -1
|
||||
.buttons
|
||||
LoadMore(nextIndex)
|
||||
|
Reference in New Issue
Block a user