Mercurial > libervia-backend
comparison 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 |
comparison
equal
deleted
inserted
replaced
2912:a3faf1c86596 | 2913:672e6be3290f |
---|---|
33 PLUGIN_INFO = { | 33 PLUGIN_INFO = { |
34 C.PI_NAME: "Invitation", | 34 C.PI_NAME: "Invitation", |
35 C.PI_IMPORT_NAME: "INVITATION", | 35 C.PI_IMPORT_NAME: "INVITATION", |
36 C.PI_TYPE: "EXP", | 36 C.PI_TYPE: "EXP", |
37 C.PI_PROTOCOLS: [], | 37 C.PI_PROTOCOLS: [], |
38 C.PI_DEPENDENCIES: ["XEP-0060"], | 38 C.PI_DEPENDENCIES: [u"XEP-0060", u"XEP-0329"], |
39 C.PI_RECOMMENDATIONS: [], | 39 C.PI_RECOMMENDATIONS: [], |
40 C.PI_MAIN: "Invitation", | 40 C.PI_MAIN: "Invitation", |
41 C.PI_HANDLER: "yes", | 41 C.PI_HANDLER: "yes", |
42 C.PI_DESCRIPTION: _(u"Experimental handling of invitations"), | 42 C.PI_DESCRIPTION: _(u"Experimental handling of invitations"), |
43 } | 43 } |
70 - client | 70 - client |
71 - service(jid.JID): pubsub service jid | 71 - service(jid.JID): pubsub service jid |
72 - node(unicode): pubsub node | 72 - node(unicode): pubsub node |
73 - item_id(unicode, None): pubsub item id | 73 - item_id(unicode, None): pubsub item id |
74 - item_elt(domish.Element): item of the invitation | 74 - item_elt(domish.Element): item of the invitation |
75 For file sharing invitation, it will be called with following arguments: | |
76 - client | |
77 - service(jid.JID): service jid of the file repository | |
78 - repos_type(unicode): type of the repository, can be: | |
79 - files: generic file sharing | |
80 - photos: photos album | |
81 - namespace(unicode, None): namespace of the repository | |
82 - path(unicode, None): path of the repository | |
83 - name(unicode, None): name of the repository | |
75 @raise exceptions.ConflictError: this namespace is already registered | 84 @raise exceptions.ConflictError: this namespace is already registered |
76 """ | 85 """ |
77 if namespace in self._ns_cb: | 86 if namespace in self._ns_cb: |
78 raise exceptions.ConflictError( | 87 raise exceptions.ConflictError( |
79 u"invitation namespace {namespace} is already register with {callback}" | 88 u"invitation namespace {namespace} is already register with {callback}" |
80 .format(namespace=namespace, callback=self._ns_cb[namespace])) | 89 .format(namespace=namespace, callback=self._ns_cb[namespace])) |
81 self._ns_cb[namespace] = callback | 90 self._ns_cb[namespace] = callback |
82 | 91 |
83 def sendPubsubInvitation(self, client, invitee_jid, service, node, | 92 def _generateBaseInvitation(self, client, invitee_jid): |
84 item_id): | 93 """Generate common mess_data end invitation_elt |
85 """Send an invitation in a <message> stanza | |
86 | 94 |
87 @param invitee_jid(jid.JID): entitee to send invitation to | 95 @param invitee_jid(jid.JID): entitee to send invitation to |
88 @param service(jid.JID): pubsub service | 96 @return (tuple[dict, domish.Element): mess_data and invitation_elt |
89 @param node(unicode): pubsub node | |
90 @param item_id(unicode): pubsub id | |
91 """ | 97 """ |
92 mess_data = { | 98 mess_data = { |
93 "from": client.jid, | 99 "from": client.jid, |
94 "to": invitee_jid, | 100 "to": invitee_jid, |
95 "uid": "", | 101 "uid": "", |
98 "subject": {}, | 104 "subject": {}, |
99 "extra": {}, | 105 "extra": {}, |
100 } | 106 } |
101 client.generateMessageXML(mess_data) | 107 client.generateMessageXML(mess_data) |
102 invitation_elt = mess_data["xml"].addElement("invitation", NS_INVITATION) | 108 invitation_elt = mess_data["xml"].addElement("invitation", NS_INVITATION) |
109 return mess_data, invitation_elt | |
110 | |
111 def sendPubsubInvitation(self, client, invitee_jid, service, node, | |
112 item_id): | |
113 """Send an pubsub invitation in a <message> stanza | |
114 | |
115 @param invitee_jid(jid.JID): entitee to send invitation to | |
116 @param service(jid.JID): pubsub service | |
117 @param node(unicode): pubsub node | |
118 @param item_id(unicode): pubsub id | |
119 """ | |
120 mess_data, invitation_elt = self._generateBaseInvitation(client, invitee_jid) | |
103 pubsub_elt = invitation_elt.addElement(u"pubsub") | 121 pubsub_elt = invitation_elt.addElement(u"pubsub") |
104 pubsub_elt[u"service"] = service.full() | 122 pubsub_elt[u"service"] = service.full() |
105 pubsub_elt[u"node"] = node | 123 pubsub_elt[u"node"] = node |
106 pubsub_elt[u"item"] = item_id | 124 pubsub_elt[u"item"] = item_id |
107 client.send(mess_data[u"xml"]) | 125 return client.send(mess_data[u"xml"]) |
126 | |
127 def sendFileSharingInvitation(self, client, invitee_jid, service, repos_type=None, | |
128 namespace=None, path=None, name=None): | |
129 """Send a file sharing invitation in a <message> stanza | |
130 | |
131 @param invitee_jid(jid.JID): entitee to send invitation to | |
132 @param service(jid.JID): file sharing service | |
133 @param repos_type(unicode, None): type of files repository, can be: | |
134 - None, "files": files sharing | |
135 - "photos": photos album | |
136 @param namespace(unicode, None): namespace of the shared repository | |
137 @param path(unicode, None): path of the shared repository | |
138 @param name(unicode, None): name of the shared repository | |
139 """ | |
140 mess_data, invitation_elt = self._generateBaseInvitation(client, invitee_jid) | |
141 file_sharing_elt = invitation_elt.addElement(u"file_sharing") | |
142 file_sharing_elt[u"service"] = service.full() | |
143 if repos_type is not None: | |
144 if repos_type not in (u"files", "photos"): | |
145 msg = u"unknown repository type: {repos_type}".format(repos_type=repos_type) | |
146 log.warning(msg) | |
147 raise exceptions.DateError(msg) | |
148 file_sharing_elt[u"type"] = repos_type | |
149 if namespace is not None: | |
150 file_sharing_elt[u"namespace"] = namespace | |
151 if path is not None: | |
152 file_sharing_elt[u"path"] = path | |
153 if name is not None: | |
154 file_sharing_elt[u"name"] = name | |
155 return client.send(mess_data[u"xml"]) | |
108 | 156 |
109 @defer.inlineCallbacks | 157 @defer.inlineCallbacks |
110 def _parsePubsubElt(self, client, pubsub_elt): | 158 def _parsePubsubElt(self, client, pubsub_elt): |
111 try: | 159 try: |
112 service = jid.JID(pubsub_elt["service"]) | 160 service = jid.JID(pubsub_elt["service"]) |
136 raise exceptions.DataError | 184 raise exceptions.DataError |
137 | 185 |
138 args = [service, node, item_id, item_elt] | 186 args = [service, node, item_id, item_elt] |
139 defer.returnValue((namespace, args)) | 187 defer.returnValue((namespace, args)) |
140 | 188 |
189 def _parseFileSharingElt(self, client, file_sharing_elt): | |
190 try: | |
191 service = jid.JID(file_sharing_elt["service"]) | |
192 except (RuntimeError, KeyError): | |
193 log.warning(_(u"Bad invitation, ignoring")) | |
194 raise exceptions.DataError | |
195 repos_type = file_sharing_elt.getAttribute(u"type", u"files") | |
196 namespace = file_sharing_elt.getAttribute(u"namespace") | |
197 path = file_sharing_elt.getAttribute(u"path") | |
198 name = file_sharing_elt.getAttribute(u"name") | |
199 args = [service, repos_type, namespace, path, name] | |
200 ns_fis = self.host.getNamespace(u"fis") | |
201 return ns_fis, args | |
202 | |
141 @defer.inlineCallbacks | 203 @defer.inlineCallbacks |
142 def onInvitation(self, message_elt, client): | 204 def onInvitation(self, message_elt, client): |
205 log.debug(u"invitation received [{profile}]".format(profile=client.profile)) | |
143 invitation_elt = message_elt.invitation | 206 invitation_elt = message_elt.invitation |
144 for elt in invitation_elt.elements(): | 207 for elt in invitation_elt.elements(): |
145 if elt.uri != NS_INVITATION: | 208 if elt.uri != NS_INVITATION: |
146 log.warning(u"unexpected element: {xml}".format(xml=elt.toXml())) | 209 log.warning(u"unexpected element: {xml}".format(xml=elt.toXml())) |
147 continue | 210 continue |
148 if elt.name == u"pubsub": | 211 if elt.name == u"pubsub": |
149 method = self._parsePubsubElt | 212 method = self._parsePubsubElt |
213 elif elt.name == u"file_sharing": | |
214 method = self._parseFileSharingElt | |
150 else: | 215 else: |
151 log.warning(u"not implemented invitation element: {xml}".format( | 216 log.warning(u"not implemented invitation element: {xml}".format( |
152 xml = elt.toXml())) | 217 xml = elt.toXml())) |
153 continue | 218 continue |
154 try: | 219 try: |