112 lines
2.9 KiB
Go
Raw Normal View History

2017-07-05 14:52:24 +00:00
package animelist
import (
"net/http"
2018-03-14 19:50:32 +00:00
"strconv"
2017-07-05 14:52:24 +00:00
"github.com/aerogo/aero"
2019-06-03 09:32:43 +00:00
"github.com/animenotifier/notify.moe/arn"
2019-06-01 04:55:49 +00:00
"github.com/animenotifier/notify.moe/assets"
2017-07-05 14:52:24 +00:00
"github.com/animenotifier/notify.moe/components"
"github.com/animenotifier/notify.moe/server/middleware"
"github.com/animenotifier/notify.moe/utils/infinitescroll"
2017-07-05 14:52:24 +00:00
)
2018-03-14 19:35:00 +00:00
const (
2018-03-14 20:05:47 +00:00
animeFirstLoad = 60
animePerScroll = 40
2018-03-14 19:35:00 +00:00
)
2019-08-28 02:50:26 +00:00
// Filter filters a user's anime list item by the status.
func Filter(ctx aero.Context) error {
2019-11-17 07:59:34 +00:00
user := arn.GetUserFromContext(ctx)
2019-08-28 02:50:26 +00:00
status := ctx.Get("status")
2019-08-29 03:23:58 +00:00
sortBy := arn.SortByRating
if user != nil {
sortBy = user.Settings().SortBy
}
return AnimeList(ctx, user, status, sortBy)
2017-07-05 14:52:24 +00:00
}
2018-03-14 19:23:45 +00:00
// AnimeList renders the anime list items.
2019-08-29 03:23:58 +00:00
func AnimeList(ctx aero.Context, user *arn.User, status string, sortBy string) error {
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 {
2018-07-07 03:42:00 +00:00
return ctx.Error(http.StatusNotFound, "Anime list not found")
2017-07-05 14:52:24 +00:00
}
2018-04-19 22:06:13 +00:00
// Filter private items
if user == nil || user.ID != viewUser.ID {
animeList = animeList.WithoutPrivateItems()
2018-04-19 22:06:13 +00:00
}
statusLists := animeList.SplitByStatus()
// Sort the items for the requested status only
animeList = statusLists[status]
animeList.Sort(sortBy)
2019-08-29 03:23:58 +00:00
// These are all anime list items for the given status
allItems := statusLists[status].Items
// Slice the part that we need
2019-11-18 05:17:00 +00:00
var items []*arn.AnimeListItem
if index < len(allItems) {
items = allItems[index:]
}
2018-03-14 19:35:00 +00:00
maxLength := animeFirstLoad
if index > 0 {
maxLength = animePerScroll
}
2018-03-14 19:35:00 +00:00
if len(items) > maxLength {
items = items[:maxLength]
}
// Next index
2018-03-14 19:35:00 +00:00
nextIndex := infinitescroll.NextIndex(ctx, len(allItems), maxLength, index)
2018-03-14 19:50:32 +00:00
// OpenGraph data
2019-06-01 04:55:49 +00:00
customCtx := ctx.(*middleware.OpenGraphContext)
customCtx.OpenGraph = &arn.OpenGraph{
2018-03-14 19:50:32 +00:00
Tags: map[string]string{
"og:title": viewUser.Nick + "'s anime list",
"og:image": "https:" + viewUser.AvatarLink("large"),
2019-06-01 04:55:49 +00:00
"og:url": "https://" + assets.Domain + viewUser.Link(),
2018-03-14 19:50:32 +00:00
"og:site_name": "notify.moe",
"og:description": strconv.Itoa(len(animeList.Items)) + " anime",
// The OpenGraph type "profile" is meant for real-life persons but I think it's okay in this context.
// An alternative would be to use "article" which is mostly used for blog posts and news.
"og:type": "profile",
},
Meta: map[string]string{
"description": viewUser.Nick + "'s anime list",
"keywords": "anime list",
},
}
// 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.AnimeListPage(items, nextIndex, viewUser, user, statusLists))
2017-07-05 14:52:24 +00:00
}