neko/internal/websocket/filechooserdialog.go
2022-03-20 11:43:00 +01:00

68 lines
1.8 KiB
Go

package websocket
import (
"gitlab.com/demodesk/neko/server/pkg/types"
"gitlab.com/demodesk/neko/server/pkg/types/event"
"gitlab.com/demodesk/neko/server/pkg/types/message"
)
func (manager *WebSocketManagerCtx) fileChooserDialogEvents() {
var activeSession types.Session
// when dialog opens, everyone should be notified.
manager.desktop.OnFileChooserDialogOpened(func() {
manager.logger.Info().Msg("file chooser dialog opened")
host := manager.sessions.GetHost()
if host == nil {
manager.logger.Warn().Msg("no host for file chooser dialog found, closing")
go manager.desktop.CloseFileChooserDialog()
return
}
activeSession = host
go manager.sessions.Broadcast(
event.FILE_CHOOSER_DIALOG_OPENED,
message.SessionID{
ID: host.ID(),
}, nil)
})
// when dialog closes, everyone should be notified.
manager.desktop.OnFileChooserDialogClosed(func() {
manager.logger.Info().Msg("file chooser dialog closed")
activeSession = nil
go manager.sessions.Broadcast(
event.FILE_CHOOSER_DIALOG_CLOSED,
message.SessionID{}, nil)
})
// when new user joins, and someone holds dialog, he shouldd be notified about it.
manager.sessions.OnConnected(func(session types.Session) {
if activeSession == nil {
return
}
manager.logger.Debug().Str("session_id", session.ID()).Msg("sending file chooser dialog status to a new session")
session.Send(
event.FILE_CHOOSER_DIALOG_OPENED,
message.SessionID{
ID: activeSession.ID(),
})
})
// when user, that holds dialog, disconnects, it should be closed.
manager.sessions.OnDisconnected(func(session types.Session) {
if activeSession == nil || activeSession != session {
return
}
manager.logger.Info().Msg("file chooser dialog owner left, closing")
manager.desktop.CloseFileChooserDialog()
})
}