plugin XEP-0277, groupblog: rich text management for receiving microblogs

This commit is contained in:
Goffi 2013-11-14 18:36:02 +01:00
commit d6e7ec0055
2 changed files with 63 additions and 21 deletions

View file

@ -176,22 +176,30 @@ class GroupBlog(object):
or event_host[-len(origin_host):] != origin_host):
warning("Host incoherence between %s and %s (hack attempt ?)" % (unicode(event.sender),
unicode(publisher)))
return
return False
client = self.host.getClient(profile)
for gbdata in self._itemsConstruction(event.items, publisher, client):
self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", gbdata, profile)
def gbdataManagementMicroblog(gbdata):
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
elif event.nodeIdentifier.startswith(NS_COMMENT_PREFIX):
# Comment
for microblog_data in self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier):
publisher = None # FIXME: see below (_handleCommentsItems)
self.host.bridge.personalEvent(publisher.full() if publisher else microblog_data["author"], "MICROBLOG", microblog_data, profile)
def gbdataManagementComments(gbdata):
for gbdatum in gbdata:
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 True
@defer.inlineCallbacks
def _handleCommentsItems(self, items, service, node_identifier):
""" Convert comments items to groupblog data, and send them as signals
@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 is not checked and can be easily spoofed. This need to be fixed
# quickly.
microblog_data = self.item2gbdata(item, "comment")
microblog_data = yield self.item2gbdata(item, "comment")
microblog_data["service"] = service.userhost()
microblog_data["node"] = node_identifier
microblog_data["verified_publisher"] = "true" if publisher else "false"
ret.append(microblog_data)
return ret
defer.returnValue(ret)
def _parseAccessData(self, microblog_data, item):
P = self.host.plugins["XEP-0060"]
@ -228,12 +236,13 @@ class GroupBlog(object):
break
@defer.inlineCallbacks
def item2gbdata(self, item, _type="main_item"):
""" 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
self._parseAccessData(microblog_data, item)
return microblog_data
defer.returnValue(microblog_data)
def getNodeName(self, publisher):
"""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))
return entry_d
@defer.inlineCallbacks
def _itemsConstruction(self, items, pub_jid, client):
""" Transforms items to group blog data and manage comments node
@param items: iterable of items
@param pub_jid: jid of the publisher or None to use items data
@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
ret = []
for item in items:
gbdata = self.item2gbdata(item)
gbdata = yield self.item2gbdata(item)
ret.append(gbdata)
# if there is a comments node, we subscribe to it
if "comments_node" in gbdata:
@ -379,7 +389,7 @@ class GroupBlog(object):
profile_key=client.profile)
except KeyError:
warning("Missing key for comments")
return ret
defer.returnValue(ret)
def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
"""Get the last published microblogs
@ -458,11 +468,13 @@ class GroupBlog(object):
mblogs = []
for jid_s in jids:
_jid = jid.JID(jid_s)
d = self.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, self.getNodeName(_jid),
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)
dlist = defer.DeferredList(mblogs)