diff src/plugins/plugin_xep_0277.py @ 303:2b52a5da0978

plugin XEP_0277: microblog access model can now be changed plugin XEP_0060: added some method to manage pubsub nodes
author Goffi <goffi@goffi.org>
date Thu, 24 Mar 2011 21:15:26 +0100
parents 9f3a6cf91668
children e04ccf122bb6
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0277.py	Mon Feb 21 01:38:16 2011 +0100
+++ b/src/plugins/plugin_xep_0277.py	Thu Mar 24 21:15:26 2011 +0100
@@ -33,6 +33,9 @@
 from time import time
 
 NS_MICROBLOG = 'urn:xmpp:microblog:0'
+NS_ACCESS_MODEL = 'pubsub#access_model'
+NS_PERSIST_ITEMS = 'pubsub#persist_items'
+NS_MAX_ITEMS = 'pubsub#max_items'
 
 PLUGIN_INFO = {
 "name": "Microblogging over XMPP Plugin",
@@ -61,6 +64,10 @@
                                       'param_2':'%(doc_profile)s',
                                       'return':'list of microblog data (dict)'
                                     })
+        host.bridge.addMethod("setMicroblogAccess", ".communication", in_sign='ss', out_sign='',
+                               method=self.setMicroblogAccess,
+                               doc = {
+                                     })
 
     def _item2mbdata(self, item):
         """Convert an XML Item to microblog data used in bridge API
@@ -124,10 +131,47 @@
         self.host.plugins["XEP-0060"].publish(None, NS_MICROBLOG, [item], profile_key = profile)
         return 0
 
-    def getLastMicroblogs(self, pub_jid, max_items=1, profile_key='@DEFAULT@', callback=None, errback=None):
+    def getLastMicroblogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@', callback=None, errback=None):
+        """Get the last published microblogs
+        @param pub_jid: jid of the publisher
+        @param max_items: how many microblogs we wants to get
+        @param profile_key: profile key
+        @param callback: used for the async answer
+        @param errback: used for the async answer
+        """
         assert(callback)
         d = self.host.plugins["XEP-0060"].getItems(jid.JID(pub_jid), NS_MICROBLOG, max_items=max_items, profile_key=profile_key)
         d.addCallbacks(lambda items: callback(map(self._item2mbdata, items)), errback)
         
+    def setMicroblogAccess(self, access="presence", profile_key='@DEFAULT@'):
+        """Create a microblog node on PEP with given access
+        If the node already exists, it is deleted and recreated
+        @param access: Node access model, according to xep-0060 #4.5
+        @param profile_key: profile key"""
+
+        jid, xmlstream = self.host.getJidNStream(profile_key)
+        def cb(result):
+            #Node is created with right permission
+            debug(_("Microblog node created"))
+
+        def fatal_err(s_error):
+            #Something went wrong
+            error(_("Can't set microblog access"))
+
+        def err_cb(s_error):
+            #If the node already exists, the condition is "conflict",
+            #else we have an unmanaged error
+            if s_error.value.condition=='conflict':
+                d = self.host.plugins["XEP-0060"].deleteNode(jid.userhostJID(), NS_MICROBLOG, profile_key=profile_key)
+                d.addCallback(lambda x: create_node().addCallback(cb).addErrback(fatal_err))
+                d.addErrback(fatal_err)
+            else:
+                fatal_err(s_error)
+        
+        def create_node():
+            return self.host.plugins["XEP-0060"].createNode(jid.userhostJID(), NS_MICROBLOG, {NS_ACCESS_MODEL:access, NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1}, profile_key=profile_key)
+
+        create_node().addCallback(cb).addErrback(err_cb)
 
         
+