comparison sat/plugins/plugin_xep_0313.py @ 3308:384283adcce1

plugins XEP-0059, XEP-0060, XEP-0277, XEP-0313: better serialisation: `data_format.serialise` is now used for `mbGet`, and RSM/MAM values are not transtyped to strings anymore. A serialised dict is now used, items are put in the `items` key. Comments handling has been refactored to use a list for the potentially multiple comments nodes. `rsm` data are now in a `rsm` key of the dict, and `mam` data are merged with other metadata.
author Goffi <goffi@goffi.org>
date Thu, 16 Jul 2020 09:07:20 +0200
parents 559a625a236b
children 569f4cf7183b
comparison
equal deleted inserted replaced
3307:9f0e28137cd0 3308:384283adcce1
213 order_by = extra.pop("order_by") 213 order_by = extra.pop("order_by")
214 assert isinstance(order_by, list) 214 assert isinstance(order_by, list)
215 mam_args["orderBy"] = order_by 215 mam_args["orderBy"] = order_by
216 216
217 return mam.MAMRequest(**mam_args) if mam_args else None 217 return mam.MAMRequest(**mam_args) if mam_args else None
218
219 def serialise(self, mam_response, data=None):
220 """Serialise data for MAM
221
222 Key set in data can be:
223 - mam_complete: a bool const indicating if all items have been received
224 - mam_stable: a bool const which is False if items order may be changed
225 All values are set as strings.
226 @param mam_response(dict): response data to serialise
227 @param data(dict, None): dict to update with mam_* data.
228 If None, a new dict is created
229 @return (dict): data dict
230 """
231 if data is None:
232 data = {}
233 data["mam_complete"] = C.boolConst(mam_response['complete'])
234 data["mam_stable"] = C.boolConst(mam_response['stable'])
235 return data
236 218
237 def getMessageFromResult(self, client, mess_elt, mam_req, service=None): 219 def getMessageFromResult(self, client, mess_elt, mam_req, service=None):
238 """Extract usable <message/> from MAM query result 220 """Extract usable <message/> from MAM query result
239 221
240 The message will be validated, and stanza-id/delay will be added if necessary. 222 The message will be validated, and stanza-id/delay will be added if necessary.
343 for elt in elt_list: 325 for elt in elt_list:
344 fwd_message_elt = self.getMessageFromResult(client, elt, mam_req, 326 fwd_message_elt = self.getMessageFromResult(client, elt, mam_req,
345 service=service) 327 service=service)
346 mess_data = client.messageProt.parseMessage(fwd_message_elt) 328 mess_data = client.messageProt.parseMessage(fwd_message_elt)
347 mess_list.append(client.messageGetBridgeArgs(mess_data)) 329 mess_list.append(client.messageGetBridgeArgs(mess_data))
348 metadata = self._rsm.serialise(rsm_response) 330 metadata = {
349 self.serialise(mam_response, metadata) 331 'rsm': self._rsm.response2dict(rsm_response),
332 'mam': mam_response
333 }
350 return mess_list, metadata, client.profile 334 return mess_list, metadata, client.profile
351 335
352 def _getArchives(self, service, extra_ser, profile_key): 336 def _getArchives(self, service, extra_ser, profile_key):
353 """ 337 """
354 @return: tuple with: 338 @return: tuple with:
355 - list of message with same data as in bridge.messageNew 339 - list of message with same data as in bridge.messageNew
356 - response metadata with: 340 - response metadata with:
357 - rsm data (rsm_first, rsm_last, rsm_count, rsm_index) 341 - rsm data (first, last, count, index)
358 - mam data (mam_complete, mam_stable) 342 - mam data (complete, stable)
359 - profile 343 - profile
360 """ 344 """
361 client = self.host.getClient(profile_key) 345 client = self.host.getClient(profile_key)
362 service = jid.JID(service) if service else None 346 service = jid.JID(service) if service else None
363 extra = data_format.deserialise(extra_ser, {}) 347 extra = data_format.deserialise(extra_ser, {})