Commit e77e74e4 authored by Philipp Hörist's avatar Philipp Hörist

Add Chatstate enum

parent 932cdc77
Pipeline #7394 passed with stages
in 5 minutes and 35 seconds
......@@ -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',
......
......@@ -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
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment