From ce77ccfdd6e746ab794580c5bc1aa7cd48d41a78 Mon Sep 17 00:00:00 2001 From: gounux Date: Wed, 24 Jul 2024 09:06:10 +0200 Subject: [PATCH] add internal message handling --- qtribu/constants.py | 1 + qtribu/gui/wdg_qchat.py | 30 ++++++++++++++++++++++++++++-- qtribu/gui/wdg_qchat.ui | 6 +++--- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/qtribu/constants.py b/qtribu/constants.py index 3ffd8f3b..bd008005 100644 --- a/qtribu/constants.py +++ b/qtribu/constants.py @@ -198,6 +198,7 @@ def local_path(self, base_path: Path = Path().home() / ".geotribu/cdn/") -> Path MENTION_MESSAGES_COLOR = "#4169e1" USER_MESSAGES_COLOR = "#00cc00" ERROR_MESSAGES_COLOR = "#ff0000" +INTERNAL_MESSAGE_AUTHOR = "internal" # QChat cheatcodes CHEATCODE_DIZZY = "givemesomecheese" diff --git a/qtribu/gui/wdg_qchat.py b/qtribu/gui/wdg_qchat.py index 413763f6..1cea77d6 100644 --- a/qtribu/gui/wdg_qchat.py +++ b/qtribu/gui/wdg_qchat.py @@ -3,7 +3,7 @@ from datetime import datetime from functools import partial from pathlib import Path -from typing import Optional +from typing import Any, Optional # PyQGIS # @@ -23,6 +23,7 @@ CHEATCODE_DIZZY, CHEATCODE_DONTCRYBABY, CHEATCODE_IAMAROBOT, + INTERNAL_MESSAGE_AUTHOR, MENTION_MESSAGES_COLOR, QCHAT_NICKNAME_MINLENGTH, USER_MESSAGES_COLOR, @@ -261,6 +262,7 @@ def disconnect_from_room(self, log: bool = True, close_ws: bool = True) -> None: ) self.btn_connect.setText(self.tr("Connect")) self.lbl_status.setText("Disconnected") + self.grb_qchat.setTitle(self.tr("QChat")) self.grb_user.setEnabled(False) self.connected = False if close_ws: @@ -273,6 +275,7 @@ def on_ws_disconnected(self) -> None: """ self.btn_connect.setText(self.tr("Connect")) self.lbl_status.setText("Disconnected") + self.grb_qchat.setTitle(self.tr("QChat")) self.grb_user.setEnabled(False) self.connected = False @@ -292,6 +295,11 @@ def on_ws_message_received(self, message: str) -> None: """ message = json.loads(message) + # check if this is an internal message + if message["author"] == INTERNAL_MESSAGE_AUTHOR: + self.handle_internal_message(message) + return + # check if a cheatcode is activated if self.settings.qchat_activate_cheatcode: activated = self.check_cheatcode(message) @@ -328,11 +336,29 @@ def on_ws_message_received(self, message: str) -> None: self.twg_chat.insertTopLevelItem(0, item) # check if a notification sound should be played - if self.settings.qchat_play_sounds: + if ( + self.settings.qchat_play_sounds + and message["author"] != self.settings.qchat_nickname + ): play_resource_sound( self.settings.qchat_ring_tone, self.settings.qchat_sound_volume ) + def handle_internal_message(self, message: dict[str, Any]) -> None: + """ + Handle an internal message, spotted by its author + """ + payload = json.loads(message["message"]) + if "nb_users" in payload: + nb_users = payload["nb_users"] + self.grb_qchat.setTitle( + self.tr("QChat - room: {room} - {nb_users} user{suffix}").format( + room=self.current_room, + nb_users=nb_users, + suffix="" if nb_users <= 1 else "s", + ) + ) + def on_clear_chat_button_clicked(self) -> None: """ Action called when the clear chat button is clicked diff --git a/qtribu/gui/wdg_qchat.ui b/qtribu/gui/wdg_qchat.ui index 772edd1b..2a818f71 100644 --- a/qtribu/gui/wdg_qchat.ui +++ b/qtribu/gui/wdg_qchat.ui @@ -149,7 +149,7 @@ - + 0 @@ -169,7 +169,7 @@ - Chat + QChat false @@ -313,7 +313,7 @@ grb_rooms - grb_chat + grb_qchat grb_user