comparison src/plugins/plugin_misc_groupblog.py @ 621:0e16288d6816

pluging groupblog: comments handling: - comments are no more automaticly requested and sent as events - a new bridge method "getGroupBlogComments" allow to request them
author Goffi <goffi@goffi.org>
date Thu, 20 Jun 2013 12:14:43 +0200
parents 1de01a2154e2
children 69a8bfd266a5
comparison
equal deleted inserted replaced
620:64db6758d223 621:0e16288d6816
99 host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin", 99 host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin",
100 in_sign='sasis', out_sign='a{saa{ss}}', 100 in_sign='sasis', out_sign='a{saa{ss}}',
101 method=self.getMassiveLastGroupBlogs, 101 method=self.getMassiveLastGroupBlogs,
102 async=True) 102 async=True)
103 103
104 host.bridge.addMethod("getGroupBlogComments", ".plugin",
105 in_sign='sss', out_sign='aa{ss}',
106 method=self.getGroupBlogComments,
107 async=True)
108
104 host.bridge.addMethod("subscribeGroupBlog", ".plugin", in_sign='ss', out_sign='', 109 host.bridge.addMethod("subscribeGroupBlog", ".plugin", in_sign='ss', out_sign='',
105 method=self.subscribeGroupBlog, 110 method=self.subscribeGroupBlog,
106 async=True) 111 async=True)
107 112
108 host.bridge.addMethod("massiveSubscribeGroupBlogs", ".plugin", in_sign='sass', out_sign='', 113 host.bridge.addMethod("massiveSubscribeGroupBlogs", ".plugin", in_sign='sass', out_sign='',
178 self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", gbdata, profile) 183 self.host.bridge.personalEvent(publisher.full(), "MICROBLOG", gbdata, profile)
179 return False 184 return False
180 185
181 elif event.nodeIdentifier.startswith(NS_COMMENT_PREFIX): 186 elif event.nodeIdentifier.startswith(NS_COMMENT_PREFIX):
182 # Comment 187 # Comment
183 self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier, profile) 188 for microblog_data in self._handleCommentsItems(event.items, event.sender, event.nodeIdentifier):
189 publisher = None # FIXME: see below (_handleCommentsItems)
190 self.host.bridge.personalEvent(publisher.full() if publisher else microblog_data["author"], "MICROBLOG", microblog_data, profile)
184 return False 191 return False
185 return True 192 return True
186 193
187 def _handleCommentsItems(self, items, service, node_identifier, profile): 194 def _handleCommentsItems(self, items, service, node_identifier):
188 """ Convert comments items to groupblog data, and send them as signals 195 """ Convert comments items to groupblog data, and send them as signals
189 @param items: comments items 196 @param items: comments items
190 @param service: jid of the PubSub service used 197 @param service: jid of the PubSub service used
191 @param node_identifier: comments node 198 @param node_identifier: comments node
192 @param profile: %(doc_profile)s 199 @return: list of group blog data
193 """ 200 """
201 ret = []
194 for item in items: 202 for item in items:
195 publisher = "" # FIXME: publisher attribute for item in SàT pubsub is not managed yet, so 203 publisher = "" # FIXME: publisher attribute for item in SàT pubsub is not managed yet, so
196 # publisher is not checked and can be easily spoofed. This need to be fixed 204 # publisher is not checked and can be easily spoofed. This need to be fixed
197 # quickly. 205 # quickly.
198 microblog_data = self.item2gbdata(item, "comment") 206 microblog_data = self.item2gbdata(item, "comment")
199 microblog_data["service"] = service.userhost() 207 microblog_data["service"] = service.userhost()
200 microblog_data["node"] = node_identifier 208 microblog_data["node"] = node_identifier
201 microblog_data["verified_publisher"] = "true" if publisher else "false" 209 microblog_data["verified_publisher"] = "true" if publisher else "false"
202 210 ret.append(microblog_data)
203 self.host.bridge.personalEvent(publisher.full() if publisher else microblog_data["author"], "MICROBLOG", microblog_data, profile) 211 return ret
204 212
205 def _parseAccessData(self, microblog_data, item): 213 def _parseAccessData(self, microblog_data, item):
206 P = self.host.plugins["XEP-0060"] 214 P = self.host.plugins["XEP-0060"]
207 form_elts = filter(lambda elt: elt.name == "x", item.children) 215 form_elts = filter(lambda elt: elt.name == "x", item.children)
208 for form_elt in form_elts: 216 for form_elt in form_elts:
321 error(_("Unknown access type")) 329 error(_("Unknown access type"))
322 raise BadAccessTypeError 330 raise BadAccessTypeError
323 331
324 self.initialise(profile_key).addCallback(initialised) 332 self.initialise(profile_key).addCallback(initialised)
325 333
326 def sendGroupBlogComment(self, node_url, message, profile_key='@DEFAULT@'): 334 def sendGroupBlogComment(self, node_url, message, profile_key='@NONE@'):
327 """Publish a comment in the given node 335 """Publish a comment in the given node
328 @param node url: link to the comments node as specified in XEP-0277 and given in microblog data's comments key 336 @param node url: link to the comments node as specified in XEP-0277 and given in microblog data's comments key
329 @param message: comment 337 @param message: comment
330 @profile_key: %(doc_profile)s 338 @profile_key: %(doc_profile)s
331 """ 339 """
342 return self.host.plugins["XEP-0060"].publish(service, node, items=[mblog_item], profile_key=profile) 350 return self.host.plugins["XEP-0060"].publish(service, node, items=[mblog_item], profile_key=profile)
343 351
344 def _itemsConstruction(self, items, pub_jid, client): 352 def _itemsConstruction(self, items, pub_jid, client):
345 """ Transforms items to group blog data and manage comments node 353 """ Transforms items to group blog data and manage comments node
346 @param items: iterable of items 354 @param items: iterable of items
347 @param pub_jib: jid of the publisher 355 @param pub_jid: jid of the publisher or None to use items data
348 @param client: SatXMPPClient instance 356 @param client: SatXMPPClient instance
349 @return: list of group blog data """ 357 @return: list of group blog data """
358 # TODO: use items data when pub_jid is None
350 ret = [] 359 ret = []
351 for item in items: 360 for item in items:
352 gbdata = self.item2gbdata(item) 361 gbdata = self.item2gbdata(item)
353 ret.append(gbdata) 362 ret.append(gbdata)
354 # if there is a comments node, we subscribe to it 363 # if there is a comments node, we subscribe to it
356 try: 365 try:
357 # every comments node must be subscribed, except if we are the publisher (we are already subscribed in this case) 366 # every comments node must be subscribed, except if we are the publisher (we are already subscribed in this case)
358 if pub_jid.userhostJID() != client.jid.userhostJID(): 367 if pub_jid.userhostJID() != client.jid.userhostJID():
359 self.host.plugins["XEP-0060"].subscribe(jid.JID(gbdata["comments_service"]), gbdata["comments_node"], 368 self.host.plugins["XEP-0060"].subscribe(jid.JID(gbdata["comments_service"]), gbdata["comments_node"],
360 profile_key=client.profile) 369 profile_key=client.profile)
361 # we get all comments of the node, and handle them
362 defer_getItems = self.host.plugins["XEP-0060"].getItems(jid.JID(gbdata["comments_service"]), gbdata["comments_node"], profile_key=client.profile)
363 defer_getItems.addCallback(self._handleCommentsItems, jid.JID(gbdata["comments_service"]), gbdata["comments_node"], client.profile)
364 except KeyError: 370 except KeyError:
365 warning("Missing key for comments") 371 warning("Missing key for comments")
366 return ret 372 return ret
367 373
368 def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@DEFAULT@'): 374 def getLastGroupBlogs(self, pub_jid_s, max_items=10, profile_key='@NONE@'):
369 """Get the last published microblogs 375 """Get the last published microblogs
370 @param pub_jid_s: jid of the publisher 376 @param pub_jid_s: jid of the publisher
371 @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7) 377 @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7)
372 @param profile_key: profile key 378 @param profile_key: profile key
373 @return: list of microblog data (dict) 379 @return: list of microblog data (dict)
383 return d 389 return d
384 390
385 #TODO: we need to use the server corresponding the the host of the jid 391 #TODO: we need to use the server corresponding the the host of the jid
386 return self.initialise(profile_key).addCallback(initialised) 392 return self.initialise(profile_key).addCallback(initialised)
387 393
388 def getMassiveLastGroupBlogs(self, publishers_type, publishers, max_items=10, profile_key='@DEFAULT@'): 394 def getGroupBlogComments(self, service_s, node, profile_key='@NONE@'):
395 """Get all comments of given node
396 @param service_s: service hosting the node
397 @param node: comments node
398 @param profile_key: profile key
399 @return: list of microblog data (dict)
400 """
401 service = jid.JID(service_s)
402
403 def initialised(result):
404 profile, client = result
405 d = self.host.plugins["XEP-0060"].getItems(service, node,
406 profile_key=profile_key)
407 d.addCallback(self._handleCommentsItems, service, node)
408 d.addErrback(lambda ignore: {}) # TODO: more complete error management (log !)
409 return d
410
411 #TODO: we need to use the server corresponding the the host of the jid
412 return self.initialise(profile_key).addCallback(initialised)
413
414 def getMassiveLastGroupBlogs(self, publishers_type, publishers, max_items=10, profile_key='@NONE@'):
389 """Get the last published microblogs for a list of groups or jids 415 """Get the last published microblogs for a list of groups or jids
390 @param publishers_type: type of the list of publishers (one of "GROUP" or "JID" or "ALL") 416 @param publishers_type: type of the list of publishers (one of "GROUP" or "JID" or "ALL")
391 @param publishers: list of publishers, according to "publishers_type" (list of groups or list of jids) 417 @param publishers: list of publishers, according to "publishers_type" (list of groups or list of jids)
392 @param max_items: how many microblogs we want to get 418 @param max_items: how many microblogs we want to get
393 @param profile_key: profile key 419 @param profile_key: profile key
440 if publishers_type == "ALL" and publishers: 466 if publishers_type == "ALL" and publishers:
441 raise Exception("Publishers list must be empty when getting microblogs for all contacts") 467 raise Exception("Publishers list must be empty when getting microblogs for all contacts")
442 return self.initialise(profile_key).addCallback(initialised) 468 return self.initialise(profile_key).addCallback(initialised)
443 #TODO: we need to use the server corresponding the the host of the jid 469 #TODO: we need to use the server corresponding the the host of the jid
444 470
445 def subscribeGroupBlog(self, pub_jid, profile_key='@DEFAULT@'): 471 def subscribeGroupBlog(self, pub_jid, profile_key='@NONE@'):
446 def initialised(result): 472 def initialised(result):
447 profile, client = result 473 profile, client = result
448 d = self.host.plugins["XEP-0060"].subscribe(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)), 474 d = self.host.plugins["XEP-0060"].subscribe(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)),
449 profile_key=profile_key) 475 profile_key=profile_key)
450 return d 476 return d
451 477
452 #TODO: we need to use the server corresponding the the host of the jid 478 #TODO: we need to use the server corresponding the the host of the jid
453 return self.initialise(profile_key).addCallback(initialised) 479 return self.initialise(profile_key).addCallback(initialised)
454 480
455 def massiveSubscribeGroupBlogs(self, publishers_type, publishers, profile_key='@DEFAULT@'): 481 def massiveSubscribeGroupBlogs(self, publishers_type, publishers, profile_key='@NONE@'):
456 """Subscribe microblogs for a list of groups or jids 482 """Subscribe microblogs for a list of groups or jids
457 @param publishers_type: type of the list of publishers (one of "GROUP" or "JID" or "ALL") 483 @param publishers_type: type of the list of publishers (one of "GROUP" or "JID" or "ALL")
458 @param publishers: list of publishers, according to "publishers_type" (list of groups or list of jids) 484 @param publishers: list of publishers, according to "publishers_type" (list of groups or list of jids)
459 @param profile_key: profile key 485 @param profile_key: profile key
460 """ 486 """