Philipp Hörist pushed to branch master at gajim / python-nbxmpp


Commits:
e77e74e4 by lovetox at 2021-02-27T11:43:03+01:00
Add Chatstate enum

- - - - -


2 changed files:

- nbxmpp/const.py
- nbxmpp/modules/chatstates.py


Changes:

=====================================
nbxmpp/const.py
=====================================
@@ -162,6 +162,52 @@ PresenceShow._WEIGHTS = {
 }
 
 
+@total_ordering
+class Chatstate(Enum):
+    COMPOSING = 'composing'
+    PAUSED = 'paused'
+    ACTIVE = 'active'
+    INACTIVE = 'inactive'
+    GONE = 'gone'
+
+    @property
+    def is_composing(self):
+        return self == Chatstate.COMPOSING
+
+    @property
+    def is_paused(self):
+        return self == Chatstate.PAUSED
+
+    @property
+    def is_active(self):
+        return self == Chatstate.ACTIVE
+
+    @property
+    def is_inactive(self):
+        return self == Chatstate.INACTIVE
+
+    @property
+    def is_gone(self):
+        return self == Chatstate.GONE
+
+    def __lt__(self, other):
+        try:
+            w1 = self._WEIGHTS[self]
+            w2 = self._WEIGHTS[other]
+        except KeyError:
+            return NotImplemented
+        return w1 < w2
+
+
+Chatstate._WEIGHTS = {
+    Chatstate.COMPOSING: 0,
+    Chatstate.PAUSED: 1,
+    Chatstate.ACTIVE: 2,
+    Chatstate.INACTIVE: 3,
+    Chatstate.GONE: 4,
+}
+
+
 class StatusCode(Enum):
     NON_ANONYMOUS = '100'
     AFFILIATION_CHANGE = '101'
@@ -606,14 +652,6 @@ TUNE_DATA = [
     'uri']
 
 
-CHATSTATES = [
-    'active',
-    'inactive',
-    'gone',
-    'composing',
-    'paused'
-]
-
 REGISTER_FIELDS = [
     'username',
     'nick',


=====================================
nbxmpp/modules/chatstates.py
=====================================
@@ -17,7 +17,7 @@
 
 from nbxmpp.namespaces import Namespace
 from nbxmpp.structs import StanzaHandler
-from nbxmpp.const import CHATSTATES
+from nbxmpp.const import Chatstate
 from nbxmpp.modules.base import BaseModule
 
 
@@ -34,7 +34,13 @@ class Chatstates(BaseModule):
         ]
 
     def _process_message_chatstate(self, _client, stanza, properties):
-        chatstate = parse_chatstate(stanza)
+        try:
+            chatstate = parse_chatstate(stanza)
+        except ValueError as error:
+            self._log.warning('Invalid chatstate: %s', error)
+            self._log.warning(stanza)
+            return
+
         if chatstate is None:
             return
 
@@ -44,11 +50,6 @@ class Chatstates(BaseModule):
         if stanza.getTag('delay', namespace=Namespace.DELAY2) is not None:
             return
 
-        if chatstate not in CHATSTATES:
-            self._log.warning('Invalid chatstate: %s', chatstate)
-            self._log.warning(stanza)
-            return
-
         properties.chatstate = chatstate
 
 
@@ -56,5 +57,5 @@ def parse_chatstate(stanza):
     children = stanza.getChildren()
     for child in children:
         if child.getNamespace() == Namespace.CHATSTATES:
-            return child.getName()
+            return Chatstate(child.getName())
     return None



View it on GitLab: 
https://dev.gajim.org/gajim/python-nbxmpp/-/commit/e77e74e4c52dceff2d35197d6e598fd14f9bff20

-- 
View it on GitLab: 
https://dev.gajim.org/gajim/python-nbxmpp/-/commit/e77e74e4c52dceff2d35197d6e598fd14f9bff20
You're receiving this email because of your account on dev.gajim.org.


_______________________________________________
Commits mailing list
[email protected]
https://lists.gajim.org/cgi-bin/listinfo/commits

Reply via email to