Commit 7b9278ae authored by Philipp Hörist's avatar Philipp Hörist

Chatstate: Use StanzaHandler instead of delegate()

parent 1af5612d
Pipeline #4382 passed with stages
in 2 minutes and 51 seconds
......@@ -51,17 +51,6 @@ def ensure_enabled(func):
return func_wrapper
def parse_chatstate(stanza: nbxmpp.Message) -> Optional[str]:
if stanza.getTag('delay', namespace=nbxmpp.NS_DELAY2) is not None:
return None
children = stanza.getChildren()
for child in children:
if child.getNamespace() == nbxmpp.NS_CHATSTATES:
return child.getName()
return None
class Chatstate(BaseModule):
def __init__(self, con: ConnectionT) -> None:
BaseModule.__init__(self, con)
......@@ -69,6 +58,10 @@ class Chatstate(BaseModule):
self.handlers = [
StanzaHandler(name='presence',
callback=self._presence_received),
StanzaHandler(name='message',
callback=self._process_chatstate,
ns=nbxmpp.NS_CHATSTATES,
priority=46),
]
# Our current chatstate with a specific contact
......@@ -144,35 +137,39 @@ class Chatstate(BaseModule):
account=self._account,
contact=contact))
def delegate(self, event: Any) -> None:
if self._con.get_own_jid().bareMatch(event.jid) or event.sent:
# Dont show chatstates from our own resources
def _process_chatstate(self, _con, _stanza, properties):
if not properties.has_chatstate:
return
if event.mtype == 'groupchat':
# Not implemented yet
if (properties.is_self_message or
properties.type.is_groupchat or
properties.is_carbon_message and properties.carbon.is_sent):
if properties.chatstate in ('inactive', 'gone',
'composing', 'paused'):
raise nbxmpp.NodeProcessed
return
chatstate = parse_chatstate(event.stanza)
if chatstate is None:
return
if event.muc_pm:
if properties.is_muc_pm:
contact = app.contacts.get_gc_contact(
self._account, event.jid, event.resource)
self._account,
properties.jid.getBare(),
properties.jid.getResource())
else:
contact = app.contacts.get_contact_from_full_jid(
self._account, event.fjid)
self._account, str(properties.jid))
if contact is None:
return
contact.chatstate = chatstate
self._log.info('Recv: %-10s - %s', chatstate, event.fjid)
contact.chatstate = properties.chatstate
self._log.info('Recv: %-10s - %s', properties.chatstate, properties.jid)
app.nec.push_outgoing_event(
NetworkEvent('chatstate-received',
account=self._account,
contact=contact))
if properties.chatstate in ('inactive', 'gone', 'composing', 'paused'):
raise nbxmpp.NodeProcessed
@ensure_enabled
def _check_last_interaction(self) -> GLib.SOURCE_CONTINUE:
now = time.time()
......
......@@ -198,11 +198,6 @@ class Message(BaseModule):
self._on_message_decrypted(event)
def _on_message_decrypted(self, event):
try:
self._con.get_module('Chatstate').delegate(event)
except nbxmpp.NodeProcessed:
return
groupchat = event.mtype == 'groupchat'
event_attr = {
......
......@@ -22,8 +22,6 @@
import time
import logging
import nbxmpp
from gajim.common import app
from gajim.common import connection_handlers
......@@ -135,11 +133,6 @@ class ConnectionHandlersZeroconf(connection_handlers.ConnectionHandlersBase):
self._on_message_decrypted(event)
def _on_message_decrypted(self, event):
try:
self.get_module('Chatstate').delegate(event)
except nbxmpp.NodeProcessed:
return
event_attr = {
'popup': False,
'msg_log_id': 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