Rewritten infinite scrolling
This commit is contained in:
@ -1,70 +1,39 @@
|
||||
package quotes
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
// Best renders the quotes page ordered by the most favorites first.
|
||||
// Best renders the best quotes.
|
||||
func Best(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
index, _ := ctx.GetInt("index")
|
||||
|
||||
quotes := arn.FilterQuotes(func(track *arn.Quote) bool {
|
||||
return !track.IsDraft
|
||||
})
|
||||
|
||||
arn.SortQuotesPopularFirst(quotes)
|
||||
|
||||
// Limit the number of displayed quotes
|
||||
loadMoreIndex := 0
|
||||
|
||||
if len(quotes) > maxQuotes {
|
||||
quotes = quotes[:maxQuotes]
|
||||
loadMoreIndex = maxQuotes
|
||||
}
|
||||
|
||||
return ctx.HTML(components.Quotes(quotes, loadMoreIndex, user))
|
||||
}
|
||||
|
||||
// BestFrom renders the quotes from the given index.
|
||||
func BestFrom(ctx *aero.Context) string {
|
||||
user := utils.GetUser(ctx)
|
||||
index, err := ctx.GetInt("index")
|
||||
|
||||
if err != nil {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index", err)
|
||||
}
|
||||
|
||||
allQuotes := arn.FilterQuotes(func(track *arn.Quote) bool {
|
||||
return !track.IsDraft
|
||||
})
|
||||
|
||||
if index < 0 || index >= len(allQuotes) {
|
||||
return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(allQuotes))+")", nil)
|
||||
}
|
||||
// Fetch all eligible quotes
|
||||
allQuotes := fetchAll()
|
||||
|
||||
// Sort the quotes by date
|
||||
arn.SortQuotesPopularFirst(allQuotes)
|
||||
|
||||
// Slice the part that we need
|
||||
quotes := allQuotes[index:]
|
||||
|
||||
if len(quotes) > maxQuotes {
|
||||
quotes = quotes[:maxQuotes]
|
||||
}
|
||||
|
||||
nextIndex := index + maxQuotes
|
||||
// Next index
|
||||
nextIndex := infinitescroll.NextIndex(ctx, len(allQuotes), maxQuotes, index)
|
||||
|
||||
if nextIndex >= len(allQuotes) {
|
||||
// End of data - no more scrolling
|
||||
ctx.Response().Header().Set("X-LoadMore-Index", "-1")
|
||||
} else {
|
||||
// Send the index for the next request
|
||||
ctx.Response().Header().Set("X-LoadMore-Index", strconv.Itoa(nextIndex))
|
||||
// In case we're scrolling, send quotes only (without the page frame)
|
||||
if index > 0 {
|
||||
return ctx.HTML(components.QuotesScrollable(quotes, user))
|
||||
}
|
||||
|
||||
return ctx.HTML(components.QuotesScrollable(quotes, user))
|
||||
// Otherwise, send the full page
|
||||
return ctx.HTML(components.Quotes(quotes, nextIndex, user))
|
||||
}
|
||||
|
Reference in New Issue
Block a user