plugin XEP-0277, groupblog: rich text management for receiving microblogs
This commit is contained in:
parent
5981926b2b
commit
d6e7ec0055
2 changed files with 63 additions and 21 deletions
|
|
@ -176,22 +176,30 @@ class GroupBlog(object):
|
||||||
or event_host[-len(origin_host):] != origin_host):
|
or event_host[-len(origin_host):] != origin_host):
|
||||||
warning("Host incoherence between %s and %s (hack attempt ?)" % (unicode(event.sender),
|
warning("Host incoherence between %s and %s (hack attempt ?)" % (unicode(event.sender),
|
||||||
unicode(publisher)))
|
unicode(publisher)))
|
||||||
return
|
return False
|
||||||
|
|
||||||
client = self.host.getClient(profile)
|
client = self.host.getClient(profile)
|
||||||
|
|
||||||
for gbdata in self._itemsConstruction(event.items, publisher, client):
|
def gbdataManagementMicroblog(gbdata):
|
||||||
self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", gbdata, profile)
|
for gbdatum in gbdata:
|
||||||
|
self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", gbdatum, profile)
|
||||||
|
|
||||||
|
d = self._itemsConstruction(event.items, publisher, client)
|
||||||
|
d.addCallback(gbdataManagementMicroblog)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
elif event.nodeIdentifier.startswith(NS_COMMENT_PREFIX):
|
elif event.nodeIdentifier.startswith(NS_COMMENT_PREFIX):
|
||||||
# Comment
|
# Comment
|
||||||
for microblog_data in self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier):
|
def gbdataManagementComments(gbdata):
|
||||||
publisher = None # FIXME: see below (_handleCommentsItems)
|
for gbdatum in gbdata:
|
||||||
self.host.bridge.personalEvent(publisher.full() if publisher else microblog_data["author"], "MICROBLOG", microblog_data, profile)
|
publisher = None # FIXME: see below (_handleCommentsItems)
|
||||||
|
self.host.bridge.personalEvent(publisher.full() if publisher else gbdatum["author"], "MICROBLOG", gbdatum, profile)
|
||||||
|
d = self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier)
|
||||||
|
d.addCallback(gbdataManagementComments)
|
||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
def _handleCommentsItems(self, items, service, node_identifier):
|
def _handleCommentsItems(self, items, service, node_identifier):
|
||||||
""" Convert comments items to groupblog data, and send them as signals
|
""" Convert comments items to groupblog data, and send them as signals
|
||||||
@param items: comments items
|
@param items: comments items
|
||||||
|
|
@ -204,12 +212,12 @@ class GroupBlog(object):
|
||||||
publisher = "" # FIXME: publisher attribute for item in SàT pubsub is not managed yet, so
|
publisher = "" # FIXME: publisher attribute for item in SàT pubsub is not managed yet, so
|
||||||
# publisher is not checked and can be easily spoofed. This need to be fixed
|
# publisher is not checked and can be easily spoofed. This need to be fixed
|
||||||
# quickly.
|
# quickly.
|
||||||
microblog_data = self.item2gbdata(item, "comment")
|
microblog_data = yield self.item2gbdata(item, "comment")
|
||||||
microblog_data["service"] = service.userhost()
|
microblog_data["service"] = service.userhost()
|
||||||
microblog_data["node"] = node_identifier
|
microblog_data["node"] = node_identifier
|
||||||
microblog_data["verified_publisher"] = "true" if publisher else "false"
|
microblog_data["verified_publisher"] = "true" if publisher else "false"
|
||||||
ret.append(microblog_data)
|
ret.append(microblog_data)
|
||||||
return ret
|
defer.returnValue(ret)
|
||||||
|
|
||||||
def _parseAccessData(self, microblog_data, item):
|
def _parseAccessData(self, microblog_data, item):
|
||||||
P = self.host.plugins["XEP-0060"]
|
P = self.host.plugins["XEP-0060"]
|
||||||
|
|
@ -228,12 +236,13 @@ class GroupBlog(object):
|
||||||
|
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
def item2gbdata(self, item, _type="main_item"):
|
def item2gbdata(self, item, _type="main_item"):
|
||||||
""" Convert item to microblog data dictionary + add access data """
|
""" Convert item to microblog data dictionary + add access data """
|
||||||
microblog_data = self.host.plugins["XEP-0277"].item2mbdata(item)
|
microblog_data = yield self.host.plugins["XEP-0277"].item2mbdata(item)
|
||||||
microblog_data["type"] = _type
|
microblog_data["type"] = _type
|
||||||
self._parseAccessData(microblog_data, item)
|
self._parseAccessData(microblog_data, item)
|
||||||
return microblog_data
|
defer.returnValue(microblog_data)
|
||||||
|
|
||||||
def getNodeName(self, publisher):
|
def getNodeName(self, publisher):
|
||||||
"""Retrieve the name of publisher's node
|
"""Retrieve the name of publisher's node
|
||||||
|
|
@ -359,16 +368,17 @@ class GroupBlog(object):
|
||||||
entry_d.addCallback(lambda mblog_item: self.host.plugins["XEP-0060"].publish(service, node, items=[mblog_item], profile_key=profile))
|
entry_d.addCallback(lambda mblog_item: self.host.plugins["XEP-0060"].publish(service, node, items=[mblog_item], profile_key=profile))
|
||||||
return entry_d
|
return entry_d
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
def _itemsConstruction(self, items, pub_jid, client):
|
def _itemsConstruction(self, items, pub_jid, client):
|
||||||
""" Transforms items to group blog data and manage comments node
|
""" Transforms items to group blog data and manage comments node
|
||||||
@param items: iterable of items
|
@param items: iterable of items
|
||||||
@param pub_jid: jid of the publisher or None to use items data
|
@param pub_jid: jid of the publisher or None to use items data
|
||||||
@param client: SatXMPPClient instance
|
@param client: SatXMPPClient instance
|
||||||
@return: list of group blog data """
|
@return: deferred which fire list of group blog data """
|
||||||
# TODO: use items data when pub_jid is None
|
# TODO: use items data when pub_jid is None
|
||||||
ret = []
|
ret = []
|
||||||
for item in items:
|
for item in items:
|
||||||
gbdata = self.item2gbdata(item)
|
gbdata = yield self.item2gbdata(item)
|
||||||
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:
|
||||||
|
|
@ -379,7 +389,7 @@ class GroupBlog(object):
|
||||||
profile_key=client.profile)
|
profile_key=client.profile)
|
||||||
except KeyError:
|
except KeyError:
|
||||||
warning("Missing key for comments")
|
warning("Missing key for comments")
|
||||||
return ret
|
defer.returnValue(ret)
|
||||||
|
|
||||||
def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
|
def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
|
||||||
"""Get the last published microblogs
|
"""Get the last published microblogs
|
||||||
|
|
@ -458,11 +468,13 @@ class GroupBlog(object):
|
||||||
|
|
||||||
mblogs = []
|
mblogs = []
|
||||||
|
|
||||||
|
|
||||||
for jid_s in jids:
|
for jid_s in jids:
|
||||||
_jid = jid.JID(jid_s)
|
_jid = jid.JID(jid_s)
|
||||||
d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(_jid),
|
d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(_jid),
|
||||||
max_items=max_items, profile_key=profile_key)
|
max_items=max_items, profile_key=profile_key)
|
||||||
d.addCallback(lambda items, source_jid: (source_jid, self._itemsConstruction(items, _jid, client)), jid_s)
|
d.addCallback(self._itemsConstruction, _jid, client)
|
||||||
|
d.addCallback(lambda gbdata, source_jid: (source_jid, gbdata), jid_s)
|
||||||
|
|
||||||
mblogs.append(d)
|
mblogs.append(d)
|
||||||
dlist = defer.DeferredList(mblogs)
|
dlist = defer.DeferredList(mblogs)
|
||||||
|
|
|
||||||
|
|
@ -83,6 +83,7 @@ class XEP_0277(object):
|
||||||
|
|
||||||
return (service, node)
|
return (service, node)
|
||||||
|
|
||||||
|
@defer.inlineCallbacks
|
||||||
def item2mbdata(self, item):
|
def item2mbdata(self, item):
|
||||||
"""Convert an XML Item to microblog data used in bridge API
|
"""Convert an XML Item to microblog data used in bridge API
|
||||||
@param item: domish.Element of microblog item
|
@param item: domish.Element of microblog item
|
||||||
|
|
@ -91,11 +92,20 @@ class XEP_0277(object):
|
||||||
entry_elt = [child for child in item.elements() if child.name == "entry"][0]
|
entry_elt = [child for child in item.elements() if child.name == "entry"][0]
|
||||||
except IndexError:
|
except IndexError:
|
||||||
warning(_('No entry element in microblog item'))
|
warning(_('No entry element in microblog item'))
|
||||||
return
|
raise exceptions.DataError('no entry found')
|
||||||
_entry = atom.Entry().import_xml(entry_elt.toXml().encode('utf-8'))
|
_entry = atom.Entry().import_xml(entry_elt.toXml().encode('utf-8'))
|
||||||
microblog_data = {}
|
microblog_data = {}
|
||||||
try:
|
try:
|
||||||
microblog_data['content'] = _entry.title.text
|
try:
|
||||||
|
content_type =_entry.title.attrs['type'].lower()
|
||||||
|
except KeyError:
|
||||||
|
content_type = 'text'
|
||||||
|
if content_type == 'xhtml':
|
||||||
|
# TODO: proper check of body namespace
|
||||||
|
microblog_data['xhtml'] = yield self.host.plugins["TEXT-SYNTAXES"].clean_xhtml(_entry.title.text)
|
||||||
|
microblog_data['content'] = _entry.title.text # FIXME: must use text version of the microblog, or convert XHTML to text if not available
|
||||||
|
else:
|
||||||
|
microblog_data['content'] = _entry.title.text
|
||||||
if len(_entry.authors):
|
if len(_entry.authors):
|
||||||
microblog_data['author'] = _entry.authors[0].name.text
|
microblog_data['author'] = _entry.authors[0].name.text
|
||||||
microblog_data['timestamp'] = str(int(_entry.updated.tf))
|
microblog_data['timestamp'] = str(int(_entry.updated.tf))
|
||||||
|
|
@ -114,7 +124,7 @@ class XEP_0277(object):
|
||||||
|
|
||||||
except (AttributeError, KeyError):
|
except (AttributeError, KeyError):
|
||||||
error(_('Error while parsing atom entry for microblogging event'))
|
error(_('Error while parsing atom entry for microblogging event'))
|
||||||
return {}
|
raise exceptions.DataError
|
||||||
|
|
||||||
##XXX: workaround for Jappix behaviour
|
##XXX: workaround for Jappix behaviour
|
||||||
if not 'author' in microblog_data:
|
if not 'author' in microblog_data:
|
||||||
|
|
@ -126,13 +136,22 @@ class XEP_0277(object):
|
||||||
except:
|
except:
|
||||||
error(_('Cannot find author'))
|
error(_('Cannot find author'))
|
||||||
##end workaround Jappix
|
##end workaround Jappix
|
||||||
return microblog_data
|
|
||||||
|
defer.returnValue(microblog_data)
|
||||||
|
|
||||||
def microblogCB(self, itemsEvent, profile):
|
def microblogCB(self, itemsEvent, profile):
|
||||||
for item in itemsEvent.items:
|
d = defer.Deferred()
|
||||||
microblog_data = self.item2mbdata(item)
|
|
||||||
|
def manageItem(microblog_data):
|
||||||
self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
|
self.host.bridge.personalEvent(itemsEvent.sender.full(), "MICROBLOG", microblog_data, profile)
|
||||||
|
|
||||||
|
for item in itemsEvent.items:
|
||||||
|
d.addCallback(lambda ignore: self.item2mbdata(item))
|
||||||
|
d.addCallback(manageItem)
|
||||||
|
|
||||||
|
d.callback(None)
|
||||||
|
return d
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def data2entry(self, data, profile):
|
def data2entry(self, data, profile):
|
||||||
"""Convert a data dict to en entry usable to create an item
|
"""Convert a data dict to en entry usable to create an item
|
||||||
|
|
@ -188,8 +207,19 @@ class XEP_0277(object):
|
||||||
@param max_items: how many microblogs we want to get
|
@param max_items: how many microblogs we want to get
|
||||||
@param profile_key: profile key
|
@param profile_key: profile key
|
||||||
"""
|
"""
|
||||||
|
def resultToArray(result):
|
||||||
|
ret = []
|
||||||
|
for (success, value) in result:
|
||||||
|
if success:
|
||||||
|
ret.append(value)
|
||||||
|
else:
|
||||||
|
error('Error while getting last microblog')
|
||||||
|
return ret
|
||||||
|
|
||||||
d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
|
d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
|
||||||
d.addCallback(lambda items: map(self.item2mbdata, items))
|
d.addCallback(lambda items: defer.DeferredList(map(self.item2mbdata, items)))
|
||||||
|
d.addCallback(resultToArray)
|
||||||
|
return d
|
||||||
|
|
||||||
def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@'):
|
def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@'):
|
||||||
"""Create a microblog node on PEP with given access
|
"""Create a microblog node on PEP with given access
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue