Commit 94b6b0c2 authored by Philipp Hörist's avatar Philipp Hörist
Browse files

Use nbxmpp last activity module

parent 9e0c64b1
Pipeline #7319 passed with stages
in 7 minutes and 8 seconds
......@@ -435,6 +435,7 @@ def _finish_connect(self):
self.get_module('Bookmarks').request_bookmarks()
self.get_module('SoftwareVersion').set_enabled(True)
self.get_module('LastActivity').set_enabled(True)
self.get_module('Annotations').request_annotations()
self.get_module('Blocking').get_blocking_list()
......
......@@ -14,45 +14,33 @@
# XEP-0012: Last Activity
import nbxmpp
from nbxmpp.namespaces import Namespace
from nbxmpp.structs import StanzaHandler
from gajim.common import app
from gajim.common import idle
from gajim.common.modules.base import BaseModule
class LastActivity(BaseModule):
_nbxmpp_extends = 'LastActivity'
_nbxmpp_methods = [
'request_last_activity',
'set_idle_func',
'disable',
]
def __init__(self, con):
BaseModule.__init__(self, con)
self.handlers = [
StanzaHandler(name='iq',
typ='get',
callback=self._answer_request,
ns=Namespace.LAST),
]
def _answer_request(self, _con, stanza, properties):
self._log.info('Request from %s', properties.jid)
allow_send = app.settings.get_account_setting(self._account,
'send_idle_time')
if app.is_installed('IDLE') and allow_send:
iq = stanza.buildReply('result')
query = iq.setQuery()
seconds = idle.Monitor.get_idle_sec()
query.attrs['seconds'] = seconds
self._log.info('Respond with seconds: %s', seconds)
else:
iq = stanza.buildReply('error')
err = nbxmpp.ErrorNode(nbxmpp.ERR_SERVICE_UNAVAILABLE)
iq.addChild(node=err)
self._con.connection.send(iq)
def set_enabled(self, enabled):
if enabled and app.is_installed('IDLE'):
if not app.settings.get_account_setting(self._account,
'send_idle_time'):
return
raise nbxmpp.NodeProcessed
self._nbxmpp('LastActivity').set_idle_func(
idle.Monitor.get_idle_sec)
else:
self._nbxmpp('LastActivity').disable()
def get_instance(*args, **kwargs):
......
......@@ -16,8 +16,8 @@
from collections import namedtuple
from datetime import timedelta
import nbxmpp
from nbxmpp.errors import StanzaError
from nbxmpp.errors import MalformedStanzaError
from nbxmpp.namespaces import Namespace
from gi.repository import Gtk
from gi.repository import Gdk
......@@ -67,7 +67,9 @@ def __init__(self, account):
con = app.connections[account]
con.get_module('SoftwareVersion').request_software_version(
self.hostname, callback=self._software_version_received)
self.request_last_activity()
con.get_module('LastActivity').request_last_activity(
self.hostname, callback=self._on_last_activity)
server_info = con.get_module('Discovery').server_info
self._add_contact_addresses(server_info.dataforms)
......@@ -129,13 +131,6 @@ def _on_cert_button_clicked(self, _button):
transient_for=self,
cert=self.cert)
def request_last_activity(self):
if not app.account_is_connected(self.account):
return
con = app.connections[self.account]
iq = nbxmpp.Iq(to=self.hostname, typ='get', queryNS=Namespace.LAST)
con.connection.SendAndCallForResponse(iq, self._on_last_activity)
def _add_contact_addresses(self, dataforms):
fields = {
'admin-addresses': _('Admin'),
......@@ -209,28 +204,20 @@ def _on_activate_link(self, label, *args):
open_uri(label.get_text(), account=self.account)
return Gdk.EVENT_STOP
def _on_last_activity(self, _nbxmpp_client, stanza):
if self._destroyed:
# Window got closed in the meantime
return
if not nbxmpp.isResultNode(stanza):
log.warning('Received malformed result: %s', stanza)
return
if stanza.getQueryNS() != Namespace.LAST:
log.warning('Wrong namespace on result: %s', stanza)
return
def _on_last_activity(self, task):
try:
seconds = int(stanza.getQuery().getAttr('seconds'))
except (ValueError, TypeError, AttributeError):
log.exception('Received malformed last activity result')
else:
delta = timedelta(seconds=seconds)
hours = 0
if seconds >= 3600:
hours = delta.seconds // 3600
uptime = _('%(days)s days, %(hours)s hours') % {
'days': delta.days, 'hours': hours}
self._ui.server_uptime.set_text(uptime)
result = task.finish()
except (StanzaError, MalformedStanzaError) as error:
log.warning(error)
return
delta = timedelta(seconds=result.seconds)
hours = 0
if result.seconds >= 3600:
hours = delta.seconds // 3600
uptime = _('%(days)s days, %(hours)s hours') % {
'days': delta.days, 'hours': hours}
self._ui.server_uptime.set_text(uptime)
def _software_version_received(self, task):
try:
......
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