diff --git a/internal/api/room/control.go b/internal/api/room/control.go index c8fb414f..44c6f71b 100644 --- a/internal/api/room/control.go +++ b/internal/api/room/control.go @@ -9,6 +9,10 @@ import ( "demodesk/neko/internal/http/auth" ) +type ControlGivePayload struct { + ID string `json:"id"` +} + func (h *RoomHandler) ControlRequest(w http.ResponseWriter, r *http.Request) { session := auth.GetSession(r) if session.IsHost() { @@ -50,3 +54,40 @@ func (h *RoomHandler) ControlRelease(w http.ResponseWriter, r *http.Request) { utils.HttpSuccess(w) } + +func (h *RoomHandler) ControlTake(w http.ResponseWriter, r *http.Request) { + session := auth.GetSession(r) + + h.sessions.SetHost(session) + + h.sessions.Broadcast( + message.Control{ + Event: event.CONTROL_LOCKED, + ID: session.ID(), + }, nil) + + utils.HttpSuccess(w) +} + +func (h *RoomHandler) ControlGive(w http.ResponseWriter, r *http.Request) { + data := &ControlGivePayload{} + if !utils.HttpJsonRequest(w, r, data) { + return + } + + target, ok := h.sessions.Get(data.ID) + if !ok { + utils.HttpBadRequest(w, "Target user was not found.") + return + } + + h.sessions.SetHost(target) + + h.sessions.Broadcast( + message.Control{ + Event: event.CONTROL_LOCKED, + ID: target.ID(), + }, nil) + + utils.HttpSuccess(w) +} diff --git a/internal/api/room/handler.go b/internal/api/room/handler.go index 294ff1ac..0b2c8676 100644 --- a/internal/api/room/handler.go +++ b/internal/api/room/handler.go @@ -50,5 +50,8 @@ func (h *RoomHandler) Route(r chi.Router) { r.Route("/control", func(r chi.Router) { r.Post("/request", h.ControlRequest) r.Post("/release", h.ControlRelease) + + r.With(auth.AdminsOnly).Post("/take", h.ControlTake) + r.With(auth.AdminsOnly).Post("/give", h.ControlGive) }) }