add control give & control take endpoints.

This commit is contained in:
Miroslav Šedivý 2020-11-18 21:10:40 +01:00
parent 6d27b0a69c
commit 6fbb1a2cc7
2 changed files with 44 additions and 0 deletions

View File

@ -9,6 +9,10 @@ import (
"demodesk/neko/internal/http/auth" "demodesk/neko/internal/http/auth"
) )
type ControlGivePayload struct {
ID string `json:"id"`
}
func (h *RoomHandler) ControlRequest(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) ControlRequest(w http.ResponseWriter, r *http.Request) {
session := auth.GetSession(r) session := auth.GetSession(r)
if session.IsHost() { if session.IsHost() {
@ -50,3 +54,40 @@ func (h *RoomHandler) ControlRelease(w http.ResponseWriter, r *http.Request) {
utils.HttpSuccess(w) 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)
}

View File

@ -50,5 +50,8 @@ func (h *RoomHandler) Route(r chi.Router) {
r.Route("/control", func(r chi.Router) { r.Route("/control", func(r chi.Router) {
r.Post("/request", h.ControlRequest) r.Post("/request", h.ControlRequest)
r.Post("/release", h.ControlRelease) r.Post("/release", h.ControlRelease)
r.With(auth.AdminsOnly).Post("/take", h.ControlTake)
r.With(auth.AdminsOnly).Post("/give", h.ControlGive)
}) })
} }