comparison src/plugins/plugin_misc_groupblog.py @ 745:812dc38c0094

plugins groupblog (xep-0060, xep-0277): added blog item modification/deletion
author souliane <souliane@mailoo.org>
date Tue, 10 Dec 2013 09:02:20 +0100
parents 03744d9ebc13
children 64dd7c0f4feb
comparison
equal deleted inserted replaced
744:312a2842b2b8 745:812dc38c0094
85 85
86 host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sassa{ss}s', out_sign='', 86 host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sassa{ss}s', out_sign='',
87 method=self.sendGroupBlog, 87 method=self.sendGroupBlog,
88 async=True) 88 async=True)
89 89
90 host.bridge.addMethod("deleteGroupBlog", ".plugin", in_sign='(sss)ss', out_sign='',
91 method=self.deleteGroupBlog,
92 async=True)
93
94 host.bridge.addMethod("updateGroupBlog", ".plugin", in_sign='(sss)ssa{ss}s', out_sign='',
95 method=self.updateGroupBlog,
96 async=True)
97
90 host.bridge.addMethod("sendGroupBlogComment", ".plugin", in_sign='ssa{ss}s', out_sign='', 98 host.bridge.addMethod("sendGroupBlogComment", ".plugin", in_sign='ssa{ss}s', out_sign='',
91 method=self.sendGroupBlogComment, 99 method=self.sendGroupBlogComment,
92 async=True) 100 async=True)
93 101
94 host.bridge.addMethod("getLastGroupBlogs", ".plugin", 102 host.bridge.addMethod("getLastGroupBlogs", ".plugin",
265 mblog_data['rich'] = extra['rich'] 273 mblog_data['rich'] = extra['rich']
266 P = self.host.plugins["XEP-0060"] 274 P = self.host.plugins["XEP-0060"]
267 access_model_value = ACCESS_TYPE_MAP[access_type] 275 access_model_value = ACCESS_TYPE_MAP[access_type]
268 276
269 if extra.get('allow_comments', 'False').lower() == 'true': 277 if extra.get('allow_comments', 'False').lower() == 'true':
278 # XXX: use the item identifier? http://bugs.goffi.org/show_bug.cgi?id=63
270 comments_node = "%s_%s__%s" % (NS_COMMENT_PREFIX, str(uuid.uuid4()), node_name) 279 comments_node = "%s_%s__%s" % (NS_COMMENT_PREFIX, str(uuid.uuid4()), node_name)
271 mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': service.userhost(), 280 mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': service.userhost(),
272 'query': urllib.urlencode([('node',comments_node.encode('utf-8'))])} 281 'query': urllib.urlencode([('node',comments_node.encode('utf-8'))])}
273 _options = {P.OPT_ACCESS_MODEL: access_model_value, 282 _options = {P.OPT_ACCESS_MODEL: access_model_value,
274 P.OPT_PERSIST_ITEMS: 1, 283 P.OPT_PERSIST_ITEMS: 1,
343 elif access_type == "JID": 352 elif access_type == "JID":
344 raise NotImplementedError 353 raise NotImplementedError
345 else: 354 else:
346 error(_("Unknown access type")) 355 error(_("Unknown access type"))
347 raise BadAccessTypeError 356 raise BadAccessTypeError
357
358 return self._initialise(profile_key).addCallback(initialised)
359
360 def deleteGroupBlog(self, pub_data, comments, profile_key='@NONE@'):
361 """Delete a microblog item from a node.
362 @param pub_data: a tuple (service, comment node identifier, item identifier)
363 @param comments: comments node identifier (for main item) or empty string
364 @param profile_key: %(doc_profile_key)s
365 """
366
367 def initialised(result):
368 profile, client = result
369 service, node, item_id = pub_data
370 if comments:
371 # remove the associated comments node
372 d = self.host.plugins["XEP-0060"].deleteNode(jid.JID(service), node, profile_key=profile)
373 d.addErrback(lambda failure: error("Deletion of node %s failed: %s" % (node, failure.getErrorMessage())))
374 node = self.getNodeName(client.jid)
375 # remove the item itself
376 d = self.host.plugins["XEP-0060"].retractItems(jid.JID(service), node, [item_id], profile_key=profile)
377 d.addErrback(lambda failure: error("Deletion of item %s from %s failed: %s" % (item_id, node, failure.getErrorMessage())))
378 return d
379
380 def notify(d):
381 # TODO: this works only on the same host, and notifications for item deletion should be
382 # implemented according to http://xmpp.org/extensions/xep-0060.html#publisher-delete-success-notify
383 # instead. The notification mechanisms implemented in sat_pubsub and wokkel seem not compatible,
384 # see wokkel.pubsub.PubSubClient._onEvent_items and sat_pubsub.backend._doNotifyRetraction
385 service, node, item_id = pub_data
386 publisher = self.host.getJidNStream(profile_key)[0]
387 profile = self.host.memory.getProfileName(profile_key)
388 gbdatum = {'id': item_id, 'type': 'main_item' if comments else 'comment'}
389 self.host.bridge.personalEvent(publisher.full(), "MICROBLOG_DELETE", gbdatum, profile)
390 return d
391
392 return self._initialise(profile_key).addCallback(initialised).addCallback(notify)
393
394 def updateGroupBlog(self, pub_data, comments, message, extra, profile_key='@NONE@'):
395 """Modify a microblog node
396 @param pub_data: a tuple (service, comment node identifier, item identifier)
397 @param comments: comments node identifier (for main item) or empty string
398 @param message: new message
399 @param extra: dict which option name as key, which can be:
400 - allow_comments: True to accept an other level of comments, False else (default: False)
401 - rich: if present, contain rich text in currently selected syntax
402 @param profile_key: %(doc_profile)
403 """
404
405 def initialised(result):
406 profile, client = result
407 mblog_data = {'content': message}
408 if 'rich' in extra:
409 mblog_data['rich'] = extra['rich']
410 service, node, item_id = pub_data
411 if comments:
412 node = self.getNodeName(client.jid)
413 mblog_data['id'] = str(item_id)
414 if extra.get('allow_comments', 'False').lower() == 'true':
415 comments_node = self.host.plugins["XEP-0277"].parseCommentUrl(comments)[1]
416 # we could use comments_node directly but it's safer to rebuild it
417 # XXX: use the item identifier? http://bugs.goffi.org/show_bug.cgi?id=63
418 hash_ = comments_node.split('_')[1].split('__')[0]
419 comments_node = "%s_%s__%s" % (NS_COMMENT_PREFIX, hash_, node)
420 mblog_data['comments'] = "xmpp:%(service)s?%(query)s" % {'service': jid.JID(service).userhost(),
421 'query': urllib.urlencode([('node', comments_node.encode('utf-8'))])}
422 entry_d = self.host.plugins["XEP-0277"].data2entry(mblog_data, profile)
423 entry_d.addCallback(lambda mblog_item: self.host.plugins["XEP-0060"].publish(jid.JID(service), node, items=[mblog_item], profile_key=profile))
424 entry_d.addErrback(lambda failure: error("Modification of %s failed: %s" % (pub_data, failure.getErrorMessage())))
425 return entry_d
348 426
349 return self._initialise(profile_key).addCallback(initialised) 427 return self._initialise(profile_key).addCallback(initialised)
350 428
351 def sendGroupBlogComment(self, node_url, message, extra, profile_key='@NONE@'): 429 def sendGroupBlogComment(self, node_url, message, extra, profile_key='@NONE@'):
352 """Publish a comment in the given node 430 """Publish a comment in the given node