Mercurial > libervia-backend
diff sat/plugins/plugin_exp_invitation.py @ 2913:672e6be3290f
plugins sharing invitation, invitation, list of interest: handle invitation to a file sharing repository
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 14 Apr 2019 08:21:51 +0200 |
parents | a3faf1c86596 |
children | b256e90612d0 |
line wrap: on
line diff
--- a/sat/plugins/plugin_exp_invitation.py Sun Apr 14 08:21:51 2019 +0200 +++ b/sat/plugins/plugin_exp_invitation.py Sun Apr 14 08:21:51 2019 +0200 @@ -35,7 +35,7 @@ C.PI_IMPORT_NAME: "INVITATION", C.PI_TYPE: "EXP", C.PI_PROTOCOLS: [], - C.PI_DEPENDENCIES: ["XEP-0060"], + C.PI_DEPENDENCIES: [u"XEP-0060", u"XEP-0329"], C.PI_RECOMMENDATIONS: [], C.PI_MAIN: "Invitation", C.PI_HANDLER: "yes", @@ -72,6 +72,15 @@ - node(unicode): pubsub node - item_id(unicode, None): pubsub item id - item_elt(domish.Element): item of the invitation + For file sharing invitation, it will be called with following arguments: + - client + - service(jid.JID): service jid of the file repository + - repos_type(unicode): type of the repository, can be: + - files: generic file sharing + - photos: photos album + - namespace(unicode, None): namespace of the repository + - path(unicode, None): path of the repository + - name(unicode, None): name of the repository @raise exceptions.ConflictError: this namespace is already registered """ if namespace in self._ns_cb: @@ -80,14 +89,11 @@ .format(namespace=namespace, callback=self._ns_cb[namespace])) self._ns_cb[namespace] = callback - def sendPubsubInvitation(self, client, invitee_jid, service, node, - item_id): - """Send an invitation in a <message> stanza + def _generateBaseInvitation(self, client, invitee_jid): + """Generate common mess_data end invitation_elt @param invitee_jid(jid.JID): entitee to send invitation to - @param service(jid.JID): pubsub service - @param node(unicode): pubsub node - @param item_id(unicode): pubsub id + @return (tuple[dict, domish.Element): mess_data and invitation_elt """ mess_data = { "from": client.jid, @@ -100,11 +106,53 @@ } client.generateMessageXML(mess_data) invitation_elt = mess_data["xml"].addElement("invitation", NS_INVITATION) + return mess_data, invitation_elt + + def sendPubsubInvitation(self, client, invitee_jid, service, node, + item_id): + """Send an pubsub invitation in a <message> stanza + + @param invitee_jid(jid.JID): entitee to send invitation to + @param service(jid.JID): pubsub service + @param node(unicode): pubsub node + @param item_id(unicode): pubsub id + """ + mess_data, invitation_elt = self._generateBaseInvitation(client, invitee_jid) pubsub_elt = invitation_elt.addElement(u"pubsub") pubsub_elt[u"service"] = service.full() pubsub_elt[u"node"] = node pubsub_elt[u"item"] = item_id - client.send(mess_data[u"xml"]) + return client.send(mess_data[u"xml"]) + + def sendFileSharingInvitation(self, client, invitee_jid, service, repos_type=None, + namespace=None, path=None, name=None): + """Send a file sharing invitation in a <message> stanza + + @param invitee_jid(jid.JID): entitee to send invitation to + @param service(jid.JID): file sharing service + @param repos_type(unicode, None): type of files repository, can be: + - None, "files": files sharing + - "photos": photos album + @param namespace(unicode, None): namespace of the shared repository + @param path(unicode, None): path of the shared repository + @param name(unicode, None): name of the shared repository + """ + mess_data, invitation_elt = self._generateBaseInvitation(client, invitee_jid) + file_sharing_elt = invitation_elt.addElement(u"file_sharing") + file_sharing_elt[u"service"] = service.full() + if repos_type is not None: + if repos_type not in (u"files", "photos"): + msg = u"unknown repository type: {repos_type}".format(repos_type=repos_type) + log.warning(msg) + raise exceptions.DateError(msg) + file_sharing_elt[u"type"] = repos_type + if namespace is not None: + file_sharing_elt[u"namespace"] = namespace + if path is not None: + file_sharing_elt[u"path"] = path + if name is not None: + file_sharing_elt[u"name"] = name + return client.send(mess_data[u"xml"]) @defer.inlineCallbacks def _parsePubsubElt(self, client, pubsub_elt): @@ -138,8 +186,23 @@ args = [service, node, item_id, item_elt] defer.returnValue((namespace, args)) + def _parseFileSharingElt(self, client, file_sharing_elt): + try: + service = jid.JID(file_sharing_elt["service"]) + except (RuntimeError, KeyError): + log.warning(_(u"Bad invitation, ignoring")) + raise exceptions.DataError + repos_type = file_sharing_elt.getAttribute(u"type", u"files") + namespace = file_sharing_elt.getAttribute(u"namespace") + path = file_sharing_elt.getAttribute(u"path") + name = file_sharing_elt.getAttribute(u"name") + args = [service, repos_type, namespace, path, name] + ns_fis = self.host.getNamespace(u"fis") + return ns_fis, args + @defer.inlineCallbacks def onInvitation(self, message_elt, client): + log.debug(u"invitation received [{profile}]".format(profile=client.profile)) invitation_elt = message_elt.invitation for elt in invitation_elt.elements(): if elt.uri != NS_INVITATION: @@ -147,6 +210,8 @@ continue if elt.name == u"pubsub": method = self._parsePubsubElt + elif elt.name == u"file_sharing": + method = self._parseFileSharingElt else: log.warning(u"not implemented invitation element: {xml}".format( xml = elt.toXml()))