plugin XEP-0277: methods to check is a node is a comments node and to get its parent

rel 364
This commit is contained in:
Goffi 2022-03-22 17:00:42 +01:00
commit 7588951bd9

View file

@ -699,6 +699,19 @@ class XEP_0277(object):
## 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 @@ class XEP_0277(object):
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 @@ class XEP_0277(object):
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 @@ class XEP_0277(object):
}
# 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 @@ class XEP_0277(object):
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 @@ class XEP_0277(object):
)
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 @@ class XEP_0277(object):
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 @@ class XEP_0277(object):
if affiliation == "member":
comments_affiliations[jid_] == "publisher"
yield self._p.setNodeAffiliations(
await self._p.setNodeAffiliations(
client, comments_service, comments_node, comments_affiliations
)