Philipp Hörist pushed to branch mute-chat-notifications at gajim / gajim
Commits: db4b52d8 by Philipp Hörist at 2023-04-02T11:40:37+02:00 imprv: Add Contact.is_muted
- - - - - f458a78b by Philipp Hörist at 2023-04-02T11:40:37+02:00 imprv: Rely more on ENUMs
- - - - - 02d06fe1 by Philipp Hörist at 2023-04-02T11:40:37+02:00 fix: Add missing permanent option
- - - - -
6 changed files:
- gajim/common/modules/contacts.py - gajim/gtk/application.py - gajim/gtk/const.py - gajim/gtk/menus.py - gajim/gtk/notification.py - gajim/gtk/structs.py
Changes:
===================================== gajim/common/modules/contacts.py ===================================== @@ -20,6 +20,9 @@ from typing import overload from typing import Union
+from datetime import datetime +from datetime import timezone + import cairo from nbxmpp.const import Affiliation from nbxmpp.const import Chatstate @@ -332,6 +335,17 @@ def chatstate(self) -> Optional[Chatstate]: def chatstate_string(self) -> str: return chatstate_to_string(self.chatstate)
+ @property + def is_muted(self) -> bool: + mute_until = self.settings.get('mute_until') + if not mute_until: + return False + + until = datetime.fromisoformat(mute_until) + if until > datetime.now(timezone.utc): + return True + return False + def __repr__(self) -> str: return f'{self.jid} ({self._account})'
@@ -639,6 +653,10 @@ def update_presence(self, def type_string(self) -> str: raise NotImplementedError
+ @property + def is_muted(self) -> bool: + raise NotImplementedError +
class GroupchatContact(CommonContact): def __init__(self, logger: LogAdapter, jid: JID, account: str) -> None:
===================================== gajim/gtk/application.py ===================================== @@ -82,6 +82,7 @@ from gajim.gtk.const import ALWAYS_ACCOUNT_ACTIONS from gajim.gtk.const import APP_ACTIONS from gajim.gtk.const import FEATURE_ACCOUNT_ACTIONS +from gajim.gtk.const import MuteState from gajim.gtk.const import ONLINE_ACCOUNT_ACTIONS from gajim.gtk.dialogs import ConfirmationDialog from gajim.gtk.dialogs import DialogButton @@ -827,19 +828,12 @@ def _on_mute_chat_action(_action: Gio.SimpleAction, client = app.get_client(params.account) contact = client.get_module('Contacts').get_contact(params.jid)
- # 0: unmute - # -1: permanently - # int: minutes - if params.minutes == 0: - contact.settings.set('mute_until', '') + if params.state == MuteState.NOT_MUTED: + contact.settings.set('mute_until', None) return
- if params.minutes == -1: - contact.settings.set('mute_until', 'permanently') - return - - until = datetime.now(timezone.utc) + timedelta(minutes=params.minutes) - contact.settings.set('mute_until', until.astimezone().isoformat()) + until = datetime.now(timezone.utc) + timedelta(minutes=params.state) + contact.settings.set('mute_until', until.isoformat())
@staticmethod @structs.actionfunction
===================================== gajim/gtk/const.py ===================================== @@ -18,6 +18,7 @@
from typing import Any from typing import Callable +from typing import Iterator from typing import NamedTuple from typing import Optional from typing import Union @@ -26,6 +27,7 @@ from enum import IntEnum from enum import unique
+from gajim.common.i18n import _ from gajim.common.setting_values import AllSettingsT
@@ -58,6 +60,28 @@ class Setting(NamedTuple): MAX_MESSAGE_LENGTH = 5000
+@unique +class MuteState(IntEnum): + NOT_MUTED = 0 + MIN_30 = 30 + MIN_60 = 60 + MIN_120 = 120 + MIN_480 = 480 + PERM = 100000000 + + @classmethod + def iter(cls) -> Iterator[tuple[int, str]]: + yield from cls._labels.items() # pyright: ignore + +MuteState._labels = { # pyright: ignore + MuteState.MIN_30: _('30 minutes'), + MuteState.MIN_60: _('1 hour'), + MuteState.MIN_120: _('2 hours'), + MuteState.MIN_480: _('8 hours'), + MuteState.PERM: _('Permanently'), + } + + @unique class Theme(IntEnum): NOT_DARK = 0
===================================== gajim/gtk/menus.py ===================================== @@ -24,7 +24,6 @@
import textwrap from datetime import datetime -from datetime import timezone from urllib.parse import quote
from gi.repository import Gio @@ -50,6 +49,7 @@ from gajim.common.structs import URI from gajim.common.util.text import escape_iri_path_segment
+from gajim.gtk.const import MuteState from gajim.gtk.structs import AccountJidParam from gajim.gtk.structs import AddChatActionParams from gajim.gtk.structs import ChatListEntryParam @@ -62,13 +62,6 @@ UriMenuItemsT = list[tuple[str, list[str], str]] UriMenuBuilderT = Callable[[URI, str], UriMenuItemsT]
-MUTE_CHAT_ITEMS = [ - (_('30 min'), 30), - (_('1 hour'), 60), - (_('2 hours'), 120), - (_('8 hours'), 480), - (_('Permanently'), -1), -]
def get_self_contact_menu(contact: types.BareContact) -> GajimMenu: account = contact.account @@ -541,28 +534,20 @@ def get_chat_list_row_menu(workspace_id: str, params = AccountJidParam(account=account, jid=jid) menu.add_item(_('Mark as read'), 'win.mark-as-read', params)
- contact_muted = False - mute_until = contact.settings.get('mute_until') - if mute_until: - if mute_until == 'permanently': - contact_muted = True - else: - until = datetime.fromisoformat(mute_until) - if until > datetime.now(timezone.utc).astimezone(): - contact_muted = True - - if contact_muted: + if contact.is_muted: menu.add_item( _('Unmute Chat'), 'app.mute-chat', - MuteContactParam(account=account, jid=jid, minutes=0)) + MuteContactParam(account=account, + jid=jid, + state=MuteState.NOT_MUTED)) else: submenu = menu.add_submenu(_('Mute Chat')) - for label, amount in MUTE_CHAT_ITEMS: + for state, label in MuteState.iter(): submenu.add_item( label, 'app.mute-chat', - MuteContactParam(account=account, jid=jid, minutes=amount)) + MuteContactParam(account=account, jid=jid, state=state))
return menu
===================================== gajim/gtk/notification.py ===================================== @@ -33,8 +33,6 @@ import logging import sys import textwrap -from datetime import datetime -from datetime import timezone
from gi.repository import Gdk from gi.repository import GdkPixbuf @@ -96,24 +94,11 @@ def _on_notification(self, event: events.Notification) -> None: if event.account and event.jid: client = app.get_client(event.account) contact = client.get_module('Contacts').get_contact(event.jid) - mute_until = contact.settings.get('mute_until')
- # '': not muted - # int: minutes - if mute_until == 'permanently': - log.debug('Not issuing notification (chat is muted ' - 'permanently) %s', contact) + if contact.is_muted: + log.debug('Notifications muted for %s', contact) return
- if mute_until: - until = datetime.fromisoformat(mute_until) - if until > datetime.now(timezone.utc).astimezone(): - log.debug( - 'Not issuing notification (chat is muted until %s) %s', - until.isoformat(), - contact) - return - if event.sound is not None: play_sound(event.sound, event.account)
===================================== gajim/gtk/structs.py ===================================== @@ -65,7 +65,7 @@ class ChatListEntryParam(VariantMixin): class MuteContactParam(VariantMixin): account: str jid: JID - minutes: int + state: int
@dataclass
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/632099b43de726401c2f4693b7c47b8f...