mirror of
https://gitlab.dit.htwk-leipzig.de/htwk-software/htwkalender.git
synced 2025-07-16 09:38:49 +02:00
200 lines
5.3 KiB
Go
200 lines
5.3 KiB
Go
//Calendar implementation for the HTWK Leipzig timetable. Evaluation and display of the individual dates in iCal format.
|
|
//Copyright (C) 2024 HTWKalender support@htwkalender.de
|
|
|
|
//This program is free software: you can redistribute it and/or modify
|
|
//it under the terms of the GNU Affero General Public License as published by
|
|
//the Free Software Foundation, either version 3 of the License, or
|
|
//(at your option) any later version.
|
|
|
|
//This program is distributed in the hope that it will be useful,
|
|
//but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
//GNU Affero General Public License for more details.
|
|
|
|
//You should have received a copy of the GNU Affero General Public License
|
|
//along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
package service
|
|
|
|
import (
|
|
"github.com/labstack/echo/v5"
|
|
"github.com/pocketbase/pocketbase"
|
|
"github.com/pocketbase/pocketbase/apis"
|
|
"github.com/pocketbase/pocketbase/core"
|
|
"htwkalender/service/db"
|
|
"htwkalender/service/ical"
|
|
"io"
|
|
"net/http"
|
|
)
|
|
|
|
func addFeedRoutes(app *pocketbase.PocketBase) {
|
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
_, err := e.Router.AddRoute(echo.Route{
|
|
Method: http.MethodPost,
|
|
Path: "/api/createFeed",
|
|
Handler: func(c echo.Context) error {
|
|
requestBody, _ := io.ReadAll(c.Request().Body)
|
|
result, err := ical.CreateIndividualFeed(requestBody, app)
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, "Failed to create individual feed")
|
|
}
|
|
return c.JSON(http.StatusOK, result)
|
|
},
|
|
Middlewares: []echo.MiddlewareFunc{
|
|
apis.ActivityLogger(app),
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
|
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
_, err := e.Router.AddRoute(echo.Route{
|
|
Method: http.MethodGet,
|
|
Path: "/api/feed",
|
|
Handler: func(c echo.Context) error {
|
|
token := c.QueryParam("token")
|
|
result, err := ical.Feed(app, token)
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, "Failed to get feed")
|
|
}
|
|
var responseWriter = c.Response().Writer
|
|
responseWriter.Header().Set("Content-type", "text/calendar")
|
|
responseWriter.Header().Set("charset", "utf-8")
|
|
responseWriter.Header().Set("Content-Disposition", "inline")
|
|
responseWriter.Header().Set("filename", "calendar.ics")
|
|
|
|
responseWriter.WriteHeader(http.StatusOK)
|
|
_, err = responseWriter.Write([]byte(result))
|
|
if err != nil {
|
|
return c.JSON(http.StatusInternalServerError, "Failed to write feed")
|
|
}
|
|
c.Response().Writer = responseWriter
|
|
return nil
|
|
},
|
|
Middlewares: []echo.MiddlewareFunc{
|
|
apis.ActivityLogger(app),
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
|
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
_, err := e.Router.AddRoute(echo.Route{
|
|
Method: http.MethodDelete,
|
|
Path: "/api/feed",
|
|
Handler: func(c echo.Context) error {
|
|
token := c.QueryParam("token")
|
|
err := db.DeleteFeed(app.Dao(), token)
|
|
if err != nil {
|
|
return c.JSON(http.StatusNotFound, err)
|
|
} else {
|
|
return c.JSON(http.StatusOK, "Feed deleted")
|
|
}
|
|
},
|
|
Middlewares: []echo.MiddlewareFunc{
|
|
apis.ActivityLogger(app),
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
|
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
_, err := e.Router.AddRoute(echo.Route{
|
|
Method: http.MethodPut,
|
|
Path: "/api/feed",
|
|
Handler: func(c echo.Context) error {
|
|
token := c.QueryParam("token")
|
|
return c.JSON(http.StatusOK, "token: "+token)
|
|
},
|
|
Middlewares: []echo.MiddlewareFunc{
|
|
apis.ActivityLogger(app),
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
|
|
// Route for Thunderbird to get calendar server information
|
|
// Response is a 200 OK without additional content
|
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
_, err := e.Router.AddRoute(echo.Route{
|
|
Method: "PROPFIND",
|
|
Path: "/",
|
|
Handler: func(c echo.Context) error {
|
|
return c.JSON(http.StatusOK, "")
|
|
},
|
|
Middlewares: []echo.MiddlewareFunc{
|
|
apis.ActivityLogger(app),
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
|
|
// Route for Thunderbird to get calendar server information
|
|
// Response is a 200 OK without additional content
|
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
_, err := e.Router.AddRoute(echo.Route{
|
|
Method: "PROPFIND",
|
|
Path: "/.well-known/caldav",
|
|
Handler: func(c echo.Context) error {
|
|
return c.JSON(http.StatusOK, "")
|
|
},
|
|
Middlewares: []echo.MiddlewareFunc{
|
|
apis.ActivityLogger(app),
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
|
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
_, err := e.Router.AddRoute(echo.Route{
|
|
Method: "PROPFIND",
|
|
Path: "/api/feed",
|
|
Handler: func(c echo.Context) error {
|
|
return c.JSON(http.StatusOK, "")
|
|
},
|
|
Middlewares: []echo.MiddlewareFunc{
|
|
apis.ActivityLogger(app),
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
|
|
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
|
|
_, err := e.Router.AddRoute(echo.Route{
|
|
Method: http.MethodHead,
|
|
Path: "/api/feed",
|
|
Handler: func(c echo.Context) error {
|
|
return c.JSON(http.StatusOK, "")
|
|
},
|
|
Middlewares: []echo.MiddlewareFunc{
|
|
apis.ActivityLogger(app),
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
})
|
|
}
|