mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-30 08:19:14 +02:00
101 lines
3.2 KiB
Go
101 lines
3.2 KiB
Go
package ical
|
|
|
|
import (
|
|
"htwkalender/model"
|
|
"htwkalender/service/functions"
|
|
"htwkalender/service/names"
|
|
"time"
|
|
|
|
"github.com/jordic/goics"
|
|
)
|
|
|
|
// IcalModel local type for EmitICal function
|
|
type IcalModel struct {
|
|
Events model.Events
|
|
Mapping map[string]model.FeedCollection
|
|
}
|
|
|
|
// EmitICal implements the interface for goics
|
|
func (icalModel IcalModel) EmitICal() goics.Componenter {
|
|
europeTime, _ := time.LoadLocation("Europe/Berlin")
|
|
c := goics.NewComponent()
|
|
c.SetType("VCALENDAR")
|
|
c.AddProperty("VERSION", "2.0")
|
|
c.AddProperty("CALSCAL", "GREGORIAN")
|
|
c.AddProperty("TZID", "Europe/Berlin")
|
|
c.AddProperty("X-WR-CALNAME", "HTWK Kalender")
|
|
c.AddProperty("X-WR-TIMEZONE", "Europe/Berlin")
|
|
c.AddProperty("X-LIC-LOCATION", "Europe/Berlin")
|
|
for _, event := range icalModel.Events {
|
|
mapEntry, mappingFound := icalModel.Mapping[event.UUID]
|
|
|
|
s := goics.NewComponent()
|
|
s.SetType("VEVENT")
|
|
k, v := goics.FormatDateTime("DTEND;TZID=Europe/Berlin", event.End.Time().Local().In(europeTime))
|
|
s.AddProperty(k, v)
|
|
k, v = goics.FormatDateTime("DTSTART;TZID=Europe/Berlin", event.Start.Time().Local().In(europeTime))
|
|
s.AddProperty(k, v)
|
|
|
|
if mappingFound {
|
|
addPropertyIfNotEmpty(s, "SUMMARY", replaceNameIfUserDefined(&event, mapEntry))
|
|
addAlarmIfSpecified(s, event, mapEntry)
|
|
} else {
|
|
addPropertyIfNotEmpty(s, "SUMMARY", event.Name)
|
|
}
|
|
|
|
addPropertyIfNotEmpty(s, "DESCRIPTION", generateDescription(event))
|
|
addPropertyIfNotEmpty(s, "LOCATION", event.Rooms)
|
|
c.AddComponent(s)
|
|
}
|
|
return c
|
|
}
|
|
|
|
// if reminder is specified in the configuration for this event, an alarm will be added to the event
|
|
func addAlarmIfSpecified(s *goics.Component, event model.Event, mapping model.FeedCollection) {
|
|
if mapping.Reminder {
|
|
a := goics.NewComponent()
|
|
a.SetType("VALARM")
|
|
a.AddProperty("TRIGGER", "-PT15M")
|
|
a.AddProperty("ACTION", "DISPLAY")
|
|
a.AddProperty("DESCRIPTION", "Next course: "+replaceNameIfUserDefined(&event, mapping)+" in "+event.Rooms)
|
|
s.AddComponent(a)
|
|
}
|
|
}
|
|
|
|
// replaceNameIfUserDefined replaces the name of the event with the user defined name if it is not empty
|
|
// all contained template strings will be replaced with the corresponding values from the event
|
|
func replaceNameIfUserDefined(event *model.Event, mapping model.FeedCollection) string {
|
|
if !functions.OnlyWhitespace(mapping.UserDefinedName) {
|
|
return names.ReplaceTemplateSubStrings(mapping.UserDefinedName, *event)
|
|
}
|
|
|
|
return event.Name
|
|
}
|
|
|
|
// AddPropertyIfNotEmpty adds a property to the component if the value is not empty
|
|
// or contains only whitespaces
|
|
func addPropertyIfNotEmpty(component *goics.Component, key string, value string) {
|
|
if !functions.OnlyWhitespace(value) {
|
|
component.AddProperty(key, value)
|
|
}
|
|
}
|
|
|
|
func generateDescription(event model.Event) string {
|
|
var description string
|
|
|
|
if !functions.OnlyWhitespace(event.Notes) {
|
|
description += "Notizen: " + event.Notes + "\n"
|
|
}
|
|
if !functions.OnlyWhitespace(event.Prof) {
|
|
description += "Prof: " + event.Prof + "\n"
|
|
}
|
|
if !functions.OnlyWhitespace(event.Course) {
|
|
description += "Gruppe: " + event.Course + "\n"
|
|
}
|
|
if !functions.OnlyWhitespace(event.EventType) {
|
|
description += "Typ: " + event.EventType + event.Compulsory + "\n"
|
|
}
|
|
|
|
return description
|
|
}
|