2017-06-27 20:12:08 +02:00
|
|
|
package profile
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
|
|
|
"github.com/animenotifier/arn"
|
|
|
|
"github.com/animenotifier/notify.moe/components"
|
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2018-03-14 15:56:09 +01:00
|
|
|
"github.com/animenotifier/notify.moe/utils/infinitescroll"
|
2017-06-27 20:12:08 +02:00
|
|
|
)
|
|
|
|
|
2018-03-14 16:46:45 +01:00
|
|
|
// GetSoundTracksAddedByUser shows all soundtracks added by a particular user.
|
|
|
|
func GetSoundTracksAddedByUser(ctx *aero.Context) string {
|
|
|
|
return getSoundTracks(ctx, addedTracks)
|
|
|
|
}
|
2018-03-14 15:56:09 +01:00
|
|
|
|
2018-03-14 16:46:45 +01:00
|
|
|
// GetSoundTracksLikedByUser shows all soundtracks liked by a particular user.
|
|
|
|
func GetSoundTracksLikedByUser(ctx *aero.Context) string {
|
|
|
|
return getSoundTracks(ctx, likedTracks)
|
|
|
|
}
|
2018-03-14 15:56:09 +01:00
|
|
|
|
2018-03-14 16:46:45 +01:00
|
|
|
// 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
|
|
|
|
})
|
|
|
|
}
|
2017-06-27 20:12:08 +02:00
|
|
|
|
2018-03-14 16:46:45 +01:00
|
|
|
// 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)
|
|
|
|
})
|
2017-06-27 20:12:08 +02:00
|
|
|
}
|
2018-03-14 11:57:40 +01:00
|
|
|
|
2018-03-14 16:46:45 +01:00
|
|
|
// getSoundTracks is the request handler for profile soundtrack pages.
|
|
|
|
func getSoundTracks(ctx *aero.Context, fetch func(userID string) []*arn.SoundTrack) string {
|
2018-03-14 11:57:40 +01:00
|
|
|
nick := ctx.Get("nick")
|
2018-03-14 16:46:45 +01:00
|
|
|
index, _ := ctx.GetInt("index")
|
2018-03-14 11:57:40 +01:00
|
|
|
user := utils.GetUser(ctx)
|
|
|
|
viewUser, err := arn.GetUserByNick(nick)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return ctx.Error(http.StatusNotFound, "User not found", err)
|
|
|
|
}
|
|
|
|
|
2018-03-14 15:56:09 +01:00
|
|
|
// Fetch all eligible tracks
|
2018-03-14 16:46:45 +01:00
|
|
|
allTracks := fetch(viewUser.ID)
|
2018-03-14 11:57:40 +01:00
|
|
|
|
2018-03-14 16:09:32 +01:00
|
|
|
// Sort the tracks by publication date
|
2018-03-14 15:56:09 +01:00
|
|
|
arn.SortSoundTracksLatestFirst(allTracks)
|
2018-03-14 11:57:40 +01:00
|
|
|
|
2018-03-14 15:56:09 +01:00
|
|
|
// 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))
|
|
|
|
}
|
2018-03-14 11:57:40 +01:00
|
|
|
|
2018-03-14 15:56:09 +01:00
|
|
|
// Otherwise, send the full page
|
2018-03-14 16:09:32 +01:00
|
|
|
return ctx.HTML(components.TrackList(tracks, viewUser, nextIndex, user, ctx.URI()))
|
2018-03-14 11:57:40 +01:00
|
|
|
}
|