comparison sat/plugins/plugin_xep_0277.py @ 3740:ea6fda69bb9f

plugin XEP-0277: methods to check is a node is a comments node and to get its parent rel 364
author Goffi <goffi@goffi.org>
date Tue, 22 Mar 2022 17:00:42 +0100
parents 33d75cd3c371
children e0ff2f277e13
comparison
equal deleted inserted replaced
3739:0a87cae85746 3740:ea6fda69bb9f
697 697
698 return item_elt 698 return item_elt
699 699
700 ## publish/preview ## 700 ## publish/preview ##
701 701
702 def isCommentsNode(self, item_id: str) -> bool:
703 """Indicate if the node is prefixed with comments namespace"""
704 return item_id.startswith(NS_COMMENT_PREFIX)
705
706 def getParentNode(self, item_id: str) -> str:
707 """Return parent of a comment node
708
709 @param item_id: a comment node
710 """
711 if not self.isCommentsNode(item_id):
712 raise ValueError("This node is not a comment node")
713 return item_id[len(NS_COMMENT_PREFIX):]
714
702 def getCommentsNode(self, item_id): 715 def getCommentsNode(self, item_id):
703 """Generate comment node 716 """Generate comment node
704 717
705 @param item_id(unicode): id of the parent item 718 @param item_id(unicode): id of the parent item
706 @return (unicode): comment node to use 719 @return (unicode): comment node to use
731 744
732 return defer.succeed( 745 return defer.succeed(
733 client.pubsub_service if client.pubsub_service is not None else parent_service 746 client.pubsub_service if client.pubsub_service is not None else parent_service
734 ) 747 )
735 748
736 @defer.inlineCallbacks 749 async def _manageComments(self, client, mb_data, service, node, item_id, access=None):
737 def _manageComments(self, client, mb_data, service, node, item_id, access=None):
738 """Check comments keys in mb_data and create comments node if necessary 750 """Check comments keys in mb_data and create comments node if necessary
739 751
740 if a comments node metadata is set in the mb_data['comments'] list, it is used 752 if a comments node metadata is set in the mb_data['comments'] list, it is used
741 otherwise it is generated (if allow_comments is True). 753 otherwise it is generated (if allow_comments is True).
742 @param mb_data(dict): microblog mb_data 754 @param mb_data(dict): microblog mb_data
771 comments_data = {} 783 comments_data = {}
772 mb_data['comments'].append({}) 784 mb_data['comments'].append({})
773 785
774 if access is None: 786 if access is None:
775 # TODO: cache access models per service/node 787 # TODO: cache access models per service/node
776 parent_node_config = yield self._p.getConfiguration(client, service, node) 788 parent_node_config = await self._p.getConfiguration(client, service, node)
777 access = parent_node_config.get(self._p.OPT_ACCESS_MODEL, self._p.ACCESS_OPEN) 789 access = parent_node_config.get(self._p.OPT_ACCESS_MODEL, self._p.ACCESS_OPEN)
778 790
779 options = { 791 options = {
780 self._p.OPT_ACCESS_MODEL: access, 792 self._p.OPT_ACCESS_MODEL: access,
781 self._p.OPT_PERSIST_ITEMS: 1, 793 self._p.OPT_PERSIST_ITEMS: 1,
785 # FIXME: would it make sense to restrict publish model to subscribers? 797 # FIXME: would it make sense to restrict publish model to subscribers?
786 self._p.OPT_PUBLISH_MODEL: self._p.ACCESS_OPEN, 798 self._p.OPT_PUBLISH_MODEL: self._p.ACCESS_OPEN,
787 } 799 }
788 800
789 # if other plugins need to change the options 801 # if other plugins need to change the options
790 yield self.host.trigger.point("XEP-0277_comments", client, mb_data, options) 802 await self.host.trigger.point("XEP-0277_comments", client, mb_data, options)
791 803
792 for comments_data in mb_data['comments']: 804 for comments_data in mb_data['comments']:
793 uri = comments_data.get('uri') 805 uri = comments_data.get('uri')
794 comments_node = comments_data.get('node') 806 comments_node = comments_data.get('node')
795 try: 807 try:
809 else: 821 else:
810 if not comments_node: 822 if not comments_node:
811 comments_node = self.getCommentsNode(item_id) 823 comments_node = self.getCommentsNode(item_id)
812 comments_data['node'] = comments_node 824 comments_data['node'] = comments_node
813 if comments_service is None: 825 if comments_service is None:
814 comments_service = yield self.getCommentsService(client, service) 826 comments_service = await self.getCommentsService(client, service)
815 if comments_service is None: 827 if comments_service is None:
816 comments_service = client.jid.userhostJID() 828 comments_service = client.jid.userhostJID()
817 comments_data['service'] = comments_service 829 comments_data['service'] = comments_service
818 830
819 comments_data['uri'] = xmpp_uri.buildXMPPUri( 831 comments_data['uri'] = xmpp_uri.buildXMPPUri(
821 path=comments_service.full(), 833 path=comments_service.full(),
822 node=comments_node, 834 node=comments_node,
823 ) 835 )
824 836
825 try: 837 try:
826 yield self._p.createNode(client, comments_service, comments_node, options) 838 await self._p.createNode(client, comments_service, comments_node, options)
827 except error.StanzaError as e: 839 except error.StanzaError as e:
828 if e.condition == "conflict": 840 if e.condition == "conflict":
829 log.info( 841 log.info(
830 "node {} already exists on service {}".format( 842 "node {} already exists on service {}".format(
831 comments_node, comments_service 843 comments_node, comments_service
834 else: 846 else:
835 raise e 847 raise e
836 else: 848 else:
837 if access == self._p.ACCESS_WHITELIST: 849 if access == self._p.ACCESS_WHITELIST:
838 # for whitelist access we need to copy affiliations from parent item 850 # for whitelist access we need to copy affiliations from parent item
839 comments_affiliations = yield self._p.getNodeAffiliations( 851 comments_affiliations = await self._p.getNodeAffiliations(
840 client, service, node 852 client, service, node
841 ) 853 )
842 # …except for "member", that we transform to publisher 854 # …except for "member", that we transform to publisher
843 # because we wants members to be able to write to comments 855 # because we wants members to be able to write to comments
844 for jid_, affiliation in list(comments_affiliations.items()): 856 for jid_, affiliation in list(comments_affiliations.items()):
845 if affiliation == "member": 857 if affiliation == "member":
846 comments_affiliations[jid_] == "publisher" 858 comments_affiliations[jid_] == "publisher"
847 859
848 yield self._p.setNodeAffiliations( 860 await self._p.setNodeAffiliations(
849 client, comments_service, comments_node, comments_affiliations 861 client, comments_service, comments_node, comments_affiliations
850 ) 862 )
851 863
852 def friendlyId(self, data): 864 def friendlyId(self, data):
853 """Generate a user friendly id from title or content""" 865 """Generate a user friendly id from title or content"""