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

Commit

Permalink
add session to settings change.
Browse files Browse the repository at this point in the history
  • Loading branch information
m1k1o committed May 4, 2024
1 parent d47302e commit 57ffca0
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 30 deletions.
2 changes: 1 addition & 1 deletion internal/api/room/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func New(
}

// generate fallback image for private mode when needed
sessions.OnSettingsChanged(func(new types.Settings, old types.Settings) {
sessions.OnSettingsChanged(func(session types.Session, new types.Settings, old types.Settings) {
if old.PrivateMode && !new.PrivateMode {
log.Debug().Msg("clearing private mode fallback image")
h.privateModeImage = nil
Expand Down
22 changes: 18 additions & 4 deletions internal/api/room/settings.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package room

import (
"encoding/json"
"io"
"net/http"

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

Expand All @@ -12,13 +16,23 @@ func (h *RoomHandler) settingsGet(w http.ResponseWriter, r *http.Request) error
}

func (h *RoomHandler) settingsSet(w http.ResponseWriter, r *http.Request) error {
settings := h.sessions.Settings()
session, _ := auth.GetSession(r)

if err := utils.HttpJsonRequest(w, r, &settings); err != nil {
return err
// We read the request body first and unmashal it inside the UpdateSettingsFunc
// to ensure atomicity of the operation.
body, err := io.ReadAll(r.Body)
if err != nil {
return utils.HttpBadRequest("unable to read request body").WithInternalErr(err)
}

h.sessions.UpdateSettings(settings)
h.sessions.UpdateSettingsFunc(session, func(settings *types.Settings) bool {
err = json.Unmarshal(body, settings)
return err == nil
})

if err != nil {
return utils.HttpBadRequest("unable to parse provided data").WithInternalErr(err)
}

return utils.HttpSuccess(w)
}
21 changes: 6 additions & 15 deletions internal/session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,39 +367,30 @@ func (manager *SessionManagerCtx) OnHostChanged(listener func(session types.Sess
})
}

func (manager *SessionManagerCtx) OnSettingsChanged(listener func(new types.Settings, old types.Settings)) {
func (manager *SessionManagerCtx) OnSettingsChanged(listener func(session types.Session, new types.Settings, old types.Settings)) {
manager.emmiter.On("settings_changed", func(payload ...any) {
listener(payload[0].(types.Settings), payload[1].(types.Settings))
listener(payload[0].(types.Session), payload[1].(types.Settings), payload[2].(types.Settings))
})
}

// ---
// settings
// ---

func (manager *SessionManagerCtx) UpdateSettings(new types.Settings) {
manager.settingsMu.Lock()
old := manager.settings
manager.settings = new
manager.settingsMu.Unlock()

manager.updateSettings(new, old)
}

func (manager *SessionManagerCtx) UpdateSettingsFunc(f func(settings *types.Settings) bool) {
func (manager *SessionManagerCtx) UpdateSettingsFunc(session types.Session, f func(settings *types.Settings) bool) {
manager.settingsMu.Lock()
new := manager.settings
if f(&new) {
old := manager.settings
manager.settings = new
manager.settingsMu.Unlock()
manager.updateSettings(new, old)
manager.updateSettings(session, new, old)
return
}
manager.settingsMu.Unlock()
}

func (manager *SessionManagerCtx) updateSettings(new, old types.Settings) {
func (manager *SessionManagerCtx) updateSettings(session types.Session, new, old types.Settings) {
// if private mode changed
if old.PrivateMode != new.PrivateMode {
// update webrtc paused state for all sessions
Expand Down Expand Up @@ -447,7 +438,7 @@ func (manager *SessionManagerCtx) updateSettings(new, old types.Settings) {
}
}

manager.emmiter.Emit("settings_changed", new, old)
manager.emmiter.Emit("settings_changed", session, new, old)
}

func (manager *SessionManagerCtx) Settings() types.Settings {
Expand Down
4 changes: 2 additions & 2 deletions internal/websocket/handler/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
}

// update settings in atomic way
h.sessions.UpdateSettingsFunc(func(settings *types.Settings) bool {
h.sessions.UpdateSettingsFunc(session, func(settings *types.Settings) bool {
// if control protection & locked controls: unlock controls
if settings.LockedControls && settings.ControlProtection {
settings.LockedControls = false
Expand Down Expand Up @@ -70,7 +70,7 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
})

// update settings in atomic way
h.sessions.UpdateSettingsFunc(func(settings *types.Settings) bool {
h.sessions.UpdateSettingsFunc(session, func(settings *types.Settings) bool {
// if control protection & not locked controls & no admin: lock controls
if !settings.LockedControls && settings.ControlProtection && !hasAdmin {
settings.LockedControls = true
Expand Down
9 changes: 7 additions & 2 deletions internal/websocket/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (manager *WebSocketManagerCtx) Start() {
Msg("session host changed")
})

manager.sessions.OnSettingsChanged(func(new types.Settings, old types.Settings) {
manager.sessions.OnSettingsChanged(func(session types.Session, new types.Settings, old types.Settings) {
// start inactive cursors
if new.InactiveCursors && !old.InactiveCursors {
manager.startInactiveCursors()
Expand All @@ -138,8 +138,13 @@ func (manager *WebSocketManagerCtx) Start() {
manager.stopInactiveCursors()
}

manager.sessions.Broadcast(event.SYSTEM_SETTINGS, new)
manager.sessions.Broadcast(event.SYSTEM_SETTINGS, message.SystemSettingsUpdate{
ID: session.ID(),
Settings: new,
})

manager.logger.Info().
Str("session_id", session.ID()).
Interface("new", new).
Interface("old", old).
Msg("settings changed")
Expand Down
8 changes: 5 additions & 3 deletions pkg/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,11 @@ func TestCanHostOnly(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
settings := sessionManager.Settings()
settings.PrivateMode = tt.privateMode
sessionManager.UpdateSettings(settings)
session, _ := GetSession(tt.r)
sessionManager.UpdateSettingsFunc(session, func(s *types.Settings) bool {
s.PrivateMode = tt.privateMode
return true
})

_, err := CanHostOnly(nil, tt.r)
if (err != nil) != tt.wantErr {
Expand Down
5 changes: 5 additions & 0 deletions pkg/types/message/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ type SystemDisconnect struct {
Message string `json:"message"`
}

type SystemSettingsUpdate struct {
ID string `json:"id"`
types.Settings
}

/////////////////////////////
// Signal
/////////////////////////////
Expand Down
5 changes: 2 additions & 3 deletions pkg/types/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,9 @@ type SessionManager interface {
OnProfileChanged(listener func(session Session))
OnStateChanged(listener func(session Session))
OnHostChanged(listener func(session Session))
OnSettingsChanged(listener func(new Settings, old Settings))
OnSettingsChanged(listener func(session Session, new Settings, old Settings))

UpdateSettings(Settings)
UpdateSettingsFunc(f func(settings *Settings) bool)
UpdateSettingsFunc(session Session, f func(settings *Settings) bool)
Settings() Settings
CookieEnabled() bool

Expand Down

0 comments on commit 57ffca0

Please sign in to comment.