Skip to content

Commit

Permalink
fix(qchat): add uncompliant messages logic
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Oct 27, 2024
1 parent 0b95b42 commit 79e9568
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
15 changes: 15 additions & 0 deletions qtribu/gui/dck_qchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
QChatNbUsersMessage,
QChatNewcomerMessage,
QChatTextMessage,
QChatUncompliantMessage,
)
from qtribu.logic.qchat_websocket import QChatWebsocket
from qtribu.tasks.dizzy import DizzyTask
Expand Down Expand Up @@ -143,6 +144,9 @@ def __init__(
# initialize websocket client
self.qchat_ws = QChatWebsocket()
self.qchat_ws.error.connect(self.on_ws_error)
self.qchat_ws.uncompliant_message_received.connect(
self.on_uncompliant_message_received
)
self.qchat_ws.text_message_received.connect(self.on_text_message_received)
self.qchat_ws.image_message_received.connect(self.on_image_message_received)
self.qchat_ws.nb_users_message_received.connect(
Expand Down Expand Up @@ -423,6 +427,17 @@ def on_ws_error(self, error_code: int) -> None:

# region websocket message received

def on_uncompliant_message_received(self, message: QChatUncompliantMessage) -> None:
self.log(
message=self.tr("Uncompliant message: {reason}").format(
reason=message.reason
),
application=self.tr("QChat"),
log_level=Qgis.Critical,
push=PlgOptionsManager().get_plg_settings().notify_push_info,
duration=PlgOptionsManager().get_plg_settings().notify_push_duration,
)

def on_text_message_received(self, message: QChatTextMessage) -> None:
"""
Launched when a text message is received from the websocket
Expand Down
5 changes: 5 additions & 0 deletions qtribu/logic/qchat_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class QChatMessage:
type: str


@dataclass(init=True, frozen=True)
class QChatUncompliantMessage(QChatMessage):
reason: str


@dataclass(init=True, frozen=True)
class QChatTextMessage(QChatMessage):
author: str
Expand Down
6 changes: 5 additions & 1 deletion qtribu/logic/qchat_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
QChatNbUsersMessage,
QChatNewcomerMessage,
QChatTextMessage,
QChatUncompliantMessage,
)
from qtribu.toolbelt import PlgLogger

Expand Down Expand Up @@ -49,6 +50,7 @@ def __init__(self):
error = pyqtSignal(int)

# QChat message signals
uncompliant_message_received = pyqtSignal(QChatUncompliantMessage)
text_message_received = pyqtSignal(QChatTextMessage)
image_message_received = pyqtSignal(QChatImageMessage)
nb_users_message_received = pyqtSignal(QChatNbUsersMessage)
Expand Down Expand Up @@ -96,7 +98,9 @@ def on_message_received(self, text: str) -> None:
"""
message = json.loads(text)
msg_type = message["type"]
if msg_type == "text":
if msg_type == "uncompliant":
self.uncompliant_message_received.emit(QChatUncompliantMessage(**message))
elif msg_type == "text":
self.text_message_received.emit(QChatTextMessage(**message))
elif msg_type == "image":
self.image_message_received.emit(QChatImageMessage(**message))
Expand Down

0 comments on commit 79e9568

Please sign in to comment.