Skip to content

Commit

Permalink
feature(qchat): handle users registration
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Oct 8, 2024
1 parent 2052276 commit c97b041
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
41 changes: 41 additions & 0 deletions qtribu/gui/dck_qchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ def __init__(self, iface: QgisInterface, parent: QWidget = None):
self.on_custom_context_menu_requested
)

# list users signal listener
self.bltn_list_users.pressed.connect(self.on_list_users_button_clicked)
self.bltn_list_users.setIcon(
QIcon(QgsApplication.iconPath("processingResult.svg"))
)

# clear chat signal listener
self.btn_clear_chat.pressed.connect(self.on_clear_chat_button_clicked)
self.btn_clear_chat.setIcon(
Expand Down Expand Up @@ -288,6 +294,7 @@ def on_ws_connected(self, room: str) -> None:
self.btn_connect.setText(self.tr("Disconnect"))
self.lbl_status.setText("Connected")
self.grb_room.setTitle(self.tr("Room: {room}").format(room=room))
self.grb_qchat.setEnabled(True)
self.grb_user.setEnabled(True)
self.current_room = room
self.connected = True
Expand All @@ -296,6 +303,12 @@ def on_ws_connected(self, room: str) -> None:
self.add_admin_message(
self.tr("Connected to room '{room}'").format(room=room)
)
# send newcomer message to websocket
message = {
"author": INTERNAL_MESSAGE_AUTHOR,
"newcomer": self.settings.author_nickname,
}
self.ws_client.sendTextMessage(json.dumps(message))

def disconnect_from_room(self, log: bool = True, close_ws: bool = True) -> None:
"""
Expand All @@ -311,6 +324,7 @@ def disconnect_from_room(self, log: bool = True, close_ws: bool = True) -> None:
self.lbl_status.setText("Disconnected")
self.grb_room.setTitle(self.tr("Room"))
self.grb_qchat.setTitle(self.tr("QChat"))
self.grb_qchat.setEnabled(False)
self.grb_user.setEnabled(False)
self.connected = False
if close_ws:
Expand Down Expand Up @@ -419,6 +433,14 @@ def handle_internal_message(self, message: dict[str, Any]) -> None:
)
)
self.log(message=f"Internal message received: {nb_users} users in room")
if (
"newcomer" in message
and message["newcomer"] != self.settings.author_nickname
):
newcomer = message["newcomer"]
self.add_admin_message(
self.tr("{newcomer} has joined the room").format(newcomer=newcomer)
)

def on_message_double_clicked(self, item: QTreeWidgetItem, column: int) -> None:
"""
Expand Down Expand Up @@ -486,6 +508,25 @@ def on_hide_message(self, item: QTreeWidgetItem) -> None:
root = self.twg_chat.invisibleRootItem()
(item.parent() or root).removeChild(item)

def on_list_users_button_clicked(self) -> None:
"""
Action called when the list users button is clicked
"""
try:
users = self.qchat_client.get_registered_users(self.current_room)
QMessageBox.information(
self,
self.tr("Registered users"),
self.tr(
"""Registered users in room ({room}):
{users}"""
).format(room=self.current_room, users=",".join(users)),
)
except Exception as exc:
self.iface.messageBar().pushCritical(self.tr("QChat error"), str(exc))
self.log(message=str(exc), log_level=Qgis.Critical)

def on_clear_chat_button_clicked(self) -> None:
"""
Action called when the clear chat button is clicked
Expand Down
33 changes: 25 additions & 8 deletions qtribu/gui/dck_qchat.ui
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@
</item>
<item>
<widget class="QgsCollapsibleGroupBox" name="grb_qchat">
<property name="enabled">
<bool>false</bool>
</property>
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
Expand Down Expand Up @@ -208,14 +211,28 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_clear_chat">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>Clear</string>
</property>
</widget>
<layout class="QHBoxLayout" name="hly_qchat_buttons">
<item>
<widget class="QPushButton" name="bltn_list_users">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>List users</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btn_clear_chat">
<property name="cursor">
<cursorShape>PointingHandCursor</cursorShape>
</property>
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
Expand Down
14 changes: 14 additions & 0 deletions qtribu/logic/qchat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,17 @@ def get_rooms(self) -> list[str]:
)
data = json.loads(str(response, "UTF8"))
return data

def get_registered_users(self, room: str) -> list[str]:
"""
Get registered users in a room with an API HTTP CALL
"""
url = f"{self.instance_uri}/room/{room}/users"
response: QByteArray = self.qntwk.get_from_source(
headers=HEADERS,
url=url,
response_expected_content_type=CONTENT_TYPE_JSON,
use_cache=False,
)
data = json.loads(str(response, "UTF8"))
return data

0 comments on commit c97b041

Please sign in to comment.