From db8ce3e4c589120aa0ab5ac47b3825e3b8001e90 Mon Sep 17 00:00:00 2001 From: Guilhem Allaman <40383801+gounux@users.noreply.github.com> Date: Tue, 5 Nov 2024 13:16:31 +0100 Subject: [PATCH 1/2] refactor(qchat): use self.settings instead of PlgOptionsManager (#217) --- qtribu/gui/dck_qchat.py | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/qtribu/gui/dck_qchat.py b/qtribu/gui/dck_qchat.py index 1b735e98..37c5e972 100644 --- a/qtribu/gui/dck_qchat.py +++ b/qtribu/gui/dck_qchat.py @@ -434,8 +434,8 @@ def on_uncompliant_message_received(self, message: QChatUncompliantMessage) -> N ), application=self.tr("QChat"), log_level=Qgis.Critical, - push=PlgOptionsManager().get_plg_settings().notify_push_info, - duration=PlgOptionsManager().get_plg_settings().notify_push_duration, + push=self.settings.notify_push_info, + duration=self.settings.notify_push_duration, ) def on_text_message_received(self, message: QChatTextMessage) -> None: @@ -464,10 +464,8 @@ def on_text_message_received(self, message: QChatTextMessage) -> None: ).format(sender=message.author, message=message.text), application=self.tr("QChat"), log_level=Qgis.Info, - push=PlgOptionsManager().get_plg_settings().notify_push_info, - duration=PlgOptionsManager() - .get_plg_settings() - .notify_push_duration, + push=self.settings.notify_push_info, + duration=self.settings.notify_push_duration, ) # check if a notification sound should be played @@ -535,8 +533,8 @@ def on_like_message_received(self, message: QChatLikeMessage) -> None: ), application=self.tr("QChat"), log_level=Qgis.Success, - push=PlgOptionsManager().get_plg_settings().notify_push_info, - duration=PlgOptionsManager().get_plg_settings().notify_push_duration, + push=self.settings.notify_push_info, + duration=self.settings.notify_push_duration, ) # play a notification sound if enabled if self.settings.qchat_play_sounds: @@ -691,8 +689,8 @@ def on_send_button_clicked(self) -> None: self.log( message=self.tr("Nickname not set : please open settings and set it"), log_level=Qgis.Warning, - push=PlgOptionsManager().get_plg_settings().notify_push_info, - duration=PlgOptionsManager().get_plg_settings().notify_push_duration, + push=self.settings.notify_push_info, + duration=self.settings.notify_push_duration, button=True, button_label=self.tr("Open Settings"), button_connect=self.on_settings_button_clicked, @@ -705,8 +703,8 @@ def on_send_button_clicked(self) -> None: "Nickname too short : must be at least 3 characters. Please open settings and set it" ), log_level=Qgis.Warning, - push=PlgOptionsManager().get_plg_settings().notify_push_info, - duration=PlgOptionsManager().get_plg_settings().notify_push_duration, + push=self.settings.notify_push_info, + duration=self.settings.notify_push_duration, button=True, button_label=self.tr("Open Settings"), button_connect=self.on_settings_button_clicked, @@ -799,8 +797,8 @@ def check_cheatcode(self, text: str) -> bool: message=self.tr("Your QGIS Pro license is about to expire"), application=self.tr("QGIS Pro"), log_level=Qgis.Warning, - push=PlgOptionsManager().get_plg_settings().notify_push_info, - duration=PlgOptionsManager().get_plg_settings().notify_push_duration, + push=self.settings.notify_push_info, + duration=self.settings.notify_push_duration, button=True, button_label=self.tr("Click here to renew it"), button_connect=self.on_renew_clicked, @@ -851,8 +849,8 @@ def on_send_layer_to_qchat(self) -> None: ), application=self.tr("QChat"), log_level=Qgis.Critical, - push=PlgOptionsManager().get_plg_settings().notify_push_info, - duration=PlgOptionsManager().get_plg_settings().notify_push_duration, + push=self.settings.notify_push_info, + duration=self.settings.notify_push_duration, ) return layer = self.iface.activeLayer() @@ -861,8 +859,8 @@ def on_send_layer_to_qchat(self) -> None: message=self.tr("No active layer in current QGIS project"), application=self.tr("QChat"), log_level=Qgis.Critical, - push=PlgOptionsManager().get_plg_settings().notify_push_info, - duration=PlgOptionsManager().get_plg_settings().notify_push_duration, + push=self.settings.notify_push_info, + duration=self.settings.notify_push_duration, ) return if layer.type() != QgsMapLayer.VectorLayer: @@ -870,8 +868,8 @@ def on_send_layer_to_qchat(self) -> None: message=self.tr("Only vector layers can be sent on QChat"), application=self.tr("QChat"), log_level=Qgis.Critical, - push=PlgOptionsManager().get_plg_settings().notify_push_info, - duration=PlgOptionsManager().get_plg_settings().notify_push_duration, + push=self.settings.notify_push_info, + duration=self.settings.notify_push_duration, ) return From eb21b4e0b4b5b51b4d042f46730f6d888969dcee Mon Sep 17 00:00:00 2001 From: Guilhem Allaman <40383801+gounux@users.noreply.github.com> Date: Wed, 6 Nov 2024 09:41:19 +0100 Subject: [PATCH 2/2] fix(qchat): add type key check when receiving a message (#218) * refactor(qchat): use constants for message types * fix(qchat): add type key check when receiving a message --- qtribu/constants.py | 10 ++++++++++ qtribu/gui/dck_qchat.py | 20 ++++++++++++++------ qtribu/logic/qchat_websocket.py | 33 +++++++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/qtribu/constants.py b/qtribu/constants.py index c019654a..2b5a9b0d 100644 --- a/qtribu/constants.py +++ b/qtribu/constants.py @@ -230,3 +230,13 @@ def local_path(self, base_path: Path = Path().home() / ".geotribu/cdn/") -> Path CHEATCODE_10OCLOCK, CHEATCODE_QGIS_PRO_LICENSE, ] + +# QChat message types +QCHAT_MESSAGE_TYPE_UNCOMPLIANT = "uncompliant" +QCHAT_MESSAGE_TYPE_TEXT = "text" +QCHAT_MESSAGE_TYPE_IMAGE = "image" +QCHAT_MESSAGE_TYPE_NB_USERS = "nb_users" +QCHAT_MESSAGE_TYPE_NEWCOMER = "newcomer" +QCHAT_MESSAGE_TYPE_EXITER = "exiter" +QCHAT_MESSAGE_TYPE_LIKE = "like" +QCHAT_MESSAGE_TYPE_GEOJSON = "geojson" diff --git a/qtribu/gui/dck_qchat.py b/qtribu/gui/dck_qchat.py index 37c5e972..6dc66aa4 100644 --- a/qtribu/gui/dck_qchat.py +++ b/qtribu/gui/dck_qchat.py @@ -31,6 +31,11 @@ CHEATCODE_IAMAROBOT, CHEATCODE_QGIS_PRO_LICENSE, CHEATCODES, + QCHAT_MESSAGE_TYPE_GEOJSON, + QCHAT_MESSAGE_TYPE_IMAGE, + QCHAT_MESSAGE_TYPE_LIKE, + QCHAT_MESSAGE_TYPE_NEWCOMER, + QCHAT_MESSAGE_TYPE_TEXT, QCHAT_NICKNAME_MINLENGTH, ) from qtribu.gui.qchat_tree_widget_items import ( @@ -382,7 +387,7 @@ def on_ws_connected(self, room: str) -> None: # send newcomer message to websocket if not self.settings.qchat_incognito_mode: message = QChatNewcomerMessage( - type="newcomer", newcomer=self.settings.author_nickname + type=QCHAT_MESSAGE_TYPE_NEWCOMER, newcomer=self.settings.author_nickname ) self.qchat_ws.send_message(message) @@ -576,7 +581,7 @@ def on_like_message(self, liked_author: str, msg: str) -> None: This may happen on right-click on a message """ message = QChatLikeMessage( - type="like", + type=QCHAT_MESSAGE_TYPE_LIKE, liker_author=self.settings.author_nickname, liked_author=liked_author, message=msg, @@ -716,7 +721,10 @@ def on_send_button_clicked(self) -> None: # send message to websocket message = QChatTextMessage( - type="text", author=nickname, avatar=avatar, text=message_text.strip() + type=QCHAT_MESSAGE_TYPE_TEXT, + author=nickname, + avatar=avatar, + text=message_text.strip(), ) self.qchat_ws.send_message(message) self.lne_message.setText("") @@ -737,7 +745,7 @@ def on_send_image_button_clicked(self) -> None: with open(fp, "rb") as file: data = file.read() message = QChatImageMessage( - type="image", + type=QCHAT_MESSAGE_TYPE_IMAGE, author=self.settings.author_nickname, avatar=self.settings.author_avatar, image_data=base64.b64encode(data).decode("utf-8"), @@ -750,7 +758,7 @@ def on_send_screenshot_button_clicked(self) -> None: with open(sc_fp, "rb") as file: data = file.read() message = QChatImageMessage( - type="image", + type=QCHAT_MESSAGE_TYPE_IMAGE, author=self.settings.author_nickname, avatar=self.settings.author_avatar, image_data=base64.b64encode(data).decode("utf-8"), @@ -879,7 +887,7 @@ def on_send_layer_to_qchat(self) -> None: exporter.setTransformGeometries(True) geojson_str = exporter.exportFeatures(layer.getFeatures()) message = QChatGeojsonMessage( - type="geojson", + type=QCHAT_MESSAGE_TYPE_GEOJSON, author=self.settings.author_nickname, avatar=self.settings.author_avatar, layer_name=layer.name(), diff --git a/qtribu/logic/qchat_websocket.py b/qtribu/logic/qchat_websocket.py index ba3928e1..d5613e07 100644 --- a/qtribu/logic/qchat_websocket.py +++ b/qtribu/logic/qchat_websocket.py @@ -3,8 +3,19 @@ from json import JSONEncoder from PyQt5 import QtWebSockets # noqa QGS103 +from qgis.core import Qgis from qgis.PyQt.QtCore import QObject, QUrl, pyqtSignal +from qtribu.constants import ( + QCHAT_MESSAGE_TYPE_EXITER, + QCHAT_MESSAGE_TYPE_GEOJSON, + QCHAT_MESSAGE_TYPE_IMAGE, + QCHAT_MESSAGE_TYPE_LIKE, + QCHAT_MESSAGE_TYPE_NB_USERS, + QCHAT_MESSAGE_TYPE_NEWCOMER, + QCHAT_MESSAGE_TYPE_TEXT, + QCHAT_MESSAGE_TYPE_UNCOMPLIANT, +) from qtribu.logic.qchat_messages import ( QChatExiterMessage, QChatGeojsonMessage, @@ -97,20 +108,26 @@ def on_message_received(self, text: str) -> None: :param text: text message received, should be a jsonified string """ message = json.loads(text) + if "type" not in message: + self.log( + message="No 'type' key in received message. Please make sure your configured instance is running gischat v>=2.0.0", + log_level=Qgis.Critical, + ) + return msg_type = message["type"] - if msg_type == "uncompliant": + if msg_type == QCHAT_MESSAGE_TYPE_UNCOMPLIANT: self.uncompliant_message_received.emit(QChatUncompliantMessage(**message)) - elif msg_type == "text": + elif msg_type == QCHAT_MESSAGE_TYPE_TEXT: self.text_message_received.emit(QChatTextMessage(**message)) - elif msg_type == "image": + elif msg_type == QCHAT_MESSAGE_TYPE_IMAGE: self.image_message_received.emit(QChatImageMessage(**message)) - elif msg_type == "nb_users": + elif msg_type == QCHAT_MESSAGE_TYPE_NB_USERS: self.nb_users_message_received.emit(QChatNbUsersMessage(**message)) - elif msg_type == "newcomer": + elif msg_type == QCHAT_MESSAGE_TYPE_NEWCOMER: self.newcomer_message_received.emit(QChatNewcomerMessage(**message)) - elif msg_type == "exiter": + elif msg_type == QCHAT_MESSAGE_TYPE_EXITER: self.exiter_message_received.emit(QChatExiterMessage(**message)) - elif msg_type == "like": + elif msg_type == QCHAT_MESSAGE_TYPE_LIKE: self.like_message_received.emit(QChatLikeMessage(**message)) - elif msg_type == "geojson": + elif msg_type == QCHAT_MESSAGE_TYPE_GEOJSON: self.geojson_message_received.emit(QChatGeojsonMessage(**message))