parcoursmob/handlers/api/calendars.go

122 lines
3.5 KiB
Go
Raw Normal View History

package api
import (
"net/http"
"time"
"git.coopgo.io/coopgo-apps/parcoursmob/services"
ics "github.com/arran4/golang-ical"
"github.com/gorilla/mux"
"github.com/rs/zerolog/log"
)
func (h *APIHandler) icsCalendar(events []services.AgendaEvent) (*ics.Calendar, error) {
calendar := ics.NewCalendarFor(h.config.GetString("service_name"))
for _, e := range events {
vevent := ics.NewEvent(e.ID)
vevent.SetSummary(e.Name)
vevent.SetDescription(e.Description)
if e.Allday {
vevent.SetAllDayStartAt(e.Startdate)
if e.Enddate.After(e.Startdate) {
vevent.SetAllDayEndAt(e.Enddate.Add(24 * time.Hour))
}
} else {
timeloc, err := time.LoadLocation("Europe/Paris")
if err != nil {
log.Error().Err(err).Msg("Tried to load timezone location Europe/Paris. Error. Missing zones in container ?")
return nil, err
}
starttime, err := time.ParseInLocation("15:04", e.Starttime, timeloc)
if err != nil {
return nil, err
}
startdatetime := time.Date(e.Startdate.Year(), e.Startdate.Month(), e.Startdate.Day(), starttime.Hour(), starttime.Minute(), 0, 0, timeloc)
endtime, err := time.Parse("15:04", e.Endtime)
if err != nil {
return nil, err
}
enddatetime := time.Date(e.Enddate.Year(), e.Enddate.Month(), e.Enddate.Day(), endtime.Hour(), endtime.Minute(), 0, 0, timeloc)
vevent.SetStartAt(startdatetime)
vevent.SetEndAt(enddatetime)
}
calendar.AddVEvent(vevent)
}
return calendar, nil
}
func (h *APIHandler) CalendarGlobal(w http.ResponseWriter, r *http.Request) {
enabled := h.config.GetBool("modules.agenda.enabled") && h.config.GetBool("modules.agenda.calendars.global.enabled")
if !enabled {
log.Error().Msg("global calendar not activated in configuration")
w.WriteHeader(http.StatusForbidden)
return
}
events, err := h.services.GetAgendaEvents()
if err != nil {
log.Error().Err(err).Msg("error retrieving agenda events")
w.WriteHeader(http.StatusInternalServerError)
return
}
calendar, err := h.icsCalendar(events)
if err != nil {
log.Error().Err(err).Msg("error while creating ics calendar")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/calendar")
w.Write([]byte(calendar.Serialize()))
}
func (h *APIHandler) CalendarOrganizations(w http.ResponseWriter, r *http.Request) {
enabled := h.config.GetBool("modules.agenda.enabled") && h.config.GetBool("modules.agenda.calendars.organizations.enabled")
if !enabled {
log.Error().Msg("organizations calendar not activated in configuration")
w.WriteHeader(http.StatusForbidden)
return
}
// TODO set additional calendar rights in group configuration to prevent default behavior ?
vars := mux.Vars(r)
groupid := vars["groupid"]
events, err := h.services.GetAgendaEvents()
if err != nil {
log.Error().Err(err).Msg("error retrieving agenda events")
w.WriteHeader(http.StatusInternalServerError)
return
}
filteredEvents := []services.AgendaEvent{}
for _, e := range events {
for _, g := range e.Owners {
log.Debug().Str("groupid", groupid).Str("g", g).Msg("check identical")
if g == groupid {
filteredEvents = append(filteredEvents, e)
continue
}
}
}
calendar, err := h.icsCalendar(filteredEvents)
if err != nil {
log.Error().Err(err).Msg("error while creating ics calendar")
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "text/calendar")
w.Write([]byte(calendar.Serialize()))
}