neko/internal/api/room/control.go

94 lines
2.0 KiB
Go
Raw Normal View History

2020-10-29 22:23:30 +01:00
package room
2020-11-16 22:48:20 +01:00
import (
"net/http"
2020-12-26 23:53:04 +01:00
"github.com/go-chi/chi"
2022-03-20 11:43:00 +01:00
"gitlab.com/demodesk/neko/server/pkg/auth"
"gitlab.com/demodesk/neko/server/pkg/utils"
2020-11-16 22:48:20 +01:00
)
2020-11-18 23:14:28 +01:00
type ControlStatusPayload struct {
2021-02-14 14:40:17 +01:00
HasHost bool `json:"has_host"`
HostId string `json:"host_id,omitempty"`
2020-11-18 23:14:28 +01:00
}
2020-11-18 23:05:38 +01:00
type ControlTargetPayload struct {
ID string `json:"id"`
}
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) controlStatus(w http.ResponseWriter, r *http.Request) error {
2020-11-18 23:14:28 +01:00
host := h.sessions.GetHost()
2021-09-17 00:58:50 +02:00
if host != nil {
return utils.HttpSuccess(w, ControlStatusPayload{
2020-11-18 23:14:28 +01:00
HasHost: true,
2021-02-14 14:40:17 +01:00
HostId: host.ID(),
2020-11-18 23:14:28 +01:00
})
}
2021-09-17 00:58:50 +02:00
return utils.HttpSuccess(w, ControlStatusPayload{
HasHost: false,
})
2020-11-18 23:14:28 +01:00
}
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) controlRequest(w http.ResponseWriter, r *http.Request) error {
2020-11-16 22:48:20 +01:00
host := h.sessions.GetHost()
if host != nil {
2021-09-17 00:58:50 +02:00
return utils.HttpUnprocessableEntity("there is already a host")
2020-11-16 22:48:20 +01:00
}
2021-09-17 00:58:50 +02:00
session, _ := auth.GetSession(r)
2020-11-16 22:48:20 +01:00
h.sessions.SetHost(session)
2021-09-17 00:58:50 +02:00
return utils.HttpSuccess(w)
2020-11-16 22:48:20 +01:00
}
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) controlRelease(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)
2020-11-16 22:48:20 +01:00
if !session.IsHost() {
2021-09-17 00:58:50 +02:00
return utils.HttpUnprocessableEntity("session is not the host")
2020-12-06 18:50:41 +01:00
}
2020-12-02 10:46:00 +01:00
h.desktop.ResetKeys()
2020-11-16 22:48:20 +01:00
h.sessions.ClearHost()
2021-02-02 18:28:32 +01:00
2021-09-17 00:58:50 +02:00
return utils.HttpSuccess(w)
2020-11-16 22:48:20 +01:00
}
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) controlTake(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)
h.sessions.SetHost(session)
2021-09-17 00:58:50 +02:00
return utils.HttpSuccess(w)
}
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) controlGive(w http.ResponseWriter, r *http.Request) error {
2021-03-13 23:32:10 +01:00
sessionId := chi.URLParam(r, "sessionId")
2021-03-13 23:32:10 +01:00
target, ok := h.sessions.Get(sessionId)
if !ok {
2021-09-17 00:58:50 +02:00
return utils.HttpNotFound("target session was not found")
2020-12-06 18:50:41 +01:00
}
2021-03-14 00:45:51 +01:00
if !target.Profile().CanHost {
2021-09-17 00:58:50 +02:00
return utils.HttpBadRequest("target session is not allowed to host")
}
h.sessions.SetHost(target)
2021-09-17 00:58:50 +02:00
return utils.HttpSuccess(w)
}
2020-11-29 15:58:26 +01:00
2021-09-17 00:58:50 +02:00
func (h *RoomHandler) controlReset(w http.ResponseWriter, r *http.Request) error {
2020-11-29 15:58:26 +01:00
host := h.sessions.GetHost()
2021-09-17 00:58:50 +02:00
if host != nil {
h.desktop.ResetKeys()
h.sessions.ClearHost()
}
2020-12-02 10:46:00 +01:00
2021-09-17 00:58:50 +02:00
return utils.HttpSuccess(w)
2020-11-29 15:58:26 +01:00
}