2017-12-03 21:33:24 +00:00
|
|
|
package quotes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aerogo/aero"
|
2019-06-03 09:32:43 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
2017-12-03 21:33:24 +00:00
|
|
|
"github.com/animenotifier/notify.moe/components"
|
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
2018-03-13 21:38:26 +00:00
|
|
|
"github.com/animenotifier/notify.moe/utils/infinitescroll"
|
2017-12-03 21:33:24 +00:00
|
|
|
)
|
|
|
|
|
2018-03-13 21:38:26 +00:00
|
|
|
const maxQuotes = 12
|
2018-01-17 22:30:55 +00:00
|
|
|
|
2018-03-13 21:38:26 +00:00
|
|
|
// Latest renders the latest quotes.
|
2019-06-01 04:55:49 +00:00
|
|
|
func Latest(ctx aero.Context) error {
|
2018-01-17 22:30:55 +00:00
|
|
|
user := utils.GetUser(ctx)
|
2018-03-13 21:38:26 +00:00
|
|
|
index, _ := ctx.GetInt("index")
|
2018-01-17 22:30:55 +00:00
|
|
|
|
2018-03-13 21:38:26 +00:00
|
|
|
// Fetch all eligible quotes
|
|
|
|
allQuotes := fetchAll()
|
2018-01-17 22:30:55 +00:00
|
|
|
|
2018-03-13 21:38:26 +00:00
|
|
|
// Sort the quotes by date
|
2018-01-17 22:30:55 +00:00
|
|
|
arn.SortQuotesLatestFirst(allQuotes)
|
|
|
|
|
2018-03-13 21:38:26 +00:00
|
|
|
// Slice the part that we need
|
2018-01-17 22:30:55 +00:00
|
|
|
quotes := allQuotes[index:]
|
|
|
|
|
|
|
|
if len(quotes) > maxQuotes {
|
|
|
|
quotes = quotes[:maxQuotes]
|
|
|
|
}
|
|
|
|
|
2018-03-13 21:38:26 +00:00
|
|
|
// Next index
|
|
|
|
nextIndex := infinitescroll.NextIndex(ctx, len(allQuotes), maxQuotes, index)
|
2018-01-17 22:30:55 +00:00
|
|
|
|
2018-03-13 21:38:26 +00:00
|
|
|
// In case we're scrolling, send quotes only (without the page frame)
|
|
|
|
if index > 0 {
|
|
|
|
return ctx.HTML(components.QuotesScrollable(quotes, user))
|
2018-01-17 22:30:55 +00:00
|
|
|
}
|
2017-12-03 21:33:24 +00:00
|
|
|
|
2018-03-13 21:38:26 +00:00
|
|
|
// Otherwise, send the full page
|
|
|
|
return ctx.HTML(components.Quotes(quotes, nextIndex, user))
|
2017-12-03 21:33:24 +00:00
|
|
|
}
|