mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-08-11 14:13:49 +02:00
feat:#16 added room ics api route
This commit is contained in:
@@ -25,7 +25,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func GetEvents(modules []model.FeedModule, conn *grpc.ClientConn) (model.Events, error) {
|
||||
func GetEventsByModules(modules []model.FeedModule, conn *grpc.ClientConn) (model.Events, error) {
|
||||
c := pb.NewModuleServiceClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
@@ -48,3 +48,21 @@ func GetEvents(modules []model.FeedModule, conn *grpc.ClientConn) (model.Events,
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func GetEventsByRoom(room string, conn *grpc.ClientConn) (model.Events, error) {
|
||||
c := pb.NewRoomServiceClient(conn)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
defer cancel()
|
||||
|
||||
r, err := c.GetRoomEvents(ctx, &pb.GetRoomRequest{Room: room})
|
||||
if err != nil {
|
||||
slog.Error("could not get room events: ", "error", err)
|
||||
}
|
||||
|
||||
events := make(model.Events, 0)
|
||||
for _, event := range r.GetEvents() {
|
||||
events = append(events, protoToEvent(event))
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
@@ -54,7 +54,7 @@ func Feed(app model.AppType, token string, userAgent string) (string, string, er
|
||||
}
|
||||
|
||||
// Get all events for modules
|
||||
events, err = htwkalenderGrpc.GetEvents(feed.Modules, app.GrpcClient)
|
||||
events, err = htwkalenderGrpc.GetEventsByModules(feed.Modules, app.GrpcClient)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
@@ -106,3 +106,23 @@ func CreateFeed(app model.AppType, modules []model.FeedCollection) (string, erro
|
||||
|
||||
return token, nil
|
||||
}
|
||||
|
||||
func FeedRoom(app model.AppType, room string) (string, string, error) {
|
||||
|
||||
// Get all events for room
|
||||
events, err := htwkalenderGrpc.GetEventsByRoom(room, app.GrpcClient)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
|
||||
// Sort events by start date
|
||||
events.Sort()
|
||||
|
||||
// Generate one Hash for E-TAG from all events
|
||||
etag := functions.HashString(events.String())
|
||||
|
||||
cal := GenerateIcalFeed(events, map[string]model.FeedCollection{}, "")
|
||||
icalFeed := &model.FeedModel{Content: cal.Serialize(), ExpiresAt: model.JSONTime(time.Now().Add(expirationTime))}
|
||||
|
||||
return icalFeed.Content, etag, nil
|
||||
}
|
||||
|
@@ -120,6 +120,27 @@ func AddFeedRoutes(app model.AppType) {
|
||||
return c.JSON(http.StatusOK, "token: "+token)
|
||||
})
|
||||
|
||||
app.Fiber.Get("/api/feed/room", func(c fiber.Ctx) error {
|
||||
room := c.Query("id")
|
||||
ifNoneMatch := c.Get("If-None-Match")
|
||||
|
||||
results, etag, err := ical.FeedRoom(app, room)
|
||||
if err != nil {
|
||||
slog.Error("Failed to get feed", "error", err, "room", room)
|
||||
return c.SendStatus(fiber.StatusNotFound)
|
||||
}
|
||||
|
||||
if ifNoneMatch == etag && ifNoneMatch != "" {
|
||||
return c.SendStatus(fiber.StatusNotModified)
|
||||
}
|
||||
|
||||
c.Response().Header.Set("Content-type", "text/calendar")
|
||||
c.Response().Header.Set("charset", "utf-8")
|
||||
c.Response().Header.Set("Content-Disposition", "inline")
|
||||
c.Response().Header.Set("filename", "calendar.ics")
|
||||
return c.SendString(results)
|
||||
})
|
||||
|
||||
app.Fiber.Head("/api/feed", func(c fiber.Ctx) error {
|
||||
return c.JSON(http.StatusOK, "")
|
||||
})
|
||||
|
Reference in New Issue
Block a user