87 lines
2.0 KiB
Go
Raw Normal View History

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