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:
parent
680dd48587
commit
8335f92c87
2 changed files with 47 additions and 23 deletions
|
|
@ -136,9 +136,8 @@ class GroupBlog(object):
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _initialise(self, profile_key):
|
def _initialise(self, profile_key):
|
||||||
"""Check that this data for this profile are initialised, and do it else
|
"""Check that the data for this profile are initialised, and do it else
|
||||||
@param client: client of the profile
|
@param profile_key: %(doc_profile)s"""
|
||||||
@profile_key: %(doc_profile)s"""
|
|
||||||
profile = self.host.memory.getProfileName(profile_key)
|
profile = self.host.memory.getProfileName(profile_key)
|
||||||
if not profile:
|
if not profile:
|
||||||
raise exceptions.ProfileUnknownError
|
raise exceptions.ProfileUnknownError
|
||||||
|
|
@ -266,7 +265,7 @@ class GroupBlog(object):
|
||||||
def _publishMblog(self, service, client, access_type, access_list, message, extra):
|
def _publishMblog(self, service, client, access_type, access_list, message, extra):
|
||||||
"""Actually publish the message on the group blog
|
"""Actually publish the message on the group blog
|
||||||
@param service: jid of the item-access pubsub service
|
@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_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 access_list: set of entities (empty list for all, groups or jids) allowed to see the item
|
||||||
@param message: message to publish
|
@param message: message to publish
|
||||||
|
|
@ -285,9 +284,7 @@ class GroupBlog(object):
|
||||||
|
|
||||||
if extra.get('allow_comments', 'False').lower() == 'true':
|
if extra.get('allow_comments', 'False').lower() == 'true':
|
||||||
# XXX: use the item identifier? http://bugs.goffi.org/show_bug.cgi?id=63
|
# 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)
|
comments_node = self.__fillCommentsElement(mblog_data, None, node_name, service)
|
||||||
mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': service.userhost(),
|
|
||||||
'query': urllib.urlencode([('node', comments_node.encode('utf-8'))])}
|
|
||||||
_options = {P.OPT_ACCESS_MODEL: access_model_value,
|
_options = {P.OPT_ACCESS_MODEL: access_model_value,
|
||||||
P.OPT_PERSIST_ITEMS: 1,
|
P.OPT_PERSIST_ITEMS: 1,
|
||||||
P.OPT_MAX_ITEMS: -1,
|
P.OPT_MAX_ITEMS: -1,
|
||||||
|
|
@ -331,6 +328,21 @@ class GroupBlog(object):
|
||||||
entry_d.addCallback(itemCreated)
|
entry_d.addCallback(itemCreated)
|
||||||
return entry_d
|
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):
|
def _mblogPublicationFailed(self, failure):
|
||||||
#TODO
|
#TODO
|
||||||
return failure
|
return failure
|
||||||
|
|
@ -368,7 +380,7 @@ class GroupBlog(object):
|
||||||
|
|
||||||
def deleteGroupBlog(self, pub_data, comments, profile_key='@NONE@'):
|
def deleteGroupBlog(self, pub_data, comments, profile_key='@NONE@'):
|
||||||
"""Delete a microblog item from a node.
|
"""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 comments: comments node identifier (for main item) or empty string
|
||||||
@param profile_key: %(doc_profile_key)s
|
@param profile_key: %(doc_profile_key)s
|
||||||
"""
|
"""
|
||||||
|
|
@ -376,13 +388,16 @@ class GroupBlog(object):
|
||||||
def initialised(result):
|
def initialised(result):
|
||||||
profile, client = result
|
profile, client = result
|
||||||
service, node, item_id = pub_data
|
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:
|
if comments:
|
||||||
# remove the associated comments node
|
# remove the associated comments node
|
||||||
d = self.host.plugins["XEP-0060"].deleteNode(jid.JID(service), node, profile_key=profile)
|
comments_service, comments_node = self.host.plugins["XEP-0277"].parseCommentUrl(comments)
|
||||||
d.addErrback(lambda failure: error("Deletion of node %s failed: %s" % (node, failure.getErrorMessage())))
|
d = self.host.plugins["XEP-0060"].deleteNode(comments_service, comments_node, profile_key=profile)
|
||||||
node = self.getNodeName(client.jid)
|
d.addErrback(lambda failure: error("Deletion of node %s failed: %s" % (comments_node, failure.getErrorMessage())))
|
||||||
# remove the item itself
|
# 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())))
|
d.addErrback(lambda failure: error("Deletion of item %s from %s failed: %s" % (item_id, node, failure.getErrorMessage())))
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
@ -394,7 +409,7 @@ class GroupBlog(object):
|
||||||
service, node, item_id = pub_data
|
service, node, item_id = pub_data
|
||||||
publisher = self.host.getJidNStream(profile_key)[0]
|
publisher = self.host.getJidNStream(profile_key)[0]
|
||||||
profile = self.host.memory.getProfileName(profile_key)
|
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)
|
self.host.bridge.personalEvent(publisher.full(), "MICROBLOG_DELETE", gbdatum, profile)
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
|
@ -402,7 +417,7 @@ class GroupBlog(object):
|
||||||
|
|
||||||
def updateGroupBlog(self, pub_data, comments, message, extra, profile_key='@NONE@'):
|
def updateGroupBlog(self, pub_data, comments, message, extra, profile_key='@NONE@'):
|
||||||
"""Modify a microblog node
|
"""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 comments: comments node identifier (for main item) or empty string
|
||||||
@param message: new message
|
@param message: new message
|
||||||
@param extra: dict which option name as key, which can be:
|
@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]:
|
if attr in extra and extra[attr]:
|
||||||
mblog_data[attr] = extra[attr]
|
mblog_data[attr] = extra[attr]
|
||||||
service, node, item_id = pub_data
|
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)
|
node = self.getNodeName(client.jid)
|
||||||
mblog_data['id'] = str(item_id)
|
mblog_data['id'] = unicode(item_id)
|
||||||
if 'published' in extra:
|
if 'published' in extra:
|
||||||
mblog_data['published'] = extra['published']
|
mblog_data['published'] = extra['published']
|
||||||
if extra.get('allow_comments', 'False').lower() == 'true':
|
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
|
# 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
|
# XXX: use the item identifier? http://bugs.goffi.org/show_bug.cgi?id=63
|
||||||
hash_ = comments_node.split('_')[1].split('__')[0]
|
entry_id = comments_node.split('_')[1].split('__')[0]
|
||||||
comments_node = "%s_%s__%s" % (NS_COMMENT_PREFIX, hash_, node)
|
self.__fillCommentsElement(mblog_data, entry_id, node, service_jid)
|
||||||
mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': jid.JID(service).userhost(),
|
|
||||||
'query': urllib.urlencode([('node', comments_node.encode('utf-8'))])}
|
|
||||||
entry_d = self.host.plugins["XEP-0277"].data2entry(mblog_data, profile)
|
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())))
|
entry_d.addErrback(lambda failure: error("Modification of %s failed: %s" % (pub_data, failure.getErrorMessage())))
|
||||||
return entry_d
|
return entry_d
|
||||||
|
|
||||||
|
|
@ -474,6 +488,10 @@ class GroupBlog(object):
|
||||||
ret = []
|
ret = []
|
||||||
for item in items:
|
for item in items:
|
||||||
gbdata = yield self.item2gbdata(item)
|
gbdata = yield self.item2gbdata(item)
|
||||||
|
try:
|
||||||
|
gbdata['service'] = client.item_access_pubsub.full()
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
ret.append(gbdata)
|
ret.append(gbdata)
|
||||||
# if there is a comments node, we subscribe to it
|
# if there is a comments node, we subscribe to it
|
||||||
if "comments_node" in gbdata:
|
if "comments_node" in gbdata:
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,12 @@ class XEP_0277(object):
|
||||||
doc={})
|
doc={})
|
||||||
|
|
||||||
def parseCommentUrl(self, node_url):
|
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')
|
parsed_url = urlparse.urlparse(node_url, 'xmpp')
|
||||||
service = jid.JID(parsed_url.path)
|
service = jid.JID(parsed_url.path)
|
||||||
queries = parsed_url.query.split(';')
|
queries = parsed_url.query.split(';')
|
||||||
|
|
@ -154,7 +160,7 @@ class XEP_0277(object):
|
||||||
microblog_data['comments_node'] = node
|
microblog_data['comments_node'] = node
|
||||||
break
|
break
|
||||||
except (KeyError, exceptions.DataError, RuntimeError):
|
except (KeyError, exceptions.DataError, RuntimeError):
|
||||||
warning("Can't parse link")
|
warning(_("Can't parse the link element of pubsub entry %s") % item['id'])
|
||||||
continue
|
continue
|
||||||
except (AttributeError, KeyError):
|
except (AttributeError, KeyError):
|
||||||
error(_('Error while parsing atom entry for microblogging event'))
|
error(_('Error while parsing atom entry for microblogging event'))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue