package wssessions import ( "clickandjoin.app/managementsystem/modules/cache" "clickandjoin.app/managementsystem/modules/structs" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/utils" ) func AddUserWebSocketSessionId(c *fiber.Ctx) error { // swagger:operation POST /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsAddUserWebSocketSessionId // --- // summary: Add user websocket session // parameters: // - name: X-Ms-Api // in: header // required: true // responses: // '200': // description: Added user web socket session userId := utils.CopyString(c.Params("userId")) wsSessionId := utils.CopyString(c.Params("wsSessionId")) if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok { if isInList(userId, wsSessionId) { return c.SendStatus(fiber.StatusUnprocessableEntity) } cache.WebSocketSessions[userId] = append(webSocketSessions, wsSessionId) } else { cache.WebSocketSessions[userId] = []string{wsSessionId} } return c.SendStatus(fiber.StatusOK) } func isInList(userId string, wsSessionId string) bool { for _, item := range cache.WebSocketSessions[userId] { if item == wsSessionId { return true } } return false } func ExistsUserWebSocketSessionId(c *fiber.Ctx) error { // swagger:operation GET /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsExistsUserWebSocketSessionId // --- // summary: Returns whether the user websocket session already exists // produces: // - application/json // parameters: // - name: X-Ms-Api // in: header // required: true // responses: // '200': // description: Added user web socket session return c.JSON(structs.ExistsUserWebSocketSessionIdResponse{E: isInList(c.Params("userId"), c.Params("wsSessionId"))}) } func GetUserWebSocketSessionIds(c *fiber.Ctx) error { // swagger:operation GET /wsconnections/:userId wsconnections wsconnectionsGetUserWebSocketSessionIds // --- // summary: List of all websocket sessions of the user that are connected // produces: // - application/json // parameters: // - name: X-Ms-Api // in: header // required: true // responses: // '200': // description: List of all websocket sessions of the user that are connected // schema: // "$ref": "#/definitions/GetUserWebSocketSessionIdsResponse" var list []string if webSocketSessions, ok := cache.WebSocketSessions[c.Params("userId")]; ok { list = webSocketSessions } return c.JSON(structs.GetUserWebSocketSessionIdsResponse{WebSocketSessions: list}) } func RemoveConnection(c *fiber.Ctx) error { // swagger:operation DELETE /wsconnections/:userId/:wsSessionId wsconnections wsconnectionsRemoveConnection // --- // summary: Deletes a websocket session from the user // parameters: // - name: X-Ms-Api // in: header // required: true // responses: // '200': // description: Websocket session deleted userId := c.Params("userId") wsSessionId := c.Params("wsSessionId") if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok { for index, session := range webSocketSessions { if session == wsSessionId { cache.WebSocketSessions[userId] = remove(webSocketSessions, index) break } } } return c.SendStatus(fiber.StatusOK) } func remove(s []string, i int) []string { s[i] = s[0] return s[1:] }