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))