ManagementSystem/routers/api/v1/wssessions/wssessions.go

133 lines
3.5 KiB
Go

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 AddUserWebSocketSession(c *fiber.Ctx) error {
// swagger:operation POST /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsAddUserWebSocketSession
// ---
// summary: Add user websocket session
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: X-Ms-Api
// in: header
// required: true
// responses:
// '200':
// description: Added user web socket session
userId := utils.CopyString(c.Params("userId"))
webSocketSession := utils.CopyString(c.Params("webSocketSession"))
if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok {
if isInList(userId, webSocketSession) {
return c.SendStatus(fiber.StatusUnprocessableEntity)
}
cache.WebSocketSessions[userId] = append(webSocketSessions, webSocketSession)
} else {
cache.WebSocketSessions[userId] = []string{webSocketSession}
}
return c.SendStatus(fiber.StatusOK)
}
func isInList(userId string, webSocketSession string) bool {
for _, item := range cache.WebSocketSessions[userId] {
if item == webSocketSession {
return true
}
}
return false
}
func ExistsUserWebSocketSession(c *fiber.Ctx) error {
// swagger:operation GET /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsExistsUserWebSocketSession
// ---
// summary: Returns whether the user websocket session already exists
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: X-Ms-Api
// in: header
// required: true
// responses:
// '200':
// description: Added user web socket session
return c.JSON(structs.ExistsUserWebSocketSessionsResponse{E: isInList(c.Params("userId"), c.Params("webSocketSession"))})
}
func GetUserWebSocketSessions(c *fiber.Ctx) error {
// swagger:operation GET /wsconnections/:userId wsconnections wsconnectionsGetUserWebSocketSessions
// ---
// summary: List of all websocket sessions of the user that are connected
// consumes:
// - application/json
// 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/GetUserWebSocketSessionsResponse"
var list []string
if webSocketSessions, ok := cache.WebSocketSessions[c.Params("userId")]; ok {
list = webSocketSessions
}
return c.JSON(structs.GetUserWebSocketSessionsResponse{WebSocketSessions: list})
}
func RemoveConnection(c *fiber.Ctx) error {
// swagger:operation DELETE /wsconnections/:userId/:webSocketSession wsconnections wsconnectionsRemoveConnection
// ---
// summary: Deletes a websocket session from the user
// consumes:
// - application/json
// produces:
// - application/json
// parameters:
// - name: X-Ms-Api
// in: header
// required: true
// responses:
// '200':
// description: Websocket session deleted
userId := c.Params("userId")
webSocketSession := c.Params("webSocketSession")
if webSocketSessions, ok := cache.WebSocketSessions[userId]; ok {
for index, session := range webSocketSessions {
if session == webSocketSession {
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:]
}