72 lines
1.7 KiB
Go
Raw Normal View History

2017-07-05 14:52:24 +00:00
package animelist
import (
"net/http"
"github.com/aerogo/aero"
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/pages/frontpage"
2017-07-05 14:52:24 +00:00
"github.com/animenotifier/notify.moe/utils"
"github.com/animenotifier/notify.moe/utils/infinitescroll"
2017-07-05 14:52:24 +00:00
)
const maxAnimeListItems = 50
2017-07-05 14:52:24 +00:00
// FilterByStatus returns a handler for the given anime list item status.
func FilterByStatus(status string) aero.Handle {
return func(ctx *aero.Context) string {
user := utils.GetUser(ctx)
if user == nil {
return frontpage.Get(ctx)
2017-07-05 14:52:24 +00:00
}
2018-03-14 19:23:45 +00:00
return AnimeList(ctx, user, status)
2017-07-05 14:52:24 +00:00
}
}
2018-03-14 19:23:45 +00:00
// AnimeList renders the anime list items.
func AnimeList(ctx *aero.Context, user *arn.User, status string) string {
2017-07-05 14:52:24 +00:00
nick := ctx.Get("nick")
index, _ := ctx.GetInt("index")
2017-07-05 14:52:24 +00:00
viewUser, err := arn.GetUserByNick(nick)
if err != nil {
return ctx.Error(http.StatusNotFound, "User not found", err)
2017-07-05 14:52:24 +00:00
}
// Fetch all eligible items
2017-07-05 14:52:24 +00:00
animeList := viewUser.AnimeList()
if animeList == nil {
return ctx.Error(http.StatusNotFound, "Anime list not found", nil)
2017-07-05 14:52:24 +00:00
}
animeList = animeList.FilterStatus(status)
// Sort the items
animeList.Sort()
// These are all animer list items for the given status
allItems := animeList.Items
// Slice the part that we need
items := allItems[index:]
if len(items) > maxAnimeListItems {
items = items[:maxAnimeListItems]
}
// Next index
nextIndex := infinitescroll.NextIndex(ctx, len(allItems), maxAnimeListItems, index)
// In case we're scrolling, send items only (without the page frame)
if index > 0 {
return ctx.HTML(components.AnimeListScrollable(items, viewUser, user))
}
2017-07-05 14:52:24 +00:00
// Otherwise, send the full page
return ctx.HTML(components.HomeAnimeList(items, nextIndex, viewUser, user, status))
2017-07-05 14:52:24 +00:00
}