plugin list of interest: expose interestsRegisterFileSharing to bridge + use coroutines

This commit is contained in:
Goffi 2020-06-09 06:25:20 +02:00
commit 9f9a6a5a2b
2 changed files with 33 additions and 8 deletions

View file

@ -21,6 +21,7 @@ from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
from sat.tools.common import data_format
from twisted.internet import defer
from twisted.words.protocols.jabber import jid
log = getLogger(__name__)
@ -70,7 +71,7 @@ class FileSharingInvitation(object):
if repos_type == "files":
type_human = _("file sharing")
elif repos_type == "photos":
type_human = _("photos album")
type_human = _("photo album")
else:
log.warning("Unknown repository type: {repos_type}".format(
repos_type=repos_type))
@ -81,5 +82,8 @@ class FileSharingInvitation(object):
'with namespace "{namespace}" at path [{path}]').format(
profile=client.profile, type_human=type_human, namespace=namespace, path=path)
)
return self.host.plugins['LIST_INTEREST'].registerFileSharing(
client, service, repos_type, namespace, path, name, extra)
return defer.ensureDeferred(
self.host.plugins['LIST_INTEREST'].registerFileSharing(
client, service, repos_type, namespace, path, name, extra
)
)

View file

@ -20,6 +20,7 @@
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.core.log import getLogger
from sat.tools.common import data_format
from wokkel import disco, iwokkel, pubsub
from zope.interface import implementer
from twisted.internet import defer
@ -60,6 +61,14 @@ class ListInterest(object):
method=self._listInterests,
async_=True,
)
host.bridge.addMethod(
"interestsRegisterFileSharing",
".plugin",
in_sign="sssssss",
out_sign="",
method=self._registerFileSharing,
async_=True,
)
def getHandler(self, client):
return ListInterestHandler(self)
@ -122,8 +131,20 @@ class ListInterest(object):
client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt]
)
@defer.inlineCallbacks
def registerFileSharing(
def _registerFileSharing(
self, service, repos_type, namespace, path, name, extra_raw,
profile
):
client = self.host.getClient(profile)
extra = data_format.deserialise(extra_raw)
return defer.ensureDeferred(self.registerFileSharing(
client, jid.JID(service), repos_type or None, namespace or None, path or None,
name or None, extra
))
async def registerFileSharing(
self, client, service, repos_type=None, namespace=None, path=None, name=None,
extra=None):
"""Register an interesting file repository in personal list
@ -133,11 +154,11 @@ class ListInterest(object):
@param namespace(unicode, None): namespace of the repository
@param path(unicode, None): path of the repository
@param name(unicode, None): name of the repository
@param extra(dict, None): as ad for [registerPubsub]
@param extra(dict, None): same as [registerPubsub]
"""
if extra is None:
extra = {}
yield self.createNode(client)
await self.createNode(client)
interest_elt = domish.Element((NS_LIST_INTEREST, "interest"))
interest_elt["namespace"] = self.host.getNamespace("fis")
if name is not None:
@ -154,7 +175,7 @@ class ListInterest(object):
if path is not None:
file_sharing_elt["path"] = path
item_elt = pubsub.Item(payload=interest_elt)
yield self._p.publish(
await self._p.publish(
client, client.jid.userhostJID(), NS_LIST_INTEREST, items=[item_elt]
)