XEP-0012: API changes.

This commit is contained in:
mathieui 2021-02-14 11:45:03 +01:00
parent d51c4e307d
commit 0b6326e1cc
3 changed files with 75 additions and 17 deletions

View file

@ -9,6 +9,42 @@ XEP-0012: Last Activity
:exclude-members: session_bind, plugin_init, plugin_end :exclude-members: session_bind, plugin_init, plugin_end
Internal API methods
--------------------
This plugin uses an in-memory storage by default to keep track of the
received and sent last activities.
.. glossary::
get_last_activity
- **jid**: :class:`~.JID` of whom to retrieve the last activity
- **node**: unused
- **ifrom**: who the request is from (None = local)
- **args**: ``None`` or an :class:`~.Iq` that is requesting the
- **returns**
information.
Get the last activity of a JID from the storage.
set_last_activity
- **jid**: :class:`~.JID` of whom to set the last activity
- **node**: unused
- **ifrom**: unused
- **args**: A dict containing ``'seconds'`` and ``'status'``
``{'seconds': Optional[int], 'status': Optional[str]}``
Set the last activity of a JID in the storage.
del_last_activity
- **jid**: :class:`~.JID` to delete from the storage
- **node**: unused
- **ifrom**: unused
- **args**: unused
Remove the last activity of a JID from the storage.
Stanza elements Stanza elements
--------------- ---------------

View file

@ -18,7 +18,7 @@ class TestLastActivity(SlixIntegration):
async def test_activity(self): async def test_activity(self):
"""Check we can set and get last activity""" """Check we can set and get last activity"""
self.clients[0]['xep_0012'].set_last_activity( await self.clients[0]['xep_0012'].set_last_activity(
status='coucou', status='coucou',
seconds=4242, seconds=4242,
) )

View file

@ -16,7 +16,7 @@ from slixmpp import future_wrapper, JID
from slixmpp.stanza import Iq from slixmpp.stanza import Iq
from slixmpp.exceptions import XMPPError from slixmpp.exceptions import XMPPError
from slixmpp.xmlstream import JID, register_stanza_plugin from slixmpp.xmlstream import JID, register_stanza_plugin
from slixmpp.xmlstream.handler import Callback from slixmpp.xmlstream.handler import CoroutineCallback
from slixmpp.xmlstream.matcher import StanzaPath from slixmpp.xmlstream.matcher import StanzaPath
from slixmpp.plugins.xep_0012 import stanza, LastActivity from slixmpp.plugins.xep_0012 import stanza, LastActivity
@ -41,7 +41,7 @@ class XEP_0012(BasePlugin):
self._last_activities = {} self._last_activities = {}
self.xmpp.register_handler( self.xmpp.register_handler(
Callback('Last Activity', CoroutineCallback('Last Activity',
StanzaPath('iq@type=get/last_activity'), StanzaPath('iq@type=get/last_activity'),
self._handle_get_last_activity)) self._handle_get_last_activity))
@ -62,28 +62,50 @@ class XEP_0012(BasePlugin):
def session_bind(self, jid): def session_bind(self, jid):
self.xmpp['xep_0030'].add_feature('jabber:iq:last') self.xmpp['xep_0030'].add_feature('jabber:iq:last')
def begin_idle(self, jid: Optional[JID] = None, status: str = None): def begin_idle(self, jid: Optional[JID] = None, status: Optional[str] = None) -> Future:
"""Reset the last activity for the given JID. """Reset the last activity for the given JID.
.. versionchanged:: 1.8.0
This function now returns a Future.
:param status: Optional status. :param status: Optional status.
""" """
self.set_last_activity(jid, 0, status) return self.set_last_activity(jid, 0, status)
def end_idle(self, jid=None): def end_idle(self, jid: Optional[JID] = None) -> Future:
self.del_last_activity(jid) """Remove the last activity of a JID.
def start_uptime(self, status=None): .. versionchanged:: 1.8.0
self.set_last_activity(None, 0, status) This function now returns a Future.
"""
return self.del_last_activity(jid)
def set_last_activity(self, jid=None, seconds=None, status=None): def start_uptime(self, status: Optional[str] = None) -> Future:
self.api['set_last_activity'](jid, args={ """
.. versionchanged:: 1.8.0
This function now returns a Future.
"""
return self.set_last_activity(None, 0, status)
def set_last_activity(self, jid=None, seconds=None, status=None) -> Future:
"""Set last activity for a JID.
.. versionchanged:: 1.8.0
This function now returns a Future.
"""
return self.api['set_last_activity'](jid, args={
'seconds': seconds, 'seconds': seconds,
'status': status}) 'status': status
})
def del_last_activity(self, jid): def del_last_activity(self, jid: JID) -> Future:
self.api['del_last_activity'](jid) """Remove the last activity of a JID.
.. versionchanged:: 1.8.0
This function now returns a Future.
"""
return self.api['del_last_activity'](jid)
@future_wrapper
def get_last_activity(self, jid: JID, local: bool = False, def get_last_activity(self, jid: JID, local: bool = False,
ifrom: Optional[JID] = None, **iqkwargs) -> Future: ifrom: Optional[JID] = None, **iqkwargs) -> Future:
"""Get last activity for a specific JID. """Get last activity for a specific JID.
@ -109,10 +131,10 @@ class XEP_0012(BasePlugin):
iq.enable('last_activity') iq.enable('last_activity')
return iq.send(**iqkwargs) return iq.send(**iqkwargs)
def _handle_get_last_activity(self, iq: Iq): async def _handle_get_last_activity(self, iq: Iq):
log.debug("Received last activity query from " + \ log.debug("Received last activity query from " + \
"<%s> to <%s>.", iq['from'], iq['to']) "<%s> to <%s>.", iq['from'], iq['to'])
reply = self.api['get_last_activity'](iq['to'], None, iq['from'], iq) reply = await self.api['get_last_activity'](iq['to'], None, iq['from'], iq)
reply.send() reply.send()
# ================================================================= # =================================================================