changeset 3335:83bc9d46a417

plugin email invitation: fixed create/simpleCreate + invitee_name: - fixed invitationSimpleCreate signature - fixed nicknames settings in create - use async coroutines - guest jid is now stored (as a string), avoiding the need to check profile to retrieve it - invitee_name is now needed in invitationSimpleCreate
author Goffi <goffi@goffi.org>
date Thu, 13 Aug 2020 23:46:18 +0200
parents 2cd54c72fae4
children e38ddaf477bd
files sat/plugins/plugin_misc_email_invitation.py
diffstat 1 files changed, 31 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_misc_email_invitation.py	Thu Aug 13 23:46:18 2020 +0200
+++ b/sat/plugins/plugin_misc_email_invitation.py	Thu Aug 13 23:46:18 2020 +0200
@@ -33,7 +33,7 @@
 
 
 PLUGIN_INFO = {
-    C.PI_NAME: "Invitations",
+    C.PI_NAME: "Email Invitations",
     C.PI_IMPORT_NAME: "EMAIL_INVITATION",
     C.PI_TYPE: C.PLUG_TYPE_MISC,
     C.PI_DEPENDENCIES: ['XEP-0077'],
@@ -90,7 +90,7 @@
                               out_sign='a{sa{ss}}',
                               method=self._list,
                               async_=True)
-        host.bridge.addMethod("invitationSimpleCreate", ".plugin", in_sign='',
+        host.bridge.addMethod("invitationSimpleCreate", ".plugin", in_sign='sssss',
                               out_sign='a{ss}',
                               method=self._simpleCreate,
                               async_=True)
@@ -125,15 +125,9 @@
             value = locals()[key]
             if value:
                 kwargs[key] = str(value)
-        d = self.create(**kwargs)
-        def serialize(data):
-            data[KEY_JID] = data[KEY_JID].full()
-            return data
-        d.addCallback(serialize)
-        return d
+        return defer.ensureDeferred(self.create(**kwargs))
 
-    @defer.inlineCallbacks
-    def create(self, **kwargs):
+    async def create(self, **kwargs):
         r"""Create an invitation
 
         This will create an XMPP account and a profile, and use a UUID to retrieve them.
@@ -244,7 +238,7 @@
             # we don't register account if there is no user as anonymous login is then
             # used
             try:
-                yield self.host.plugins['XEP-0077'].registerNewAccount(jid_, password)
+                await self.host.plugins['XEP-0077'].registerNewAccount(jid_, password)
             except error.StanzaError as e:
                 prefix = jid_.user
                 idx = 0
@@ -255,7 +249,7 @@
                     log.info(_("requested jid already exists, trying with {}".format(
                         jid_.full())))
                     try:
-                        yield self.host.plugins['XEP-0077'].registerNewAccount(jid_,
+                        await self.host.plugins['XEP-0077'].registerNewAccount(jid_,
                                                                                password)
                     except error.StanzaError:
                         idx += 1
@@ -270,11 +264,11 @@
 
         extra[KEY_GUEST_PROFILE] = guest_profile = INVITEE_PROFILE_TPL.format(uuid=id_)
         # profile creation should not fail as we generate unique name ourselves
-        yield self.host.memory.createProfile(guest_profile, password)
-        yield self.host.memory.startSession(password, guest_profile)
-        yield self.host.memory.setParam("JabberID", jid_.full(), "Connection",
+        await self.host.memory.createProfile(guest_profile, password)
+        await self.host.memory.startSession(password, guest_profile)
+        await self.host.memory.setParam("JabberID", jid_.full(), "Connection",
                                         profile_key=guest_profile)
-        yield self.host.memory.setParam("Password", password, "Connection",
+        await self.host.memory.setParam("Password", password, "Connection",
                                         profile_key=guest_profile)
         name = kwargs.pop('name', None)
         if name is not None:
@@ -284,10 +278,10 @@
             except KeyError:
                 pass
             else:
-                yield defer.ensureDeferred(self.host.connect(guest_profile, password))
+                await self.host.connect(guest_profile, password)
                 guest_client = self.host.getClient(guest_profile)
-                yield id_plugin.setIdentity(guest_client, {'nick': name})
-                yield self.host.disconnect(guest_profile)
+                await id_plugin.setIdentity(guest_client, {'nicknames': [name]})
+                await self.host.disconnect(guest_profile)
 
         ## email
         language = kwargs.pop('language', None)
@@ -323,7 +317,7 @@
             invite_url = url_template.format(**format_args)
             format_args['url'] = invite_url
 
-            yield sat_email.sendEmail(
+            await sat_email.sendEmail(
                 self.host.memory.config,
                 [email] + emails_extra,
                 (kwargs.pop('message_subject', None) or DEFAULT_SUBJECT).format(
@@ -331,31 +325,37 @@
                 (kwargs.pop('message_body', None) or DEFAULT_BODY).format(**format_args),
             )
 
-        ## extra data saving
-        self.invitations[id_] = extra
 
         if kwargs:
             log.warning(_("Not all arguments have been consumed: {}").format(kwargs))
 
+        extra[KEY_JID] = jid_.full()
+
+        ## extra data saving
+        self.invitations[id_] = extra
+
         extra[KEY_ID] = id_
-        extra[KEY_JID] = jid_
-        defer.returnValue(extra)
 
-    def _simpleCreate(self, invitee_email, url_template, extra_s, profile):
+        return extra
+
+    def _simpleCreate(self, invitee_email, invitee_name, url_template, extra_s, profile):
         client = self.host.getClient(profile)
         # FIXME: needed because python-dbus use a specific string class
         invitee_email = str(invitee_email)
+        invitee_name = str(invitee_name)
         url_template = str(url_template)
         extra = data_format.deserialise(extra_s)
         d = defer.ensureDeferred(
-            self.simpleCreate(client, invitee_email, url_template, extra)
+            self.simpleCreate(client, invitee_email, invitee_name, url_template, extra)
         )
         d.addCallback(lambda data: {k: str(v) for k,v in data.items()})
         return d
 
-    async def simpleCreate(self, client, invitee_email, url_template, extra):
+    async def simpleCreate(
+        self, client, invitee_email, invitee_name, url_template, extra):
         """Simplified method to invite somebody by email"""
         return await self.create(
+            name=invitee_name,
             email=invitee_email,
             url_template=url_template,
             profile=client.profile,
@@ -414,19 +414,18 @@
         return d
 
     def _list(self, profile=C.PROF_KEY_NONE):
-        return self.list(profile)
+        return defer.ensureDeferred(self.list(profile))
 
-    @defer.inlineCallbacks
-    def list(self, profile=C.PROF_KEY_NONE):
+    async def list(self, profile=C.PROF_KEY_NONE):
         """List invitations
 
         @param profile(unicode): return invitation linked to this profile only
             C.PROF_KEY_NONE: don't filter invitations
         @return list(unicode): invitations uids
         """
-        invitations = yield self.invitations.all()
+        invitations = await self.invitations.all()
         if profile != C.PROF_KEY_NONE:
             invitations = {id_:data for id_, data in invitations.items()
                            if data.get('profile') == profile}
 
-        defer.returnValue(invitations)
+        return invitations