add session to settings change.

This commit is contained in:
Miroslav Šedivý 2024-05-05 00:03:32 +02:00
parent d47302e2cd
commit 57ffca0bed
8 changed files with 46 additions and 30 deletions

View File

@ -31,7 +31,7 @@ func New(
} }
// generate fallback image for private mode when needed // 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 { if old.PrivateMode && !new.PrivateMode {
log.Debug().Msg("clearing private mode fallback image") log.Debug().Msg("clearing private mode fallback image")
h.privateModeImage = nil h.privateModeImage = nil

View File

@ -1,8 +1,12 @@
package room package room
import ( import (
"encoding/json"
"io"
"net/http" "net/http"
"github.com/demodesk/neko/pkg/auth"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/utils" "github.com/demodesk/neko/pkg/utils"
) )
@ -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 { 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 { // We read the request body first and unmashal it inside the UpdateSettingsFunc
return err // 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) return utils.HttpSuccess(w)
} }

View File

@ -367,9 +367,9 @@ 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) { 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))
}) })
} }
@ -377,29 +377,20 @@ func (manager *SessionManagerCtx) OnSettingsChanged(listener func(new types.Sett
// settings // settings
// --- // ---
func (manager *SessionManagerCtx) UpdateSettings(new types.Settings) { func (manager *SessionManagerCtx) UpdateSettingsFunc(session types.Session, f func(settings *types.Settings) bool) {
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) {
manager.settingsMu.Lock() manager.settingsMu.Lock()
new := manager.settings new := manager.settings
if f(&new) { if f(&new) {
old := manager.settings old := manager.settings
manager.settings = new manager.settings = new
manager.settingsMu.Unlock() manager.settingsMu.Unlock()
manager.updateSettings(new, old) manager.updateSettings(session, new, old)
return return
} }
manager.settingsMu.Unlock() 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 private mode changed
if old.PrivateMode != new.PrivateMode { if old.PrivateMode != new.PrivateMode {
// update webrtc paused state for all sessions // update webrtc paused state for all sessions
@ -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 { func (manager *SessionManagerCtx) Settings() types.Settings {

View File

@ -39,7 +39,7 @@ func (h *MessageHandlerCtx) SessionConnected(session types.Session) error {
} }
// update settings in atomic way // 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 control protection & locked controls: unlock controls
if settings.LockedControls && settings.ControlProtection { if settings.LockedControls && settings.ControlProtection {
settings.LockedControls = false settings.LockedControls = false
@ -70,7 +70,7 @@ func (h *MessageHandlerCtx) SessionDisconnected(session types.Session) error {
}) })
// update settings in atomic way // 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 control protection & not locked controls & no admin: lock controls
if !settings.LockedControls && settings.ControlProtection && !hasAdmin { if !settings.LockedControls && settings.ControlProtection && !hasAdmin {
settings.LockedControls = true settings.LockedControls = true

View File

@ -127,7 +127,7 @@ func (manager *WebSocketManagerCtx) Start() {
Msg("session host changed") 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 // start inactive cursors
if new.InactiveCursors && !old.InactiveCursors { if new.InactiveCursors && !old.InactiveCursors {
manager.startInactiveCursors() manager.startInactiveCursors()
@ -138,8 +138,13 @@ func (manager *WebSocketManagerCtx) Start() {
manager.stopInactiveCursors() 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(). manager.logger.Info().
Str("session_id", session.ID()).
Interface("new", new). Interface("new", new).
Interface("old", old). Interface("old", old).
Msg("settings changed") Msg("settings changed")

View File

@ -224,9 +224,11 @@ func TestCanHostOnly(t *testing.T) {
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
settings := sessionManager.Settings() session, _ := GetSession(tt.r)
settings.PrivateMode = tt.privateMode sessionManager.UpdateSettingsFunc(session, func(s *types.Settings) bool {
sessionManager.UpdateSettings(settings) s.PrivateMode = tt.privateMode
return true
})
_, err := CanHostOnly(nil, tt.r) _, err := CanHostOnly(nil, tt.r)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {

View File

@ -42,6 +42,11 @@ type SystemDisconnect struct {
Message string `json:"message"` Message string `json:"message"`
} }
type SystemSettingsUpdate struct {
ID string `json:"id"`
types.Settings
}
///////////////////////////// /////////////////////////////
// Signal // Signal
///////////////////////////// /////////////////////////////

View File

@ -101,10 +101,9 @@ type SessionManager interface {
OnProfileChanged(listener func(session Session)) OnProfileChanged(listener func(session Session))
OnStateChanged(listener func(session Session)) OnStateChanged(listener func(session Session))
OnHostChanged(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(session Session, f func(settings *Settings) bool)
UpdateSettingsFunc(f func(settings *Settings) bool)
Settings() Settings Settings() Settings
CookieEnabled() bool CookieEnabled() bool