Profile tracks is now a separate package

This commit is contained in:
Eduard Urbach 2018-03-14 18:21:22 +01:00
parent 1a380f3139
commit 5b05d82b7d
7 changed files with 98 additions and 76 deletions

View File

@ -43,6 +43,7 @@ import (
"github.com/animenotifier/notify.moe/pages/popular"
"github.com/animenotifier/notify.moe/pages/posts"
"github.com/animenotifier/notify.moe/pages/profile"
"github.com/animenotifier/notify.moe/pages/profile/profiletracks"
"github.com/animenotifier/notify.moe/pages/quote"
"github.com/animenotifier/notify.moe/pages/quotes"
"github.com/animenotifier/notify.moe/pages/recommended"
@ -169,10 +170,10 @@ 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.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/soundtracks/added", profiletracks.Added)
l.Page("/user/:nick/soundtracks/added/from/:index", profiletracks.Added)
l.Page("/user/:nick/soundtracks/liked", profiletracks.Liked)
l.Page("/user/:nick/soundtracks/liked/from/:index", profiletracks.Liked)
l.Page("/user/:nick/stats", profile.GetStatsByUser)
l.Page("/user/:nick/followers", profile.GetFollowers)
l.Page("/user/:nick/animelist", animelist.Get)

View File

@ -8,7 +8,6 @@ import (
)
const maxPosts = 5
const maxTracks = 12
// Get user profile page.
func Get(ctx *aero.Context) string {

View 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
})
}

View 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)
})
}

View 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()))
}

View File

@ -1,71 +0,0 @@
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"
"github.com/animenotifier/notify.moe/utils/infinitescroll"
)
// 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 {
return getSoundTracks(ctx, likedTracks)
}
// 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
})
}
// 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()))
}