comparison sat/plugins/plugin_exp_invitation.py @ 2931:b256e90612d0

plugins invitation*, events: added an extra parameter and use it to keep a thumnail URL
author Goffi <goffi@goffi.org>
date Fri, 03 May 2019 13:05:01 +0200
parents 672e6be3290f
children 83cbd4545274
comparison
equal deleted inserted replaced
2930:32b6893240e0 2931:b256e90612d0
66 66
67 @param namespace(unicode): namespace handled 67 @param namespace(unicode): namespace handled
68 @param callback(callbable): method handling the invitation 68 @param callback(callbable): method handling the invitation
69 For pubsub invitation, it will be called with following arguments: 69 For pubsub invitation, it will be called with following arguments:
70 - client 70 - client
71 - name(unicode, None): name of the event
72 - extra(dict): extra data
71 - service(jid.JID): pubsub service jid 73 - service(jid.JID): pubsub service jid
72 - node(unicode): pubsub node 74 - node(unicode): pubsub node
73 - item_id(unicode, None): pubsub item id 75 - item_id(unicode, None): pubsub item id
74 - item_elt(domish.Element): item of the invitation 76 - item_elt(domish.Element): item of the invitation
75 For file sharing invitation, it will be called with following arguments: 77 For file sharing invitation, it will be called with following arguments:
76 - client 78 - client
79 - name(unicode, None): name of the repository
80 - extra(dict): extra data
77 - service(jid.JID): service jid of the file repository 81 - service(jid.JID): service jid of the file repository
78 - repos_type(unicode): type of the repository, can be: 82 - repos_type(unicode): type of the repository, can be:
79 - files: generic file sharing 83 - files: generic file sharing
80 - photos: photos album 84 - photos: photos album
81 - namespace(unicode, None): namespace of the repository 85 - namespace(unicode, None): namespace of the repository
82 - path(unicode, None): path of the repository 86 - path(unicode, None): path of the repository
83 - name(unicode, None): name of the repository
84 @raise exceptions.ConflictError: this namespace is already registered 87 @raise exceptions.ConflictError: this namespace is already registered
85 """ 88 """
86 if namespace in self._ns_cb: 89 if namespace in self._ns_cb:
87 raise exceptions.ConflictError( 90 raise exceptions.ConflictError(
88 u"invitation namespace {namespace} is already register with {callback}" 91 u"invitation namespace {namespace} is already register with {callback}"
89 .format(namespace=namespace, callback=self._ns_cb[namespace])) 92 .format(namespace=namespace, callback=self._ns_cb[namespace]))
90 self._ns_cb[namespace] = callback 93 self._ns_cb[namespace] = callback
91 94
92 def _generateBaseInvitation(self, client, invitee_jid): 95 def _generateBaseInvitation(self, client, invitee_jid, name, extra):
93 """Generate common mess_data end invitation_elt 96 """Generate common mess_data end invitation_elt
94 97
95 @param invitee_jid(jid.JID): entitee to send invitation to 98 @param invitee_jid(jid.JID): entitee to send invitation to
99 @param name(unicode, None): name of the shared repository
100 @param extra(dict, None): extra data, where key can be:
101 - thumb_url: URL of a thumbnail
96 @return (tuple[dict, domish.Element): mess_data and invitation_elt 102 @return (tuple[dict, domish.Element): mess_data and invitation_elt
97 """ 103 """
98 mess_data = { 104 mess_data = {
99 "from": client.jid, 105 "from": client.jid,
100 "to": invitee_jid, 106 "to": invitee_jid,
104 "subject": {}, 110 "subject": {},
105 "extra": {}, 111 "extra": {},
106 } 112 }
107 client.generateMessageXML(mess_data) 113 client.generateMessageXML(mess_data)
108 invitation_elt = mess_data["xml"].addElement("invitation", NS_INVITATION) 114 invitation_elt = mess_data["xml"].addElement("invitation", NS_INVITATION)
115 if name is not None:
116 invitation_elt[u"name"] = name
117 thumb_url = extra.get(u'thumb_url')
118 if thumb_url:
119 if not thumb_url.startswith(u'http'):
120 log.warning(
121 u"only http URLs are allowed for thumbnails, got {url}, ignoring"
122 .format(url=thumb_url))
123 else:
124 invitation_elt[u'thumb_url'] = thumb_url
109 return mess_data, invitation_elt 125 return mess_data, invitation_elt
110 126
111 def sendPubsubInvitation(self, client, invitee_jid, service, node, 127 def sendPubsubInvitation(self, client, invitee_jid, service, node,
112 item_id): 128 item_id, name, extra):
113 """Send an pubsub invitation in a <message> stanza 129 """Send an pubsub invitation in a <message> stanza
114 130
115 @param invitee_jid(jid.JID): entitee to send invitation to 131 @param invitee_jid(jid.JID): entitee to send invitation to
116 @param service(jid.JID): pubsub service 132 @param service(jid.JID): pubsub service
117 @param node(unicode): pubsub node 133 @param node(unicode): pubsub node
118 @param item_id(unicode): pubsub id 134 @param item_id(unicode): pubsub id
119 """ 135 @param name(unicode, None): see [_generateBaseInvitation]
120 mess_data, invitation_elt = self._generateBaseInvitation(client, invitee_jid) 136 @param extra(dict, None): see [_generateBaseInvitation]
137 """
138 if extra is None:
139 extra = {}
140 mess_data, invitation_elt = self._generateBaseInvitation(
141 client, invitee_jid, name, extra)
121 pubsub_elt = invitation_elt.addElement(u"pubsub") 142 pubsub_elt = invitation_elt.addElement(u"pubsub")
122 pubsub_elt[u"service"] = service.full() 143 pubsub_elt[u"service"] = service.full()
123 pubsub_elt[u"node"] = node 144 pubsub_elt[u"node"] = node
124 pubsub_elt[u"item"] = item_id 145 pubsub_elt[u"item"] = item_id
125 return client.send(mess_data[u"xml"]) 146 return client.send(mess_data[u"xml"])
126 147
127 def sendFileSharingInvitation(self, client, invitee_jid, service, repos_type=None, 148 def sendFileSharingInvitation(self, client, invitee_jid, service, repos_type=None,
128 namespace=None, path=None, name=None): 149 namespace=None, path=None, name=None, extra=None):
129 """Send a file sharing invitation in a <message> stanza 150 """Send a file sharing invitation in a <message> stanza
130 151
131 @param invitee_jid(jid.JID): entitee to send invitation to 152 @param invitee_jid(jid.JID): entitee to send invitation to
132 @param service(jid.JID): file sharing service 153 @param service(jid.JID): file sharing service
133 @param repos_type(unicode, None): type of files repository, can be: 154 @param repos_type(unicode, None): type of files repository, can be:
134 - None, "files": files sharing 155 - None, "files": files sharing
135 - "photos": photos album 156 - "photos": photos album
136 @param namespace(unicode, None): namespace of the shared repository 157 @param namespace(unicode, None): namespace of the shared repository
137 @param path(unicode, None): path of the shared repository 158 @param path(unicode, None): path of the shared repository
138 @param name(unicode, None): name of the shared repository 159 @param name(unicode, None): see [_generateBaseInvitation]
139 """ 160 @param extra(dict, None): see [_generateBaseInvitation]
140 mess_data, invitation_elt = self._generateBaseInvitation(client, invitee_jid) 161 """
162 if extra is None:
163 extra = {}
164 mess_data, invitation_elt = self._generateBaseInvitation(
165 client, invitee_jid, name, extra)
141 file_sharing_elt = invitation_elt.addElement(u"file_sharing") 166 file_sharing_elt = invitation_elt.addElement(u"file_sharing")
142 file_sharing_elt[u"service"] = service.full() 167 file_sharing_elt[u"service"] = service.full()
143 if repos_type is not None: 168 if repos_type is not None:
144 if repos_type not in (u"files", "photos"): 169 if repos_type not in (u"files", "photos"):
145 msg = u"unknown repository type: {repos_type}".format(repos_type=repos_type) 170 msg = u"unknown repository type: {repos_type}".format(
171 repos_type=repos_type)
146 log.warning(msg) 172 log.warning(msg)
147 raise exceptions.DateError(msg) 173 raise exceptions.DateError(msg)
148 file_sharing_elt[u"type"] = repos_type 174 file_sharing_elt[u"type"] = repos_type
149 if namespace is not None: 175 if namespace is not None:
150 file_sharing_elt[u"namespace"] = namespace 176 file_sharing_elt[u"namespace"] = namespace
151 if path is not None: 177 if path is not None:
152 file_sharing_elt[u"path"] = path 178 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"]) 179 return client.send(mess_data[u"xml"])
156 180
157 @defer.inlineCallbacks 181 @defer.inlineCallbacks
158 def _parsePubsubElt(self, client, pubsub_elt): 182 def _parsePubsubElt(self, client, pubsub_elt):
159 try: 183 try:
193 log.warning(_(u"Bad invitation, ignoring")) 217 log.warning(_(u"Bad invitation, ignoring"))
194 raise exceptions.DataError 218 raise exceptions.DataError
195 repos_type = file_sharing_elt.getAttribute(u"type", u"files") 219 repos_type = file_sharing_elt.getAttribute(u"type", u"files")
196 namespace = file_sharing_elt.getAttribute(u"namespace") 220 namespace = file_sharing_elt.getAttribute(u"namespace")
197 path = file_sharing_elt.getAttribute(u"path") 221 path = file_sharing_elt.getAttribute(u"path")
198 name = file_sharing_elt.getAttribute(u"name") 222 args = [service, repos_type, namespace, path]
199 args = [service, repos_type, namespace, path, name]
200 ns_fis = self.host.getNamespace(u"fis") 223 ns_fis = self.host.getNamespace(u"fis")
201 return ns_fis, args 224 return ns_fis, args
202 225
203 @defer.inlineCallbacks 226 @defer.inlineCallbacks
204 def onInvitation(self, message_elt, client): 227 def onInvitation(self, message_elt, client):
205 log.debug(u"invitation received [{profile}]".format(profile=client.profile)) 228 log.debug(u"invitation received [{profile}]".format(profile=client.profile))
206 invitation_elt = message_elt.invitation 229 invitation_elt = message_elt.invitation
230
231 name = invitation_elt.getAttribute(u"name")
232 extra = {}
233 if invitation_elt.hasAttribute(u"thumb_url"):
234 extra[u'thumb_url'] = invitation_elt[u'thumb_url']
235
207 for elt in invitation_elt.elements(): 236 for elt in invitation_elt.elements():
208 if elt.uri != NS_INVITATION: 237 if elt.uri != NS_INVITATION:
209 log.warning(u"unexpected element: {xml}".format(xml=elt.toXml())) 238 log.warning(u"unexpected element: {xml}".format(xml=elt.toXml()))
210 continue 239 continue
211 if elt.name == u"pubsub": 240 if elt.name == u"pubsub":
224 continue 253 continue
225 254
226 try: 255 try:
227 cb = self._ns_cb[namespace] 256 cb = self._ns_cb[namespace]
228 except KeyError: 257 except KeyError:
229 log.warning(_(u'No handler for namespace "{namespace}", invitation ignored') 258 log.warning(_(
259 u'No handler for namespace "{namespace}", invitation ignored')
230 .format(namespace=namespace)) 260 .format(namespace=namespace))
231 else: 261 else:
232 cb(client, *args) 262 cb(client, name, extra, *args)
233 263
234 264
235 class PubsubInvitationHandler(XMPPHandler): 265 class PubsubInvitationHandler(XMPPHandler):
236 implements(iwokkel.IDisco) 266 implements(iwokkel.IDisco)
237 267