comparison src/plugins/plugin_xep_0277.py @ 2480:dc55b854ccf3

plugin XEP-0277: use shortuuid instead of uuid + use item's XMPP URI for atom ID
author Goffi <goffi@goffi.org>
date Mon, 22 Jan 2018 22:00:46 +0100
parents efd7a24d611c
children 0046283a285d
comparison
equal deleted inserted replaced
2479:0d6c53e6c591 2480:dc55b854ccf3
29 from sat.core import exceptions 29 from sat.core import exceptions
30 from sat.tools import xml_tools 30 from sat.tools import xml_tools
31 from sat.tools import sat_defer 31 from sat.tools import sat_defer
32 from sat.tools import utils 32 from sat.tools import utils
33 from sat.tools.common import data_format 33 from sat.tools.common import data_format
34 from sat.tools.common import uri as xmpp_uri
34 35
35 # XXX: sat_tmp.wokkel.pubsub is actually used instead of wokkel version 36 # XXX: sat_tmp.wokkel.pubsub is actually used instead of wokkel version
36 from wokkel import pubsub 37 from wokkel import pubsub
37 from wokkel import disco, iwokkel 38 from wokkel import disco, iwokkel
38 from zope.interface import implements 39 from zope.interface import implements
39 import uuid 40 import shortuuid
40 import time 41 import time
41 import dateutil 42 import dateutil
42 import calendar 43 import calendar
43 import urlparse 44 import urlparse
44 45
343 yield self.host.trigger.point("XEP-0277_item2data", item_elt, entry_elt, microblog_data) 344 yield self.host.trigger.point("XEP-0277_item2data", item_elt, entry_elt, microblog_data)
344 345
345 defer.returnValue(microblog_data) 346 defer.returnValue(microblog_data)
346 347
347 @defer.inlineCallbacks 348 @defer.inlineCallbacks
348 def data2entry(self, data, item_id=None, profile=C.PROF_KEY_NONE): 349 def data2entry(self, client, data, item_id, service, node):
349 """Convert a data dict to en entry usable to create an item 350 """Convert a data dict to en entry usable to create an item
350 351
351 @param data: data dict as given by bridge method. 352 @param data: data dict as given by bridge method.
352 @param item_id(unicode, None): id of the item to use 353 @param item_id(unicode): id of the item to use
354 @param service(jid.JID, None): pubsub service where the item is sent
355 Needed to construct Atom id
356 @param node(unicode): pubsub node where the item is sent
357 Needed to construct Atom id
353 @return: deferred which fire domish.Element 358 @return: deferred which fire domish.Element
354 """ 359 """
355 client = self.host.getClient(profile)
356 entry_elt = domish.Element((NS_ATOM, 'entry')) 360 entry_elt = domish.Element((NS_ATOM, 'entry'))
357 361
358 ## language ## 362 ## language ##
359 if u'language' in data: 363 if u'language' in data:
360 entry_elt[(C.NS_XML, u'lang')] = data[u'language'].strip() 364 entry_elt[(C.NS_XML, u'lang')] = data[u'language'].strip()
367 attr = "{}{}".format(elem_name, type_) 371 attr = "{}{}".format(elem_name, type_)
368 if attr in data: 372 if attr in data:
369 elem = entry_elt.addElement(elem_name) 373 elem = entry_elt.addElement(elem_name)
370 if type_: 374 if type_:
371 if type_ == '_rich': # convert input from current syntax to XHTML 375 if type_ == '_rich': # convert input from current syntax to XHTML
372 xml_content = yield synt.convert(data[attr], synt.getCurrentSyntax(profile), "XHTML") 376 xml_content = yield synt.convert(data[attr], synt.getCurrentSyntax(client.profile), "XHTML")
373 if '{}_xhtml'.format(elem_name) in data: 377 if '{}_xhtml'.format(elem_name) in data:
374 raise failure.Failure(exceptions.DataError(_("Can't have xhtml and rich content at the same time"))) 378 raise failure.Failure(exceptions.DataError(_("Can't have xhtml and rich content at the same time")))
375 else: 379 else:
376 xml_content = data[attr] 380 xml_content = data[attr]
377 381
440 for tag in data_format.dict2iter("tag", data): 444 for tag in data_format.dict2iter("tag", data):
441 category_elt = entry_elt.addElement("category") 445 category_elt = entry_elt.addElement("category")
442 category_elt['term'] = tag 446 category_elt['term'] = tag
443 447
444 ## id ## 448 ## id ##
445 entry_id = data.get('id', item_id or unicode(uuid.uuid4())) # FIXME: use a proper id (see XEP-0277 §7.1) 449 entry_id = data.get('id', xmpp_uri.buildXMPPUri(
450 u'pubsub',
451 path=service.full() if service is not None else client.jid.userhost(),
452 node=node,
453 item=item_id))
446 entry_elt.addElement('id', content=entry_id) # 454 entry_elt.addElement('id', content=entry_id) #
447 455
448 ## comments ## 456 ## comments ##
449 if 'comments' in data: 457 if 'comments' in data:
450 link_elt = entry_elt.addElement('link') 458 link_elt = entry_elt.addElement('link')
594 # TODO: check that all data keys are used, this would avoid sending publicly a private message 602 # TODO: check that all data keys are used, this would avoid sending publicly a private message
595 # by accident (e.g. if group pluging is not loaded, and "grou*" key are not used) 603 # by accident (e.g. if group pluging is not loaded, and "grou*" key are not used)
596 if node is None: 604 if node is None:
597 node = NS_MICROBLOG 605 node = NS_MICROBLOG
598 606
599 item_id = data.get('id') or unicode(uuid.uuid4()) 607 item_id = data.get('id') or unicode(shortuuid.uuid())
600 608
601 try: 609 try:
602 yield self._manageComments(client, data, service, node, item_id, access=None) 610 yield self._manageComments(client, data, service, node, item_id, access=None)
603 except error.StanzaError: 611 except error.StanzaError:
604 log.warning(u"Can't create comments node for item {}".format(item_id)) 612 log.warning(u"Can't create comments node for item {}".format(item_id))
605 item = yield self.data2entry(data, item_id, client.profile) 613 item = yield self.data2entry(client, data, item_id, service, node)
606 ret = yield self._p.publish(client, service, node, [item]) 614 ret = yield self._p.publish(client, service, node, [item])
607 defer.returnValue(ret) 615 defer.returnValue(ret)
608 616
609 ## retract ## 617 ## retract ##
610 618