2017-11-22 10:51:59 +00:00
|
|
|
package calendar
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aerogo/aero"
|
2019-06-03 09:32:43 +00:00
|
|
|
"github.com/animenotifier/notify.moe/arn"
|
|
|
|
"github.com/animenotifier/notify.moe/arn/validate"
|
2017-11-22 10:51:59 +00:00
|
|
|
"github.com/animenotifier/notify.moe/components"
|
|
|
|
"github.com/animenotifier/notify.moe/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
var weekdayNames = []string{
|
|
|
|
"Sunday",
|
|
|
|
"Monday",
|
|
|
|
"Tuesday",
|
|
|
|
"Wednesday",
|
|
|
|
"Thursday",
|
|
|
|
"Friday",
|
|
|
|
"Saturday",
|
|
|
|
}
|
|
|
|
|
2019-09-10 00:49:51 +00:00
|
|
|
// Get renders the calendar page.
|
2019-06-01 04:55:49 +00:00
|
|
|
func Get(ctx aero.Context) error {
|
2019-11-17 07:59:34 +00:00
|
|
|
user := arn.GetUserFromContext(ctx)
|
2017-11-22 10:51:59 +00:00
|
|
|
oneWeek := 7 * 24 * time.Hour
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
year, month, day := now.Date()
|
|
|
|
now = time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
|
|
|
|
|
|
|
|
// Weekday index that we start with, Sunday is 0.
|
|
|
|
weekdayIndex := int(now.Weekday())
|
|
|
|
|
|
|
|
// Create days
|
2018-06-30 07:50:53 +00:00
|
|
|
days := make([]*utils.CalendarDay, 7)
|
2017-11-22 10:51:59 +00:00
|
|
|
|
|
|
|
for i := 0; i < 7; i++ {
|
|
|
|
days[i] = &utils.CalendarDay{
|
|
|
|
Name: weekdayNames[(weekdayIndex+i)%7],
|
2017-12-03 18:08:05 +00:00
|
|
|
Class: "weekday",
|
2017-11-22 10:51:59 +00:00
|
|
|
Entries: []*utils.CalendarEntry{},
|
|
|
|
}
|
2017-12-03 18:08:05 +00:00
|
|
|
|
|
|
|
if days[i].Name == "Saturday" || days[i].Name == "Sunday" {
|
|
|
|
days[i].Class += " weekend"
|
|
|
|
}
|
2017-11-22 10:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add anime episodes to the days
|
2019-08-28 08:06:42 +00:00
|
|
|
for episode := range arn.StreamEpisodes() {
|
|
|
|
if episode.Anime().Status == "finished" {
|
2018-01-27 17:44:21 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-28 08:06:42 +00:00
|
|
|
if !validate.DateTime(episode.AiringDate.Start) {
|
|
|
|
continue
|
|
|
|
}
|
2017-11-22 10:51:59 +00:00
|
|
|
|
2019-08-28 08:06:42 +00:00
|
|
|
// Since we validated the date earlier, we can ignore the error value.
|
|
|
|
airingDate, _ := time.Parse(time.RFC3339, episode.AiringDate.Start)
|
2017-11-22 12:04:01 +00:00
|
|
|
|
2019-08-28 08:06:42 +00:00
|
|
|
// Subtract from the starting date offset.
|
|
|
|
since := airingDate.Sub(now)
|
2017-11-22 10:51:59 +00:00
|
|
|
|
2019-08-28 08:06:42 +00:00
|
|
|
// Ignore entries in the past and more than 1 week away.
|
|
|
|
if since < 0 || since >= oneWeek {
|
|
|
|
continue
|
|
|
|
}
|
2017-11-22 11:48:18 +00:00
|
|
|
|
2019-08-28 08:06:42 +00:00
|
|
|
dayIndex := int(since / (24 * time.Hour))
|
2017-11-22 11:48:18 +00:00
|
|
|
|
2019-08-28 08:06:42 +00:00
|
|
|
entry := &utils.CalendarEntry{
|
|
|
|
Anime: episode.Anime(),
|
|
|
|
Episode: episode,
|
|
|
|
Added: false,
|
|
|
|
}
|
2017-11-22 11:48:18 +00:00
|
|
|
|
2019-08-28 08:06:42 +00:00
|
|
|
if user != nil {
|
|
|
|
animeListItem := user.AnimeList().Find(entry.Anime.ID)
|
2017-11-22 11:48:18 +00:00
|
|
|
|
2019-08-28 08:06:42 +00:00
|
|
|
if animeListItem != nil && (animeListItem.Status == arn.AnimeListStatusWatching || animeListItem.Status == arn.AnimeListStatusPlanned) {
|
|
|
|
entry.Added = true
|
2017-11-22 11:48:18 +00:00
|
|
|
}
|
2017-11-22 10:51:59 +00:00
|
|
|
}
|
2019-08-28 08:06:42 +00:00
|
|
|
|
|
|
|
days[dayIndex].Entries = append(days[dayIndex].Entries, entry)
|
2017-11-22 10:51:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < 7; i++ {
|
2019-06-07 01:38:14 +00:00
|
|
|
// nolint:scopelint
|
2017-11-22 10:51:59 +00:00
|
|
|
sort.Slice(days[i].Entries, func(a, b int) bool {
|
|
|
|
airingA := days[i].Entries[a].Episode.AiringDate.Start
|
|
|
|
airingB := days[i].Entries[b].Episode.AiringDate.Start
|
|
|
|
|
|
|
|
if airingA == airingB {
|
|
|
|
return days[i].Entries[a].Anime.Title.Canonical < days[i].Entries[b].Anime.Title.Canonical
|
|
|
|
}
|
|
|
|
|
|
|
|
return airingA < airingB
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.HTML(components.Calendar(days, user))
|
|
|
|
}
|