Skip to content

Commit

Permalink
fix(qchat): add type key check when receiving a message (#218)
Browse files Browse the repository at this point in the history
* refactor(qchat): use constants for message types

* fix(qchat): add type key check when receiving a message
  • Loading branch information
gounux authored and Guts committed Nov 6, 2024
1 parent 2dd2c35 commit 17d8318
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
10 changes: 10 additions & 0 deletions qtribu/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
20 changes: 14 additions & 6 deletions qtribu/gui/dck_qchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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("")
Expand All @@ -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"),
Expand All @@ -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"),
Expand Down Expand Up @@ -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(),
Expand Down
33 changes: 25 additions & 8 deletions qtribu/logic/qchat_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))

0 comments on commit 17d8318

Please sign in to comment.