Skip to content

Commit

Permalink
add internal message handling
Browse files Browse the repository at this point in the history
  • Loading branch information
gounux committed Jul 24, 2024
1 parent 2371906 commit ce77ccf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions qtribu/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
30 changes: 28 additions & 2 deletions qtribu/gui/wdg_qchat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand All @@ -23,6 +23,7 @@
CHEATCODE_DIZZY,
CHEATCODE_DONTCRYBABY,
CHEATCODE_IAMAROBOT,
INTERNAL_MESSAGE_AUTHOR,
MENTION_MESSAGES_COLOR,
QCHAT_NICKNAME_MINLENGTH,
USER_MESSAGES_COLOR,
Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions qtribu/gui/wdg_qchat.ui
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
</widget>
</item>
<item>
<widget class="QgsCollapsibleGroupBox" name="grb_chat">
<widget class="QgsCollapsibleGroupBox" name="grb_qchat">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
Expand All @@ -169,7 +169,7 @@
</size>
</property>
<property name="title">
<string>Chat</string>
<string>QChat</string>
</property>
<property name="collapsed">
<bool>false</bool>
Expand Down Expand Up @@ -313,7 +313,7 @@
</customwidgets>
<tabstops>
<tabstop>grb_rooms</tabstop>
<tabstop>grb_chat</tabstop>
<tabstop>grb_qchat</tabstop>
<tabstop>grb_user</tabstop>
</tabstops>
<resources/>
Expand Down

0 comments on commit ce77ccf

Please sign in to comment.