mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
REST API control.
This commit is contained in:
parent
eec285cd07
commit
3f4914d260
@ -1 +1,56 @@
|
||||
package room
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"demodesk/neko/internal/types/event"
|
||||
"demodesk/neko/internal/types/message"
|
||||
"demodesk/neko/internal/utils"
|
||||
"demodesk/neko/internal/http/auth"
|
||||
)
|
||||
|
||||
func (h *RoomHandler) ControlRequest(w http.ResponseWriter, r *http.Request) {
|
||||
session := auth.GetSession(r)
|
||||
if session.IsHost() {
|
||||
utils.HttpBadRequest(w, "User is already host.")
|
||||
return
|
||||
}
|
||||
|
||||
host := h.sessions.GetHost()
|
||||
if host != nil {
|
||||
utils.HttpBadRequest(w, "There is already a host.")
|
||||
return
|
||||
}
|
||||
|
||||
h.sessions.SetHost(session)
|
||||
|
||||
if err := h.sessions.Broadcast(
|
||||
message.Control{
|
||||
Event: event.CONTROL_LOCKED,
|
||||
ID: session.ID(),
|
||||
}, nil); err != nil {
|
||||
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED)
|
||||
}
|
||||
|
||||
utils.HttpSuccess(w)
|
||||
}
|
||||
|
||||
func (h *RoomHandler) ControlRelease(w http.ResponseWriter, r *http.Request) {
|
||||
session := auth.GetSession(r)
|
||||
if !session.IsHost() {
|
||||
utils.HttpBadRequest(w, "User is not the host.")
|
||||
return
|
||||
}
|
||||
|
||||
h.sessions.ClearHost()
|
||||
|
||||
if err := h.sessions.Broadcast(
|
||||
message.Control{
|
||||
Event: event.CONTROL_RELEASE,
|
||||
ID: session.ID(),
|
||||
}, nil); err != nil {
|
||||
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
|
||||
}
|
||||
|
||||
utils.HttpSuccess(w)
|
||||
}
|
||||
|
@ -46,4 +46,9 @@ func (h *RoomHandler) Route(r chi.Router) {
|
||||
r.Get("/", h.ClipboardRead)
|
||||
r.Post("/", h.ClipboardWrite)
|
||||
})
|
||||
|
||||
r.Route("/control", func(r chi.Router) {
|
||||
r.Post("/request", h.ControlRequest)
|
||||
r.Post("/release", h.ControlRelease)
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user