neko/internal/api/room/control.go

150 lines
2.9 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"
2020-11-17 10:48:20 +13:00
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
"demodesk/neko/internal/utils"
"demodesk/neko/internal/http/auth"
)
2020-11-19 11:14:28 +13:00
type ControlStatusPayload struct {
HasHost bool `json:"has_host"`
HostId string `json:"host_id,omitempty"`
}
2020-11-19 11:05:38 +13:00
type ControlTargetPayload struct {
ID string `json:"id"`
}
2020-11-19 11:14:28 +13:00
func (h *RoomHandler) controlStatus(w http.ResponseWriter, r *http.Request) {
host := h.sessions.GetHost()
if host == nil {
utils.HttpSuccess(w, ControlStatusPayload{
HasHost: false,
})
} else {
utils.HttpSuccess(w, ControlStatusPayload{
HasHost: true,
HostId: host.ID(),
})
}
}
2020-11-19 10:35:50 +13:00
func (h *RoomHandler) controlRequest(w http.ResponseWriter, r *http.Request) {
2020-11-17 10:48:20 +13:00
host := h.sessions.GetHost()
if host != nil {
2020-11-19 11:05:38 +13:00
utils.HttpUnprocessableEntity(w, "There is already a host.")
2020-11-17 10:48:20 +13:00
return
}
2020-11-19 11:05:38 +13:00
session := auth.GetSession(r)
2020-12-07 06:50:41 +13:00
if !session.CanHost() {
utils.HttpBadRequest(w, "Member is not allowed to host.")
return
}
2020-11-17 10:48:20 +13:00
h.sessions.SetHost(session)
2020-11-19 08:30:33 +13:00
h.sessions.Broadcast(
2020-12-01 06:24:38 +13:00
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: true,
HostID: session.ID(),
2020-11-19 08:30:33 +13:00
}, nil)
2020-11-17 10:48:20 +13:00
utils.HttpSuccess(w)
}
2020-11-19 10:35:50 +13:00
func (h *RoomHandler) controlRelease(w http.ResponseWriter, r *http.Request) {
2020-11-17 10:48:20 +13:00
session := auth.GetSession(r)
if !session.IsHost() {
2020-12-07 06:50:41 +13:00
utils.HttpUnprocessableEntity(w, "Member is not the host.")
return
}
if !session.CanHost() {
utils.HttpBadRequest(w, "Member is not allowed to host.")
2020-11-17 10:48:20 +13:00
return
}
2020-12-02 22:46:00 +13:00
h.desktop.ResetKeys()
2020-11-17 10:48:20 +13:00
h.sessions.ClearHost()
2020-11-19 08:30:33 +13:00
h.sessions.Broadcast(
2020-12-01 06:24:38 +13:00
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: false,
2020-11-19 08:30:33 +13:00
}, nil)
2020-11-17 10:48:20 +13:00
utils.HttpSuccess(w)
}
2020-11-19 10:35:50 +13:00
func (h *RoomHandler) controlTake(w http.ResponseWriter, r *http.Request) {
session := auth.GetSession(r)
2020-12-07 06:50:41 +13:00
if !session.CanHost() {
utils.HttpBadRequest(w, "Member is not allowed to host.")
return
}
h.sessions.SetHost(session)
h.sessions.Broadcast(
2020-12-01 06:24:38 +13:00
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: true,
HostID: session.ID(),
}, nil)
utils.HttpSuccess(w)
}
2020-11-19 10:35:50 +13:00
func (h *RoomHandler) controlGive(w http.ResponseWriter, r *http.Request) {
2020-12-27 11:53:04 +13:00
memberId := chi.URLParam(r, "memberId")
2020-12-27 11:53:04 +13:00
target, ok := h.sessions.Get(memberId)
if !ok {
utils.HttpNotFound(w, "Target member was not found.")
2020-12-07 06:50:41 +13:00
return
}
if !target.CanHost() {
utils.HttpBadRequest(w, "Target member is not allowed to host.")
return
}
h.sessions.SetHost(target)
h.sessions.Broadcast(
2020-12-01 06:24:38 +13:00
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: true,
HostID: target.ID(),
}, nil)
utils.HttpSuccess(w)
}
2020-11-30 03:58:26 +13:00
func (h *RoomHandler) controlReset(w http.ResponseWriter, r *http.Request) {
host := h.sessions.GetHost()
if host == nil {
utils.HttpSuccess(w)
return
}
2020-12-02 22:46:00 +13:00
h.desktop.ResetKeys()
2020-11-30 03:58:26 +13:00
h.sessions.ClearHost()
2020-12-02 22:46:00 +13:00
2020-11-30 03:58:26 +13:00
h.sessions.Broadcast(
2020-12-01 06:24:38 +13:00
message.ControlHost{
Event: event.CONTROL_HOST,
HasHost: false,
2020-11-30 03:58:26 +13:00
}, nil)
utils.HttpSuccess(w)
}