neko/server/internal/api/room/control.go

110 lines
2.4 KiB
Go
Raw Normal View History

2020-10-30 10:23:30 +13:00
package room
2020-11-17 10:48:20 +13:00
import (
"net/http"
2020-12-27 11:53:04 +13:00
"github.com/go-chi/chi"
"github.com/demodesk/neko/pkg/auth"
2024-05-10 22:41:51 +12:00
"github.com/demodesk/neko/pkg/types/event"
"github.com/demodesk/neko/pkg/types/message"
"github.com/demodesk/neko/pkg/utils"
2020-11-17 10:48:20 +13:00
)
2020-11-19 11:14:28 +13:00
type ControlStatusPayload struct {
2021-02-15 02:40:17 +13:00
HasHost bool `json:"has_host"`
HostId string `json:"host_id,omitempty"`
2020-11-19 11:14:28 +13:00
}
2020-11-19 11:05:38 +13:00
type ControlTargetPayload struct {
ID string `json:"id"`
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) controlStatus(w http.ResponseWriter, r *http.Request) error {
host, hasHost := h.sessions.GetHost()
2020-11-19 11:14:28 +13:00
var hostId string
if hasHost {
hostId = host.ID()
2020-11-19 11:14:28 +13:00
}
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w, ControlStatusPayload{
HasHost: hasHost,
HostId: hostId,
2021-09-17 10:58:50 +12:00
})
2020-11-19 11:14:28 +13:00
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) controlRequest(w http.ResponseWriter, r *http.Request) error {
2024-05-10 22:41:51 +12:00
session, _ := auth.GetSession(r)
host, hasHost := h.sessions.GetHost()
if hasHost {
2024-05-10 22:41:51 +12:00
// TODO: Some throttling mechanism to prevent spamming.
// let host know that someone wants to take control
host.Send(
event.CONTROL_REQUEST,
message.SessionID{
ID: session.ID(),
})
return utils.HttpError(http.StatusAccepted, "control request sent")
2020-11-17 10:48:20 +13:00
}
2023-05-15 08:41:32 +12:00
if h.sessions.Settings().LockedControls && !session.Profile().IsAdmin {
return utils.HttpForbidden("controls are locked")
}
2024-05-07 09:47:13 +12:00
session.SetAsHost()
2020-11-17 10:48:20 +13:00
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w)
2020-11-17 10:48:20 +13:00
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) controlRelease(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)
2020-11-17 10:48:20 +13:00
if !session.IsHost() {
2021-09-17 10:58:50 +12:00
return utils.HttpUnprocessableEntity("session is not the host")
2020-12-07 06:50:41 +13:00
}
2020-12-02 22:46:00 +13:00
h.desktop.ResetKeys()
2024-05-07 09:47:13 +12:00
session.ClearHost()
2021-02-03 06:28:32 +13:00
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w)
2020-11-17 10:48:20 +13:00
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) controlTake(w http.ResponseWriter, r *http.Request) error {
session, _ := auth.GetSession(r)
2024-05-07 09:47:13 +12:00
session.SetAsHost()
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w)
}
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) controlGive(w http.ResponseWriter, r *http.Request) error {
2024-05-07 09:47:13 +12:00
session, _ := auth.GetSession(r)
2021-03-14 11:32:10 +13:00
sessionId := chi.URLParam(r, "sessionId")
2021-03-14 11:32:10 +13:00
target, ok := h.sessions.Get(sessionId)
if !ok {
2021-09-17 10:58:50 +12:00
return utils.HttpNotFound("target session was not found")
2020-12-07 06:50:41 +13:00
}
2021-03-14 12:45:51 +13:00
if !target.Profile().CanHost {
2021-09-17 10:58:50 +12:00
return utils.HttpBadRequest("target session is not allowed to host")
}
2024-05-07 09:47:13 +12:00
target.SetAsHostBy(session)
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w)
}
2020-11-30 03:58:26 +13:00
2021-09-17 10:58:50 +12:00
func (h *RoomHandler) controlReset(w http.ResponseWriter, r *http.Request) error {
2024-05-07 09:47:13 +12:00
session, _ := auth.GetSession(r)
_, hasHost := h.sessions.GetHost()
2020-11-30 03:58:26 +13:00
if hasHost {
2021-09-17 10:58:50 +12:00
h.desktop.ResetKeys()
2024-05-07 09:47:13 +12:00
session.ClearHost()
2021-09-17 10:58:50 +12:00
}
2020-12-02 22:46:00 +13:00
2021-09-17 10:58:50 +12:00
return utils.HttpSuccess(w)
2020-11-30 03:58:26 +13:00
}