From 18d19ca75e72fa598e42ac3b3c775a9f96e0117f Mon Sep 17 00:00:00 2001 From: Zlatin Balevsky Date: Mon, 11 Nov 2019 23:32:23 +0000 Subject: [PATCH] wip on joining and leaving rooms --- .../com/muwire/core/chat/ChatServer.groovy | 2 +- .../com/muwire/gui/ChatRoomController.groovy | 43 +++++++++++++++++-- .../com/muwire/gui/ChatRoomModel.groovy | 2 + .../views/com/muwire/gui/ChatRoomView.groovy | 4 +- .../com/muwire/gui/ChatServerView.groovy | 1 + 5 files changed, 47 insertions(+), 5 deletions(-) diff --git a/core/src/main/groovy/com/muwire/core/chat/ChatServer.groovy b/core/src/main/groovy/com/muwire/core/chat/ChatServer.groovy index 53e385cc..ec7eda77 100644 --- a/core/src/main/groovy/com/muwire/core/chat/ChatServer.groovy +++ b/core/src/main/groovy/com/muwire/core/chat/ChatServer.groovy @@ -195,7 +195,7 @@ class ChatServer { } private void processJoin(String room, ChatMessageEvent e) { - joinRoom(room, e.sender) + joinRoom(e.sender, room) rooms[room].each { if (it == e.sender) return diff --git a/gui/griffon-app/controllers/com/muwire/gui/ChatRoomController.groovy b/gui/griffon-app/controllers/com/muwire/gui/ChatRoomController.groovy index 936ee0f2..456fd1b4 100644 --- a/gui/griffon-app/controllers/com/muwire/gui/ChatRoomController.groovy +++ b/gui/griffon-app/controllers/com/muwire/gui/ChatRoomController.groovy @@ -29,6 +29,8 @@ class ChatRoomController { @MVCMember @Nonnull ChatRoomView view + boolean leftRoom + @ControllerAction void say() { String words = view.sayField.text @@ -45,23 +47,58 @@ class ChatRoomController { UUID uuid = UUID.randomUUID() String room = model.console ? ChatServer.CONSOLE : model.room - byte [] sig = ChatConnection.sign(uuid, now, room, command.source, model.core.me, mvcGroup.parentGroup.model.host, model.core.spk) + byte [] sig = ChatConnection.sign(uuid, now, room, command.source, model.core.me, model.host, model.core.spk) def event = new ChatMessageEvent(uuid : uuid, payload : command.source, sender : model.core.me, - host : mvcGroup.parentGroup.model.host, + host : model.host, room : room, chatTime : now, sig : sig) model.core.eventBus.publish(event) - if (command.payload.length() > 0) { + if (command.action == ChatAction.SAY && command.payload.length() > 0) { String toShow = DataHelper.formatTime(now) + " <" + model.core.me.getHumanReadableName() + "> "+command.payload view.roomTextArea.append(toShow) view.roomTextArea.append('\n') } + + if (command.action == ChatAction.JOIN) { + String newRoom = command.payload + if (mvcGroup.parentGroup.childrenGroups.containsKey(newRoom)) + return + def params = [:] + params['core'] = model.core + params['tabName'] = model.host.getHumanReadableName() + "-chat-rooms" + params['room'] = newRoom + params['console'] = false + params['host'] = model.host + + mvcGroup.parentGroup.createMVCGroup("chat-room", newRoom, params) + } + if (command.action == ChatAction.LEAVE && !model.console) { + leftRoom = true + view.closeTab.call() + } + } + + void leaveRoom() { + if (leftRoom) + return + leftRoom = true + long now = System.currentTimeMillis() + UUID uuid = UUID.randomUUID() + byte [] sig = ChatConnection.sign(uuid, now, model.room, "/LEAVE", model.core.me, model.host, model.core.spk) + def event = new ChatMessageEvent(uuid : uuid, + payload : "/LEAVE", + sender : model.core.me, + host : model.host, + room : model.room, + chatTime : now, + sig : sig) + model.core.eventBus.publish(event) } void handleChatMessage(ChatMessageEvent e) { diff --git a/gui/griffon-app/models/com/muwire/gui/ChatRoomModel.groovy b/gui/griffon-app/models/com/muwire/gui/ChatRoomModel.groovy index de2a325a..0c777bdf 100644 --- a/gui/griffon-app/models/com/muwire/gui/ChatRoomModel.groovy +++ b/gui/griffon-app/models/com/muwire/gui/ChatRoomModel.groovy @@ -1,6 +1,7 @@ package com.muwire.gui import com.muwire.core.Core +import com.muwire.core.Persona import griffon.core.artifact.GriffonModel import griffon.transform.Observable @@ -9,6 +10,7 @@ import griffon.metadata.ArtifactProviderFor @ArtifactProviderFor(GriffonModel) class ChatRoomModel { Core core + Persona host String tabName String room boolean console diff --git a/gui/griffon-app/views/com/muwire/gui/ChatRoomView.groovy b/gui/griffon-app/views/com/muwire/gui/ChatRoomView.groovy index 95cf8aad..2931a541 100644 --- a/gui/griffon-app/views/com/muwire/gui/ChatRoomView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/ChatRoomView.groovy @@ -44,7 +44,8 @@ class ChatRoomView { pane = builder.panel { borderLayout() panel(constraints : BorderLayout.CENTER) { - splitPane(orientation : JSplitPane.HORIZONTAL_SPLIT, continuousLayout : true, dividerLocation : 200) + gridLayout(rows : 1, cols : 1) + splitPane(orientation : JSplitPane.HORIZONTAL_SPLIT, continuousLayout : true, dividerLocation : 100) panel { table(autoCreateRowSorter : true, rowHeight : rowHeight) { tableModel(list : model.members) { @@ -90,6 +91,7 @@ class ChatRoomView { def closeTab = { int index = parent.indexOfComponent(pane) parent.removeTabAt(index) + controller.leaveRoom() mvcGroup.destroy() } } \ No newline at end of file diff --git a/gui/griffon-app/views/com/muwire/gui/ChatServerView.groovy b/gui/griffon-app/views/com/muwire/gui/ChatServerView.groovy index 0dd1c7c2..0e970f91 100644 --- a/gui/griffon-app/views/com/muwire/gui/ChatServerView.groovy +++ b/gui/griffon-app/views/com/muwire/gui/ChatServerView.groovy @@ -60,6 +60,7 @@ class ChatServerView { params['tabName'] = model.host.getHumanReadableName() + "-chat-rooms" params['room'] = 'Console' params['console'] = true + params['host'] = model.host mvcGroup.createMVCGroup("chat-room",ChatServer.CONSOLE, params) }