comparison sat/plugins/plugin_xep_0277.py @ 3724:a0c08fcfe11e

plugin XEP-0277: various fixes + async: - `NS_ATOM` is now set at class level, so it can be accessed by other plugins - set `author` to `None` when no `name` element is found - make `data2entry` a corountine - fix incorrect use of yield in `trigger.point`
author Goffi <goffi@goffi.org>
date Tue, 25 Jan 2022 17:22:15 +0100
parents 68f2a9c171d1
children 33d75cd3c371
comparison
equal deleted inserted replaced
3723:1cdb9d9fad6b 3724:a0c08fcfe11e
75 pass 75 pass
76 76
77 77
78 class XEP_0277(object): 78 class XEP_0277(object):
79 namespace = NS_MICROBLOG 79 namespace = NS_MICROBLOG
80 NS_ATOM = NS_ATOM
80 81
81 def __init__(self, host): 82 def __init__(self, host):
82 log.info(_("Microblogging plugin initialization")) 83 log.info(_("Microblogging plugin initialization"))
83 self.host = host 84 self.host = host
84 host.registerNamespace("microblog", NS_MICROBLOG) 85 host.registerNamespace("microblog", NS_MICROBLOG)
466 name_elt = next(author_elt.elements(NS_ATOM, "name")) 467 name_elt = next(author_elt.elements(NS_ATOM, "name"))
467 except StopIteration: 468 except StopIteration:
468 log.warning( 469 log.warning(
469 "No name element found in author element of item {}".format(id_) 470 "No name element found in author element of item {}".format(id_)
470 ) 471 )
472 author = None
471 else: 473 else:
472 author = microblog_data["author"] = str(name_elt).strip() 474 author = microblog_data["author"] = str(name_elt).strip()
473 # uri 475 # uri
474 try: 476 try:
475 uri_elt = next(author_elt.elements(NS_ATOM, "uri")) 477 uri_elt = next(author_elt.elements(NS_ATOM, "uri"))
539 "XEP-0277_item2data", item_elt, entry_elt, microblog_data 541 "XEP-0277_item2data", item_elt, entry_elt, microblog_data
540 ) 542 )
541 543
542 defer.returnValue(microblog_data) 544 defer.returnValue(microblog_data)
543 545
544 @defer.inlineCallbacks 546 async def data2entry(self, client, data, item_id, service, node):
545 def data2entry(self, client, data, item_id, service, node):
546 """Convert a data dict to en entry usable to create an item 547 """Convert a data dict to en entry usable to create an item
547 548
548 @param data: data dict as given by bridge method. 549 @param data: data dict as given by bridge method.
549 @param item_id(unicode): id of the item to use 550 @param item_id(unicode): id of the item to use
550 @param service(jid.JID, None): pubsub service where the item is sent 551 @param service(jid.JID, None): pubsub service where the item is sent
567 attr = "{}{}".format(elem_name, type_) 568 attr = "{}{}".format(elem_name, type_)
568 if attr in data: 569 if attr in data:
569 elem = entry_elt.addElement(elem_name) 570 elem = entry_elt.addElement(elem_name)
570 if type_: 571 if type_:
571 if type_ == "_rich": # convert input from current syntax to XHTML 572 if type_ == "_rich": # convert input from current syntax to XHTML
572 xml_content = yield synt.convert( 573 xml_content = await synt.convert(
573 data[attr], synt.getCurrentSyntax(client.profile), "XHTML" 574 data[attr], synt.getCurrentSyntax(client.profile), "XHTML"
574 ) 575 )
575 if "{}_xhtml".format(elem_name) in data: 576 if "{}_xhtml".format(elem_name) in data:
576 raise failure.Failure( 577 raise failure.Failure(
577 exceptions.DataError( 578 exceptions.DataError(
599 elem["type"] = "xhtml" 600 elem["type"] = "xhtml"
600 if elem_name not in data: 601 if elem_name not in data:
601 # there is raw text content, which is mandatory 602 # there is raw text content, which is mandatory
602 # so we create one from xhtml content 603 # so we create one from xhtml content
603 elem_txt = entry_elt.addElement(elem_name) 604 elem_txt = entry_elt.addElement(elem_name)
604 text_content = yield self.host.plugins[ 605 text_content = await self.host.plugins[
605 "TEXT_SYNTAXES" 606 "TEXT_SYNTAXES"
606 ].convert( 607 ].convert(
607 xml_content, 608 xml_content,
608 self.host.plugins["TEXT_SYNTAXES"].SYNTAX_XHTML, 609 self.host.plugins["TEXT_SYNTAXES"].SYNTAX_XHTML,
609 self.host.plugins["TEXT_SYNTAXES"].SYNTAX_TEXT, 610 self.host.plugins["TEXT_SYNTAXES"].SYNTAX_TEXT,
687 ## final item building ## 688 ## final item building ##
688 item_elt = pubsub.Item(id=item_id, payload=entry_elt) 689 item_elt = pubsub.Item(id=item_id, payload=entry_elt)
689 690
690 ## the trigger ## 691 ## the trigger ##
691 # if other plugins have things to add or change 692 # if other plugins have things to add or change
692 yield self.host.trigger.point( 693 self.host.trigger.point(
693 "XEP-0277_data2entry", client, data, entry_elt, item_elt 694 "XEP-0277_data2entry", client, data, entry_elt, item_elt
694 ) 695 )
695 696
696 defer.returnValue(item_elt) 697 return item_elt
697 698
698 ## publish/preview ## 699 ## publish/preview ##
699 700
700 def getCommentsNode(self, item_id): 701 def getCommentsNode(self, item_id):
701 """Generate comment node 702 """Generate comment node