changeset 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 0a87cae85746
children eddab3798aca
files sat/plugins/plugin_xep_0277.py
diffstat 1 files changed, 20 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0277.py	Tue Mar 22 17:00:42 2022 +0100
+++ b/sat/plugins/plugin_xep_0277.py	Tue Mar 22 17:00:42 2022 +0100
@@ -699,6 +699,19 @@
 
     ## publish/preview ##
 
+    def isCommentsNode(self, item_id: str) -> bool:
+        """Indicate if the node is prefixed with comments namespace"""
+        return item_id.startswith(NS_COMMENT_PREFIX)
+
+    def getParentNode(self, item_id: str) -> str:
+        """Return parent of a comment node
+
+        @param item_id: a comment node
+        """
+        if not self.isCommentsNode(item_id):
+            raise ValueError("This node is not a comment node")
+        return item_id[len(NS_COMMENT_PREFIX):]
+
     def getCommentsNode(self, item_id):
         """Generate comment node
 
@@ -733,8 +746,7 @@
             client.pubsub_service if client.pubsub_service is not None else parent_service
         )
 
-    @defer.inlineCallbacks
-    def _manageComments(self, client, mb_data, service, node, item_id, access=None):
+    async def _manageComments(self, client, mb_data, service, node, item_id, access=None):
         """Check comments keys in mb_data and create comments node if necessary
 
         if a comments node metadata is set in the mb_data['comments'] list, it is used
@@ -773,7 +785,7 @@
 
         if access is None:
             # TODO: cache access models per service/node
-            parent_node_config = yield self._p.getConfiguration(client, service, node)
+            parent_node_config = await self._p.getConfiguration(client, service, node)
             access = parent_node_config.get(self._p.OPT_ACCESS_MODEL, self._p.ACCESS_OPEN)
 
         options = {
@@ -787,7 +799,7 @@
         }
 
         # if other plugins need to change the options
-        yield self.host.trigger.point("XEP-0277_comments", client, mb_data, options)
+        await self.host.trigger.point("XEP-0277_comments", client, mb_data, options)
 
         for comments_data in mb_data['comments']:
             uri = comments_data.get('uri')
@@ -811,7 +823,7 @@
                     comments_node = self.getCommentsNode(item_id)
                 comments_data['node'] = comments_node
                 if comments_service is None:
-                    comments_service = yield self.getCommentsService(client, service)
+                    comments_service = await self.getCommentsService(client, service)
                     if comments_service is None:
                         comments_service = client.jid.userhostJID()
                 comments_data['service'] = comments_service
@@ -823,7 +835,7 @@
                 )
 
             try:
-                yield self._p.createNode(client, comments_service, comments_node, options)
+                await self._p.createNode(client, comments_service, comments_node, options)
             except error.StanzaError as e:
                 if e.condition == "conflict":
                     log.info(
@@ -836,7 +848,7 @@
             else:
                 if access == self._p.ACCESS_WHITELIST:
                     # for whitelist access we need to copy affiliations from parent item
-                    comments_affiliations = yield self._p.getNodeAffiliations(
+                    comments_affiliations = await self._p.getNodeAffiliations(
                         client, service, node
                     )
                     # …except for "member", that we transform to publisher
@@ -845,7 +857,7 @@
                         if affiliation == "member":
                             comments_affiliations[jid_] == "publisher"
 
-                    yield self._p.setNodeAffiliations(
+                    await self._p.setNodeAffiliations(
                         client, comments_service, comments_node, comments_affiliations
                     )