42 lines
1.1 KiB
Go
Raw Normal View History

2017-12-02 20:53:01 +00:00
package companies
import (
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/utils"
2018-03-13 22:06:16 +00:00
"github.com/animenotifier/notify.moe/utils/infinitescroll"
2017-12-02 20:53:01 +00:00
)
2018-03-13 22:30:09 +00:00
const maxPopularCompanies = 60
2017-12-02 20:53:01 +00:00
2018-03-13 22:06:16 +00:00
// Popular renders the best companies.
2017-12-02 20:53:01 +00:00
func Popular(ctx *aero.Context) string {
user := utils.GetUser(ctx)
2018-03-13 22:06:16 +00:00
index, _ := ctx.GetInt("index")
2017-12-02 20:53:01 +00:00
2018-03-13 22:06:16 +00:00
// Fetch all eligible companies
allCompanies := fetchAll()
2017-12-02 20:53:01 +00:00
2018-03-13 22:06:16 +00:00
// Sort the companies by popularity
arn.SortCompaniesPopularFirst(allCompanies)
2017-12-02 20:53:01 +00:00
2018-03-13 22:06:16 +00:00
// Slice the part that we need
companies := allCompanies[index:]
2017-12-02 20:53:01 +00:00
2018-03-13 22:06:16 +00:00
if len(companies) > maxPopularCompanies {
companies = companies[:maxPopularCompanies]
2017-12-02 20:53:01 +00:00
}
2018-03-13 22:06:16 +00:00
// Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allCompanies), maxPopularCompanies, index)
2017-12-02 20:53:01 +00:00
2018-03-13 22:06:16 +00:00
// In case we're scrolling, send companies only (without the page frame)
if index > 0 {
return ctx.HTML(components.PopularCompaniesScrollable(companies, user))
2017-12-02 20:53:01 +00:00
}
2018-03-13 22:06:16 +00:00
// Otherwise, send the full page
return ctx.HTML(components.PopularCompanies(companies, nextIndex, user))
2017-12-02 20:53:01 +00:00
}