Refactor last activity code into own module

This commit is contained in:
Philipp Hörist 2018-06-25 23:00:40 +02:00
parent b74706afcf
commit 8fa1ee4617
4 changed files with 54 additions and 33 deletions

View file

@ -69,6 +69,7 @@ from gajim.common.modules.ping import Ping
from gajim.common.modules.search import Search
from gajim.common.modules.annotations import Annotations
from gajim.common.modules.roster_item_exchange import RosterItemExchange
from gajim.common.modules.last_activity import LastActivity
from gajim.common.connection_handlers import *
from gajim.common.contacts import GC_Contact
from gajim.gtkgui_helpers import get_action
@ -662,6 +663,7 @@ class Connection(CommonConnection, ConnectionHandlers):
self.register_module('Search', Search, self)
self.register_module('Annotations', Annotations, self)
self.register_module('RosterItemExchange', RosterItemExchange, self)
self.register_module('LastActivity', LastActivity, self)
app.ged.register_event_handler('privacy-list-received', ged.CORE,
self._nec_privacy_list_received)

View file

@ -48,7 +48,6 @@ from gajim.common import app
from gajim.common import dataforms
from gajim.common import jingle_xtls
from gajim.common import configpaths
from gajim.common import idle
from gajim.common.caps_cache import muc_caps_cache
from gajim.common.commands import ConnectionCommands
from gajim.common.pubsub import ConnectionPubSub
@ -1345,8 +1344,6 @@ ConnectionHTTPUpload):
app.ged.register_event_handler('http-auth-received', ged.CORE,
self._nec_http_auth_received)
app.ged.register_event_handler('last-request-received', ged.CORE,
self._nec_last_request_received)
app.ged.register_event_handler('roster-set-received',
ged.CORE, self._nec_roster_set_received)
app.ged.register_event_handler('private-storage-bookmarks-received',
@ -1380,8 +1377,6 @@ ConnectionHTTPUpload):
ConnectionHTTPUpload.cleanup(self)
app.ged.remove_event_handler('http-auth-received', ged.CORE,
self._nec_http_auth_received)
app.ged.remove_event_handler('last-request-received', ged.CORE,
self._nec_last_request_received)
app.ged.remove_event_handler('roster-set-received',
ged.CORE, self._nec_roster_set_received)
app.ged.remove_event_handler('private-storage-bookmarks-received',
@ -1640,29 +1635,6 @@ ConnectionHTTPUpload):
app.config.set_per('accounts', self.name, 'roster_version',
obj.version)
def _LastCB(self, con, iq_obj):
log.debug('LastCB')
if not self.connection or self.connected < 2:
return
app.nec.push_incoming_event(LastRequestEvent(None, conn=self,
stanza=iq_obj))
raise nbxmpp.NodeProcessed
def _nec_last_request_received(self, obj):
if obj.conn.name != self.name:
return
if app.is_installed('IDLE') and app.config.get_per('accounts', self.name,
'send_idle_time'):
iq_obj = obj.stanza.buildReply('result')
qp = iq_obj.setQuery()
qp.attrs['seconds'] = idle.Monitor.get_idle_sec()
else:
iq_obj = obj.stanza.buildReply('error')
err = nbxmpp.ErrorNode(name=nbxmpp.NS_STANZAS + \
' service-unavailable')
iq_obj.addChild(node=err)
self.connection.send(iq_obj)
def _messageCB(self, con, msg):
"""
Called when we receive a message
@ -2086,7 +2058,6 @@ ConnectionHTTPUpload):
nbxmpp.NS_DISCO_INFO)
con.RegisterHandler('iq', self._DiscoverInfoErrorCB, 'error',
nbxmpp.NS_DISCO_INFO)
con.RegisterHandler('iq', self._LastCB, 'get', nbxmpp.NS_LAST)
con.RegisterHandler('iq', self._MucOwnerCB, 'result',
nbxmpp.NS_MUC_OWNER)
con.RegisterHandler('iq', self._MucAdminCB, 'result',

View file

@ -193,10 +193,6 @@ class HttpAuthReceivedEvent(nec.NetworkIncomingEvent):
self.msg = self.stanza.getTagData('body')
return True
class LastRequestEvent(nec.NetworkIncomingEvent):
name = 'last-request-received'
base_network_events = []
class RosterReceivedEvent(nec.NetworkIncomingEvent):
name = 'roster-received'
base_network_events = []

View file

@ -0,0 +1,52 @@
# This file is part of Gajim.
#
# Gajim is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; version 3 only.
#
# Gajim is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
# XEP-0012: Last Activity
import logging
import nbxmpp
from gajim.common import app
from gajim.common import idle
log = logging.getLogger('gajim.c.m.last_activity')
class LastActivity:
def __init__(self, con):
self._con = con
self._account = con.name
self.handlers = [('iq', self._answer_request, 'get', nbxmpp.NS_LAST)]
def _answer_request(self, con, stanza):
log.info('Request from %s', stanza.getFrom())
if not app.account_is_connected(self._account):
return
allow_send = app.config.get_per(
'accounts', self._account, 'send_idle_time')
if app.is_installed('IDLE') and allow_send:
iq = stanza.buildReply('result')
query = iq.setQuery()
query.attrs['seconds'] = idle.Monitor.get_idle_sec()
else:
iq = stanza.buildReply('error')
err = nbxmpp.ErrorNode(nbxmpp.ERR_SERVICE_UNAVAILABLE)
iq.addChild(node=err)
self._con.connection.send(iq)
raise nbxmpp.NodeProcessed