Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0277.py @ 1661:96ee986dab3c
plugin XEP-0277: added a trigger on data2entry and comments management
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 24 Nov 2015 16:21:18 +0100 |
parents | 069abd15354f |
children | 5d0ff155be1a |
comparison
equal
deleted
inserted
replaced
1660:204c2a7fe68b | 1661:96ee986dab3c |
---|---|
315 microblog_data['author_email'] = unicode(email_elt) | 315 microblog_data['author_email'] = unicode(email_elt) |
316 | 316 |
317 defer.returnValue(microblog_data) | 317 defer.returnValue(microblog_data) |
318 | 318 |
319 @defer.inlineCallbacks | 319 @defer.inlineCallbacks |
320 def data2entry(self, data, item_id=None, profile_key=C.PROF_KEY_NONE): | 320 def data2entry(self, data, item_id=None, profile=C.PROF_KEY_NONE): |
321 """Convert a data dict to en entry usable to create an item | 321 """Convert a data dict to en entry usable to create an item |
322 | 322 |
323 @param data: data dict as given by bridge method. | 323 @param data: data dict as given by bridge method. |
324 @param item_id(unicode, None): id of the item to use | 324 @param item_id(unicode, None): id of the item to use |
325 if None the id will be generated randomly | 325 if None the id will be generated randomly |
326 @return: deferred which fire domish.Element | 326 @return: deferred which fire domish.Element |
327 """ | 327 """ |
328 if item_id is None: | 328 if item_id is None: |
329 item_id = unicode(uuid.uuid4()) | 329 item_id = unicode(uuid.uuid4()) |
330 client = self.host.getClient(profile_key) | 330 client = self.host.getClient(profile) |
331 entry_elt = domish.Element((NS_ATOM, 'entry')) | 331 entry_elt = domish.Element((NS_ATOM, 'entry')) |
332 | 332 |
333 ## content and title ## | 333 ## content and title ## |
334 synt = self.host.plugins["TEXT-SYNTAXES"] | 334 synt = self.host.plugins["TEXT-SYNTAXES"] |
335 | 335 |
338 attr = "{}{}".format(elem_name, type_) | 338 attr = "{}{}".format(elem_name, type_) |
339 if attr in data: | 339 if attr in data: |
340 elem = entry_elt.addElement(elem_name) | 340 elem = entry_elt.addElement(elem_name) |
341 if type_: | 341 if type_: |
342 if type_ == '_rich': # convert input from current syntax to XHTML | 342 if type_ == '_rich': # convert input from current syntax to XHTML |
343 converted = yield synt.convert(data[attr], synt.getCurrentSyntax(profile_key), "XHTML") | 343 converted = yield synt.convert(data[attr], synt.getCurrentSyntax(profile), "XHTML") |
344 if '{}_xhtml'.format(elem_name) in data: | 344 if '{}_xhtml'.format(elem_name) in data: |
345 raise failure.Failure(exceptions.DataError(_("Can't have xhtml and rich content at the same time"))) | 345 raise failure.Failure(exceptions.DataError(_("Can't have xhtml and rich content at the same time"))) |
346 else: # clean the XHTML input | 346 else: # clean the XHTML input |
347 converted = yield synt.clean_xhtml(data[attr]) | 347 converted = yield synt.clean_xhtml(data[attr]) |
348 | 348 |
410 link_elt['rel'] = 'replies' | 410 link_elt['rel'] = 'replies' |
411 link_elt['title'] = 'comments' | 411 link_elt['title'] = 'comments' |
412 | 412 |
413 ## final item building ## | 413 ## final item building ## |
414 item_elt = pubsub.Item(id=item_id, payload=entry_elt) | 414 item_elt = pubsub.Item(id=item_id, payload=entry_elt) |
415 | |
416 ## the trigger ## | |
417 # if other plugins have things to add or change | |
418 yield self.host.trigger.point("XEP-0277_data2entry", client, data, entry_elt, item_elt) | |
419 | |
415 defer.returnValue(item_elt) | 420 defer.returnValue(item_elt) |
416 | 421 |
417 ## publish ## | 422 ## publish ## |
418 | 423 |
419 @defer.inlineCallbacks | 424 @defer.inlineCallbacks |
438 self._p.OPT_DELIVER_PAYLOADS: 1, | 443 self._p.OPT_DELIVER_PAYLOADS: 1, |
439 self._p.OPT_SEND_ITEM_SUBSCRIBE: 1, | 444 self._p.OPT_SEND_ITEM_SUBSCRIBE: 1, |
440 self._p.OPT_PUBLISH_MODEL: "subscribers", # TODO: should be open if *both* node and item access_model are open (public node and item) | 445 self._p.OPT_PUBLISH_MODEL: "subscribers", # TODO: should be open if *both* node and item access_model are open (public node and item) |
441 } | 446 } |
442 | 447 |
448 # if other plugins need to change the options | |
449 yield self.host.trigger.point("XEP-0277_comments", client, mb_data, options) | |
450 | |
443 comments_node_base = u"{}{}".format(NS_COMMENT_PREFIX, item_id) | 451 comments_node_base = u"{}{}".format(NS_COMMENT_PREFIX, item_id) |
444 comments_node = comments_node_base | 452 comments_node = comments_node_base |
445 | 453 |
446 suffix = None | 454 suffix = None |
447 comments_service = client.pubsub_service if client.pubsub_service is not None else service | 455 comments_service = client.pubsub_service if client.pubsub_service is not None else service |
456 | |
457 # we try to create the comment nodes # | |
448 max_tries = 3 | 458 max_tries = 3 |
449 | 459 |
450 for i in xrange(max_tries+1): | 460 for i in xrange(max_tries+1): |
451 try: | 461 try: |
452 yield self._p.createNode(comments_service, comments_node, options, profile_key=profile) | 462 yield self._p.createNode(comments_service, comments_node, options, profile_key=profile) |