73 lines
1.6 KiB
Go
Raw Normal View History

2017-12-03 21:33:24 +00:00
package quotes
import (
2018-02-25 16:06:47 +00:00
"net/http"
"strconv"
2017-12-03 21:33:24 +00:00
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
)
2018-02-25 16:06:47 +00:00
const maxQuotes = 6
// Latest renders the quotes page.
func Latest(ctx *aero.Context) string {
user := utils.GetUser(ctx)
quotes := arn.FilterQuotes(func(track *arn.Quote) bool {
2018-02-26 11:25:35 +00:00
return !track.IsDraft
})
arn.SortQuotesLatestFirst(quotes)
2018-02-25 18:22:59 +00:00
// Limit the number of displayed quotes
2018-02-25 16:06:47 +00:00
loadMoreIndex := 0
if len(quotes) > maxQuotes {
quotes = quotes[:maxQuotes]
2018-02-25 16:06:47 +00:00
loadMoreIndex = maxQuotes
}
2018-02-25 16:06:47 +00:00
return ctx.HTML(components.Quotes(quotes, loadMoreIndex, user))
}
// LatestFrom renders the quotes from the given index.
func LatestFrom(ctx *aero.Context) string {
2017-12-03 21:33:24 +00:00
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 {
2018-02-26 11:25:35 +00:00
return !track.IsDraft
})
if index < 0 || index >= len(allQuotes) {
return ctx.Error(http.StatusBadRequest, "Invalid start index (maximum is "+strconv.Itoa(len(allQuotes))+")", nil)
}
arn.SortQuotesLatestFirst(allQuotes)
quotes := allQuotes[index:]
if len(quotes) > maxQuotes {
quotes = quotes[:maxQuotes]
}
nextIndex := index + maxQuotes
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))
}
2017-12-03 21:33:24 +00:00
return ctx.HTML(components.QuotesScrollable(quotes, user))
2017-12-03 21:33:24 +00:00
}