Mercurial > libervia-backend
comparison src/plugins/plugin_misc_groupblog.py @ 1233:0b87d029f0a3
plugin XEP-0277, groupblog: fixes namespace issue of the items that are received from an event + trap some errors
author | souliane <souliane@mailoo.org> |
---|---|
date | Mon, 06 Oct 2014 17:25:41 +0200 |
parents | 318eab3f93f8 |
children | b4a264915ea9 |
comparison
equal
deleted
inserted
replaced
1232:6b10442e8920 | 1233:0b87d029f0a3 |
---|---|
210 d = self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier) | 210 d = self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier) |
211 d.addCallback(gbdataManagementComments) | 211 d.addCallback(gbdataManagementComments) |
212 return False | 212 return False |
213 return True | 213 return True |
214 | 214 |
215 @defer.inlineCallbacks | |
216 def _handleCommentsItems(self, items, service, node_identifier): | 215 def _handleCommentsItems(self, items, service, node_identifier): |
217 """ Convert comments items to groupblog data, and send them as signals | 216 """ Convert comments items to groupblog data, and send them as signals |
218 @param items: comments items | 217 @param items: comments items |
219 @param service: jid of the PubSub service used | 218 @param service: jid of the PubSub service used |
220 @param node_identifier: comments node | 219 @param node_identifier: comments node |
221 @return: list of group blog data | 220 @return: deferred list of group blog data |
222 """ | 221 """ |
223 ret = [] | 222 d_list = [] |
224 for item in items: | 223 |
224 def cb(microblog_data): | |
225 publisher = "" # FIXME: publisher attribute for item in SàT pubsub is not managed yet, so | 225 publisher = "" # FIXME: publisher attribute for item in SàT pubsub is not managed yet, so |
226 # publisher is not checked and can be easily spoofed. This need to be fixed | 226 # publisher is not checked and can be easily spoofed. This need to be fixed |
227 # quickly. | 227 # quickly. |
228 microblog_data = yield self.item2gbdata(item, "comment") | |
229 microblog_data["service"] = service.userhost() | 228 microblog_data["service"] = service.userhost() |
230 microblog_data["node"] = node_identifier | 229 microblog_data["node"] = node_identifier |
231 microblog_data["verified_publisher"] = "true" if publisher else "false" | 230 microblog_data["verified_publisher"] = "true" if publisher else "false" |
232 ret.append(microblog_data) | 231 return microblog_data |
233 defer.returnValue(ret) | 232 |
233 for item in items: | |
234 d_list.append(self.item2gbdata(item, "comment").addCallback(cb)) | |
235 return defer.DeferredList(d_list, consumeErrors=True).addCallback(lambda result: [value for (success, value) in result if success]) | |
234 | 236 |
235 def _parseAccessData(self, microblog_data, item): | 237 def _parseAccessData(self, microblog_data, item): |
236 P = self.host.plugins["XEP-0060"] | 238 P = self.host.plugins["XEP-0060"] |
237 form_elts = [child for child in item.elements() if child.name == "x"] | 239 form_elts = [child for child in item.elements() if child.name == "x"] |
238 for form_elt in form_elts: | 240 for form_elt in form_elts: |
475 entry_d.addCallback(lambda mblog_item: self.host.plugins["XEP-0060"].publish(service, node, items=[mblog_item], profile_key=profile)) | 477 entry_d.addCallback(lambda mblog_item: self.host.plugins["XEP-0060"].publish(service, node, items=[mblog_item], profile_key=profile)) |
476 return entry_d | 478 return entry_d |
477 | 479 |
478 return self._initialise(profile_key).addCallback(initialised) | 480 return self._initialise(profile_key).addCallback(initialised) |
479 | 481 |
480 | |
481 @defer.inlineCallbacks | |
482 def _itemsConstruction(self, items, pub_jid, client): | 482 def _itemsConstruction(self, items, pub_jid, client): |
483 """ Transforms items to group blog data and manage comments node | 483 """ Transforms items to group blog data and manage comments node |
484 | 484 |
485 @param items: iterable of items | 485 @param items: iterable of items |
486 @param pub_jid: jid of the publisher or None to use items data | 486 @param pub_jid: jid of the publisher or None to use items data |
487 @param client: SatXMPPClient instance | 487 @param client: SatXMPPClient instance |
488 @return: deferred which fire list of group blog data """ | 488 @return: deferred which fire list of group blog data """ |
489 # TODO: use items data when pub_jid is None | 489 # TODO: use items data when pub_jid is None |
490 ret = [] | 490 d_list = [] |
491 for item in items: | 491 |
492 gbdata = yield self.item2gbdata(item) | 492 @defer.inlineCallbacks |
493 def cb(gbdata): | |
493 try: | 494 try: |
494 gbdata['service'] = client.item_access_pubsub.full() | 495 gbdata['service'] = client.item_access_pubsub.full() |
495 except AttributeError: | 496 except AttributeError: |
496 pass | 497 log.warning(_("Pubsub service is unknown for blog entry %s") % gbdata['id']) |
497 ret.append(gbdata) | |
498 # every comments node must be subscribed, except if we are the publisher (we are already subscribed in this case) | 498 # every comments node must be subscribed, except if we are the publisher (we are already subscribed in this case) |
499 if "comments_node" in gbdata and pub_jid.userhostJID() != client.jid.userhostJID(): | 499 if "comments_node" in gbdata and pub_jid.userhostJID() != client.jid.userhostJID(): |
500 try: | 500 try: |
501 service = jid.JID(gbdata["comments_service"]) | 501 service = jid.JID(gbdata["comments_service"]) |
502 node = gbdata["comments_node"] | 502 node = gbdata["comments_node"] |
503 except KeyError: | 503 except KeyError: |
504 log.warning("Missing key for comments") | 504 log.error(_("Missing key for blog comment %s") % gbdata['id']) |
505 continue | 505 defer.returnValue(gbdata) |
506 # TODO: see if it is really needed to check for not subscribing twice to the node | 506 # TODO: see if it is really needed to check for not subscribing twice to the node |
507 # It previously worked without this check, but the pubsub service logs were polluted | 507 # It previously worked without this check, but the pubsub service logs were polluted |
508 # or, if in debug mode, it made sat-pubsub very difficult to debug. | 508 # or, if in debug mode, it made sat-pubsub very difficult to debug. |
509 subscribed_nodes = yield self.host.plugins['XEP-0060'].listSubscribedNodes(service, profile=client.profile) | 509 subscribed_nodes = yield self.host.plugins['XEP-0060'].listSubscribedNodes(service, profile=client.profile) |
510 if node not in subscribed_nodes: # avoid sat-pubsub "SubscriptionExists" error | 510 if node not in subscribed_nodes: # avoid sat-pubsub "SubscriptionExists" error |
511 self.host.plugins["XEP-0060"].subscribe(service, node, profile_key=client.profile) | 511 self.host.plugins["XEP-0060"].subscribe(service, node, profile_key=client.profile) |
512 defer.returnValue(ret) | 512 defer.returnValue(gbdata) |
513 | |
514 for item in items: | |
515 d_list.append(self.item2gbdata(item).addCallback(cb)) | |
516 return defer.DeferredList(d_list, consumeErrors=True).addCallback(lambda result: [value for (success, value) in result if success]) | |
513 | 517 |
514 def __getGroupBlogs(self, pub_jid_s, max_items=10, item_ids=None, profile_key=C.PROF_KEY_NONE): | 518 def __getGroupBlogs(self, pub_jid_s, max_items=10, item_ids=None, profile_key=C.PROF_KEY_NONE): |
515 """Retrieve previously published items from a publish subscribe node. | 519 """Retrieve previously published items from a publish subscribe node. |
516 @param pub_jid_s: jid of the publisher | 520 @param pub_jid_s: jid of the publisher |
517 @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7) | 521 @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7) |