package calendar import ( "ArinDash/config" "sort" "time" "github.com/arran4/golang-ical" ) type configFile struct { Calendar calendarConfig } type calendarConfig struct { ICS []ICS Icon string } type ICS struct { Icon string URL string } type Event struct { UID string Summary string Description string Start time.Time End time.Time Location string Icon string } func FetchEvents() []Event { cfg := &configFile{} config.LoadConfig(cfg) events := make([]Event, 0) for _, ic := range cfg.Calendar.ICS { cal, err := ics.ParseCalendarFromUrl(ic.URL) if err != nil { panic(err) } for _, event := range cal.Events() { startAt, err := event.GetStartAt() if err != nil { startAt = time.Now() } endAt, err := event.GetEndAt() if err != nil { endAt = startAt } events = append(events, Event{ UID: getValue(event, ics.ComponentPropertyUniqueId), Icon: ic.Icon, Summary: getValue(event, ics.ComponentPropertySummary), Description: getValue(event, ics.ComponentPropertyDescription), Start: startAt, End: endAt, Location: getValue(event, ics.ComponentPropertyLocation), }) } } result := filter(events, func(i Event) bool { return i.End.After(time.Now()) }) sort.Slice(result, func(i, j int) bool { return result[i].Start.Before(result[j].Start) }) return result } func getValue(event *ics.VEvent, property ics.ComponentProperty) string { ianaProperty := event.GetProperty(property) if ianaProperty == nil { return "" } return ianaProperty.Value } func filter[T any](ss []T, test func(T) bool) (ret []T) { for _, s := range ss { if test(s) { ret = append(ret, s) } } return }