45 lines
1.2 KiB
Go
Raw Normal View History

2017-12-02 20:53:01 +00:00
package companies
import (
"github.com/aerogo/aero"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2017-12-02 20:53:01 +00:00
"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-23 18:24:05 +00:00
const maxPopularCompanies = 10
2017-12-02 20:53:01 +00:00
2018-03-13 22:06:16 +00:00
// Popular renders the best companies.
2019-06-01 04:55:49 +00:00
func Popular(ctx aero.Context) error {
2017-12-02 20:53:01 +00:00
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-23 18:04:06 +00:00
// Get company to anime map
companyToAnime := arn.GetCompanyToAnimeMap()
2018-03-13 22:06:16 +00:00
// In case we're scrolling, send companies only (without the page frame)
if index > 0 {
2018-03-23 18:04:06 +00:00
return ctx.HTML(components.PopularCompaniesScrollable(companies, companyToAnime, user))
2017-12-02 20:53:01 +00:00
}
2018-03-13 22:06:16 +00:00
// Otherwise, send the full page
2018-03-23 18:04:06 +00:00
return ctx.HTML(components.PopularCompanies(companies, companyToAnime, nextIndex, user))
2017-12-02 20:53:01 +00:00
}