100 lines
2.2 KiB
Go
Raw Normal View History

2016-11-19 23:54:31 +09:00
package dashboard
import (
2017-06-28 21:32:13 +02:00
"sort"
2017-06-28 21:17:49 +02:00
2016-11-19 23:54:31 +09:00
"github.com/aerogo/aero"
2017-06-26 12:50:15 -03:00
"github.com/aerogo/flow"
2017-06-08 01:14:45 +02:00
"github.com/animenotifier/arn"
"github.com/animenotifier/notify.moe/components"
2017-06-15 23:03:55 +02:00
"github.com/animenotifier/notify.moe/pages/frontpage"
2017-06-07 23:37:13 +02:00
"github.com/animenotifier/notify.moe/utils"
2016-11-19 23:54:31 +09:00
)
2016-12-06 12:36:31 +09:00
const maxPosts = 5
const maxFollowing = 5
2017-06-27 16:51:27 +02:00
const maxSoundTracks = 5
2017-06-28 21:17:49 +02:00
const maxScheduleItems = 5
2017-06-26 19:03:48 +02:00
// Get the dashboard or the frontpage when logged out.
2016-11-19 23:54:31 +09:00
func Get(ctx *aero.Context) string {
2017-06-08 01:14:45 +02:00
user := utils.GetUser(ctx)
2017-06-15 23:03:55 +02:00
if user == nil {
return frontpage.Get(ctx)
}
2017-06-26 19:03:48 +02:00
return dashboard(ctx)
2017-06-26 12:50:15 -03:00
}
2017-06-07 21:12:59 +02:00
2017-06-26 19:03:48 +02:00
// Render the dashboard.
func dashboard(ctx *aero.Context) string {
2017-06-29 20:52:30 +02:00
var forumActivity []arn.Postable
2017-06-26 13:53:06 -03:00
var userList interface{}
var followingList []*arn.User
2017-06-27 16:51:27 +02:00
var soundTracks []*arn.SoundTrack
2017-06-28 21:17:49 +02:00
var upcomingEpisodes []*arn.UpcomingEpisode
2017-06-26 19:03:48 +02:00
2017-06-26 13:58:02 -03:00
user := utils.GetUser(ctx)
2017-06-07 21:12:59 +02:00
2017-06-26 12:50:15 -03:00
flow.Parallel(func() {
2017-06-29 20:52:30 +02:00
forumActivity, _ = arn.GetForumActivityCached()
2017-06-28 21:17:49 +02:00
}, func() {
2017-07-17 22:43:26 +02:00
animeList, err := arn.GetAnimeList(user.ID)
2017-06-28 21:17:49 +02:00
if err != nil {
return
}
2017-07-05 15:50:29 +02:00
animeList = animeList.Watching()
2017-07-05 14:34:33 +02:00
animeList.PrefetchAnime()
2017-06-28 21:17:49 +02:00
for _, item := range animeList.Items {
2017-07-05 14:34:33 +02:00
futureEpisodes := item.Anime().UpcomingEpisodes()
2017-06-29 08:32:46 +02:00
if len(futureEpisodes) == 0 {
continue
2017-06-28 21:17:49 +02:00
}
2017-06-29 08:32:46 +02:00
upcomingEpisodes = append(upcomingEpisodes, futureEpisodes...)
2017-06-28 21:17:49 +02:00
}
2017-06-28 21:32:13 +02:00
sort.Slice(upcomingEpisodes, func(i, j int) bool {
return upcomingEpisodes[i].Episode.AiringDate.Start < upcomingEpisodes[j].Episode.AiringDate.Start
})
2017-07-10 20:51:06 +02:00
if len(upcomingEpisodes) >= maxScheduleItems {
upcomingEpisodes = upcomingEpisodes[:maxScheduleItems]
}
2017-06-27 12:39:41 +02:00
}, func() {
2017-06-27 16:51:27 +02:00
var err error
soundTracks, err = arn.AllSoundTracks()
if err != nil {
return
}
arn.SortSoundTracksLatestFirst(soundTracks)
if len(soundTracks) > maxSoundTracks {
soundTracks = soundTracks[:maxSoundTracks]
}
2017-06-26 13:53:06 -03:00
}, func() {
2017-06-27 16:51:27 +02:00
var err error
2017-06-27 00:14:47 +02:00
userList, err = arn.DB.GetMany("User", user.Following)
2017-06-27 16:51:27 +02:00
if err != nil {
return
}
2017-06-26 13:53:06 -03:00
followingList = userList.([]*arn.User)
followingList = arn.SortUsersLastSeen(followingList)
if len(followingList) > maxFollowing {
followingList = followingList[:maxFollowing]
}
})
2017-06-29 18:39:42 +02:00
return ctx.HTML(components.Dashboard(upcomingEpisodes, forumActivity, soundTracks, followingList))
2016-11-19 23:54:31 +09:00
}