From 64dd31844b339fe3982297bdb85d374a95833975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Wed, 20 Jan 2021 23:48:50 +0100 Subject: [PATCH] filechooser websocket handler. --- internal/types/event/events.go | 5 ++ internal/websocket/filechooserdialog.go | 72 +++++++++++++++++++++++++ internal/websocket/manager.go | 12 +---- 3 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 internal/websocket/filechooserdialog.go diff --git a/internal/types/event/events.go b/internal/types/event/events.go index 09593304..36d48ede 100644 --- a/internal/types/event/events.go +++ b/internal/types/event/events.go @@ -51,3 +51,8 @@ const ( const ( BORADCAST_STATUS = "broadcast/status" ) + +const ( + FILE_CHOOSER_DIALOG_OPENED = "file_chooser_dialog/opened" + FILE_CHOOSER_DIALOG_CLOSED = "file_chooser_dialog/closed" +) diff --git a/internal/websocket/filechooserdialog.go b/internal/websocket/filechooserdialog.go new file mode 100644 index 00000000..9ad4def2 --- /dev/null +++ b/internal/websocket/filechooserdialog.go @@ -0,0 +1,72 @@ +package websocket + +import ( + "demodesk/neko/internal/types" + "demodesk/neko/internal/types/event" + "demodesk/neko/internal/types/message" +) + +func (ws *WebSocketManagerCtx) fileChooserDialogEvents() { + var file_chooser_dialog_member types.Session + + // when dialog opens, everyone should be notified. + ws.desktop.OnFileChooserDialogOpened(func() { + ws.logger.Info().Msg("FileChooserDialog opened") + + host := ws.sessions.GetHost() + if host == nil { + ws.logger.Warn().Msg("no host for FileChooserDialog found, closing") + go ws.desktop.CloseFileChooserDialog() + return + } + + file_chooser_dialog_member = host + + go ws.sessions.Broadcast(message.MemberID{ + Event: event.FILE_CHOOSER_DIALOG_OPENED, + ID: host.ID(), + }, nil) + }) + + // when dialog closes, everyone should be notified. + ws.desktop.OnFileChooserDialogClosed(func() { + ws.logger.Info().Msg("FileChooserDialog closed") + + file_chooser_dialog_member = nil + + go ws.sessions.Broadcast(message.MemberID{ + Event: event.FILE_CHOOSER_DIALOG_CLOSED, + }, nil) + }) + + + // when new user joins, and someone holds dialog, he shouldd be notified about it. + ws.sessions.OnConnected(func(session types.Session) { + if file_chooser_dialog_member == nil { + return + } + + if err := session.Send(message.MemberID{ + Event: event.FILE_CHOOSER_DIALOG_OPENED, + ID: file_chooser_dialog_member.ID(), + }); err != nil { + ws.logger.Warn(). + Str("id", session.ID()). + Err(err). + Msgf("could not send event `%s` to session", event.FILE_CHOOSER_DIALOG_OPENED) + } + }) + + // when user, that holds dialog, disconnects, it should be closed. + ws.sessions.OnDisconnected(func(session types.Session) { + if file_chooser_dialog_member == nil { + return + } + + if session.ID() != file_chooser_dialog_member.ID() { + return + } + + ws.desktop.CloseFileChooserDialog() + }) +} diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index 12feb094..511c9450 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -134,17 +134,7 @@ func (ws *WebSocketManagerCtx) Start() { } }) - ws.desktop.OnFileChooserDialogOpened(func() { - // TODO: Implement. - ws.logger.Info(). - Msg("FileChooserDialog opened") - }) - - ws.desktop.OnFileChooserDialogClosed(func() { - // TODO: Implement. - ws.logger.Info(). - Msg("FileChooserDialog closed") - }) + ws.fileChooserDialogEvents() } func (ws *WebSocketManagerCtx) Shutdown() error {