Skip to content
This repository has been archived by the owner on Jul 18, 2024. It is now read-only.

Commit

Permalink
add control requesting to the host.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o committed May 10, 2024
1 parent 0e8108e commit 59b2fae
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
17 changes: 14 additions & 3 deletions internal/api/room/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"github.com/go-chi/chi"

"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types/event"
"github.com/demodesk/neko/pkg/types/message"
"github.com/demodesk/neko/pkg/utils"
)

Expand Down Expand Up @@ -33,12 +35,21 @@ func (h *RoomHandler) controlStatus(w http.ResponseWriter, r *http.Request) erro
}

func (h *RoomHandler) controlRequest(w http.ResponseWriter, r *http.Request) error {
_, hasHost := h.sessions.GetHost()
session, _ := auth.GetSession(r)
host, hasHost := h.sessions.GetHost()
if hasHost {
return utils.HttpUnprocessableEntity("there is already a host")
// 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")
}

session, _ := auth.GetSession(r)
if h.sessions.Settings().LockedControls && !session.Profile().IsAdmin {
return utils.HttpForbidden("controls are locked")
}
Expand Down
34 changes: 20 additions & 14 deletions internal/websocket/handler/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,29 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
return ErrIsNotAllowedToHost
}

if !h.sessions.Settings().ImplicitHosting {
// tell session if there is a host
if host, hasHost := h.sessions.GetHost(); hasHost {
session.Send(
event.CONTROL_HOST,
message.ControlHost{
HasHost: true,
HostID: host.ID(),
})

return ErrIsAlreadyHosted
}
// if implicit hosting is enabled, set session as host without asking
if h.sessions.Settings().ImplicitHosting {
session.SetAsHost()
return nil
}

session.SetAsHost()
// if there is no host, set session as host
host, hasHost := h.sessions.GetHost()
if !hasHost {
session.SetAsHost()
return nil
}

return nil
// 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 ErrIsAlreadyHosted
}

func (h *MessageHandlerCtx) controlMove(session types.Session, payload *message.ControlPos) error {
Expand Down

0 comments on commit 59b2fae

Please sign in to comment.