plugins groupblog, XEP-0277: attempt to clarify the code for the comments handling:

- add or update some methods docstrings
- add GroupBlog.__fillCommentsElement to factorize the code responsible to build the comments link
- do not mix up attributes names between the item own service/node and the comments service/node
This commit is contained in:
souliane 2014-02-14 21:24:31 +01:00
commit 8335f92c87
2 changed files with 47 additions and 23 deletions

View file

@ -136,9 +136,8 @@ class GroupBlog(object):
@defer.inlineCallbacks
def _initialise(self, profile_key):
"""Check that this data for this profile are initialised, and do it else
@param client: client of the profile
@profile_key: %(doc_profile)s"""
"""Check that the data for this profile are initialised, and do it else
@param profile_key: %(doc_profile)s"""
profile = self.host.memory.getProfileName(profile_key)
if not profile:
raise exceptions.ProfileUnknownError
@ -266,7 +265,7 @@ class GroupBlog(object):
def _publishMblog(self, service, client, access_type, access_list, message, extra):
"""Actually publish the message on the group blog
@param service: jid of the item-access pubsub service
@param client: SatXMPPClient of the published
@param client: SatXMPPClient of the publisher
@param access_type: one of "PUBLIC", "GROUP", "JID"
@param access_list: set of entities (empty list for all, groups or jids) allowed to see the item
@param message: message to publish
@ -285,9 +284,7 @@ class GroupBlog(object):
if extra.get('allow_comments', 'False').lower() == 'true':
# XXX: use the item identifier? http://bugs.goffi.org/show_bug.cgi?id=63
comments_node = "%s_%s__%s" % (NS_COMMENT_PREFIX, str(uuid.uuid4()), node_name)
mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': service.userhost(),
'query': urllib.urlencode([('node', comments_node.encode('utf-8'))])}
comments_node = self.__fillCommentsElement(mblog_data, None, node_name, service)
_options = {P.OPT_ACCESS_MODEL: access_model_value,
P.OPT_PERSIST_ITEMS: 1,
P.OPT_MAX_ITEMS: -1,
@ -331,6 +328,21 @@ class GroupBlog(object):
entry_d.addCallback(itemCreated)
return entry_d
def __fillCommentsElement(self, mblog_data, entry_id, node_name, service_jid):
"""
@param mblog_data: dict containing the microblog data
@param entry_id: unique identifier of the entry
@param node_name: the pubsub node name
@param service_jid: the JID of the pubsub service
@return: the comments node string
"""
if entry_id is None:
entry_id = unicode(uuid.uuid4())
comments_node = "%s_%s__%s" % (NS_COMMENT_PREFIX, entry_id, node_name)
mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': service_jid.userhost(),
'query': urllib.urlencode([('node', comments_node.encode('utf-8'))])}
return comments_node
def _mblogPublicationFailed(self, failure):
#TODO
return failure
@ -368,7 +380,7 @@ class GroupBlog(object):
def deleteGroupBlog(self, pub_data, comments, profile_key='@NONE@'):
"""Delete a microblog item from a node.
@param pub_data: a tuple (service, comment node identifier, item identifier)
@param pub_data: a tuple (service, node identifier, item identifier)
@param comments: comments node identifier (for main item) or empty string
@param profile_key: %(doc_profile_key)s
"""
@ -376,13 +388,16 @@ class GroupBlog(object):
def initialised(result):
profile, client = result
service, node, item_id = pub_data
service_jid = jid.JID(service) if service else client.item_access_pubsub
if comments or not node: # main item
node = self.getNodeName(client.jid)
if comments:
# remove the associated comments node
d = self.host.plugins["XEP-0060"].deleteNode(jid.JID(service), node, profile_key=profile)
d.addErrback(lambda failure: error("Deletion of node %s failed: %s" % (node, failure.getErrorMessage())))
node = self.getNodeName(client.jid)
comments_service, comments_node = self.host.plugins["XEP-0277"].parseCommentUrl(comments)
d = self.host.plugins["XEP-0060"].deleteNode(comments_service, comments_node, profile_key=profile)
d.addErrback(lambda failure: error("Deletion of node %s failed: %s" % (comments_node, failure.getErrorMessage())))
# remove the item itself
d = self.host.plugins["XEP-0060"].retractItems(jid.JID(service), node, [item_id], profile_key=profile)
d = self.host.plugins["XEP-0060"].retractItems(service_jid, node, [item_id], profile_key=profile)
d.addErrback(lambda failure: error("Deletion of item %s from %s failed: %s" % (item_id, node, failure.getErrorMessage())))
return d
@ -394,7 +409,7 @@ class GroupBlog(object):
service, node, item_id = pub_data
publisher = self.host.getJidNStream(profile_key)[0]
profile = self.host.memory.getProfileName(profile_key)
gbdatum = {'id': item_id, 'type': 'main_item' if comments else 'comment'}
gbdatum = {'id': item_id, 'type': 'main_item' if (comments or not node) else 'comment'}
self.host.bridge.personalEvent(publisher.full(), "MICROBLOG_DELETE", gbdatum, profile)
return d
@ -402,7 +417,7 @@ class GroupBlog(object):
def updateGroupBlog(self, pub_data, comments, message, extra, profile_key='@NONE@'):
"""Modify a microblog node
@param pub_data: a tuple (service, comment node identifier, item identifier)
@param pub_data: a tuple (service, node identifier, item identifier)
@param comments: comments node identifier (for main item) or empty string
@param message: new message
@param extra: dict which option name as key, which can be:
@ -418,21 +433,20 @@ class GroupBlog(object):
if attr in extra and extra[attr]:
mblog_data[attr] = extra[attr]
service, node, item_id = pub_data
if comments:
service_jid = jid.JID(service) if service else client.item_access_pubsub
if comments or not node: # main item
node = self.getNodeName(client.jid)
mblog_data['id'] = str(item_id)
mblog_data['id'] = unicode(item_id)
if 'published' in extra:
mblog_data['published'] = extra['published']
if extra.get('allow_comments', 'False').lower() == 'true':
comments_node = self.host.plugins["XEP-0277"].parseCommentUrl(comments)[1]
comments_service, comments_node = self.host.plugins["XEP-0277"].parseCommentUrl(comments)
# we could use comments_node directly but it's safer to rebuild it
# XXX: use the item identifier? http://bugs.goffi.org/show_bug.cgi?id=63
hash_ = comments_node.split('_')[1].split('__')[0]
comments_node = "%s_%s__%s" % (NS_COMMENT_PREFIX, hash_, node)
mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': jid.JID(service).userhost(),
'query': urllib.urlencode([('node', comments_node.encode('utf-8'))])}
entry_id = comments_node.split('_')[1].split('__')[0]
self.__fillCommentsElement(mblog_data, entry_id, node, service_jid)
entry_d = self.host.plugins["XEP-0277"].data2entry(mblog_data, profile)
entry_d.addCallback(lambda mblog_item: self.host.plugins["XEP-0060"].publish(jid.JID(service), node, items=[mblog_item], profile_key=profile))
entry_d.addCallback(lambda mblog_item: self.host.plugins["XEP-0060"].publish(service_jid, node, items=[mblog_item], profile_key=profile))
entry_d.addErrback(lambda failure: error("Modification of %s failed: %s" % (pub_data, failure.getErrorMessage())))
return entry_d
@ -474,6 +488,10 @@ class GroupBlog(object):
ret = []
for item in items:
gbdata = yield self.item2gbdata(item)
try:
gbdata['service'] = client.item_access_pubsub.full()
except AttributeError:
pass
ret.append(gbdata)
# if there is a comments node, we subscribe to it
if "comments_node" in gbdata:

View file

@ -71,6 +71,12 @@ class XEP_0277(object):
doc={})
def parseCommentUrl(self, node_url):
"""Determine the fields comments_service and comments_node of a microblog data
from the href attribute of an entry's link element. For example this input:
xmpp:sat-pubsub.libervia.org?node=urn%3Axmpp%3Acomments%3A_c5c4a142-2279-4b2a-ba4c-1bc33aa87634__urn%3Axmpp%3Agroupblog%3Asouliane%libervia.org
will return (JID(u'sat-pubsub.libervia.org'), 'urn:xmpp:comments:_c5c4a142-2279-4b2a-ba4c-1bc33aa87634__urn:xmpp:groupblog:souliane%libervia.org')
@return: a tuple (JID, str)
"""
parsed_url = urlparse.urlparse(node_url, 'xmpp')
service = jid.JID(parsed_url.path)
queries = parsed_url.query.split(';')
@ -154,7 +160,7 @@ class XEP_0277(object):
microblog_data['comments_node'] = node
break
except (KeyError, exceptions.DataError, RuntimeError):
warning("Can't parse link")
warning(_("Can't parse the link element of pubsub entry %s") % item['id'])
continue
except (AttributeError, KeyError):
error(_('Error while parsing atom entry for microblogging event'))