changeset 310:53adec87d1d7

plugin group blog: group blog subscription
author Goffi <goffi@goffi.org>
date Mon, 11 Apr 2011 12:47:35 +0200
parents f1a3db8ee04a
children 0aa6ca6cdbdd
files src/plugins/plugin_misc_groupblog.py src/plugins/plugin_xep_0060.py src/plugins/plugin_xep_0277.py
diffstat 3 files changed, 46 insertions(+), 13 deletions(-) [+]
line wrap: on
line diff
--- a/src/plugins/plugin_misc_groupblog.py	Sat Apr 09 13:17:31 2011 +0200
+++ b/src/plugins/plugin_misc_groupblog.py	Mon Apr 11 12:47:35 2011 +0200
@@ -34,10 +34,12 @@
 
 MBLOG_COLLECTION = 'MBLOGCOLLECTION'
 CONFIG_NODE = 'CONFIG'
-NS_ACCESS_MODEL = 'pubsub#access_model'
-NS_PERSIST_ITEMS = 'pubsub#persist_items'
-NS_MAX_ITEMS = 'pubsub#max_items'
-NS_NODE_TYPE = 'pubsub#node_type'
+OPT_ACCESS_MODEL = 'pubsub#access_model'
+OPT_PERSIST_ITEMS = 'pubsub#persist_items'
+OPT_MAX_ITEMS = 'pubsub#max_items'
+OPT_NODE_TYPE = 'pubsub#node_type'
+OPT_SUBSCRIPTION_TYPE = 'pubsub#subscription_type'
+OPT_SUBSCRIPTION_DEPTH = 'pubsub#subscription_depth'
 TYPE_COLLECTION = 'collection'
 
 PLUGIN_INFO = {
@@ -85,6 +87,13 @@
                                        'param_1':'text to send',
                                        'param_2':'%(doc_profile)s'
                                      })
+        
+        host.bridge.addMethod("subscribeGroupBlog", ".communication", in_sign='ss', out_sign='',
+                               method=self.subscribeGroupBlog,
+                               doc = { 'summary':"Subscribe to the group blog of somebody",
+                                       'param_0':'jid of the group node published',
+                                       'param_1':'%(doc_profile)s'
+                                     })
 
     def _getRootNode(self, entity):
         return "%(entity)s_%(root_suff)s" % {'entity':entity.userhost(), 'root_suff':MBLOG_COLLECTION}
@@ -105,14 +114,14 @@
     def _configNodeFail(self, failure, errback):
         import pdb
         pdb.set_trace()
-        errback() #FIXME
+        errback("") #FIXME
 
     def _configNodeErr(self, failure, user_jid, pubsub_ent, callback, errback, profile):
         if failure.value.condition == 'item-not-found':
             debug(_('Multiblog config node not found, creating it'))
-            _options = {NS_ACCESS_MODEL:"whitelist", NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1}
+            _options = {OPT_ACCESS_MODEL:"whitelist", OPT_PERSIST_ITEMS:1, OPT_MAX_ITEMS:-1}
             d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, self._getConfigNode(user_jid), _options, profile_key=profile)
-            d.addCallback(self._configNodeCb, callback, profile)
+            d.addCallback(lambda result: self._configNodeCb([] , callback, profile))
             d.addErrback(self._configNodeFail, errback)
         else:
             self._configNodeFail(failure, errback)
@@ -187,7 +196,7 @@
                 error(msg)
                 raise NodeCreationError(msg)
 
-            _options = {NS_NODE_TYPE:TYPE_COLLECTION}
+            _options = {OPT_NODE_TYPE:TYPE_COLLECTION}
             d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, self._getRootNode(user_jid), _options, profile_key=profile)
             d.addCallback(self._createNode, name, user_jid, groups, pubsub_ent, message, profile)
             d.addErrback(err_creating_root_node) 
@@ -204,7 +213,7 @@
         @param pubsub_ent: publish/subscribe service's entity
         @param message: message to publish
         @param profile: profile of the user creating the node"""
-        _options = {NS_ACCESS_MODEL:"roster", NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1,
+        _options = {OPT_ACCESS_MODEL:"roster", OPT_PERSIST_ITEMS:1, OPT_MAX_ITEMS:-1,
                     'pubsub#node_type':'leaf', 'pubsub#collection':self._getRootNode(user_jid),
                     'pubsub#roster_groups_allowed':groups}
         d = self.host.plugins["XEP-0060"].createNode(pubsub_ent, name, _options, profile_key=profile)
@@ -293,3 +302,23 @@
             return
         client.client_initialized.addCallback(after_init)
 
+    def subscribeGroupBlog(self, pub_jid, profile_key='@DEFAULT@'):
+        debug(_('subscribing mblog nodes'))
+        _pub_jid = jid.JID(pub_jid)
+        profile = self.host.memory.getProfileName(profile_key)
+        if not profile:
+            error(_("Unknown profile"))
+            return
+        
+        def after_init(ignore):
+            pubsub_ent = self.host.memory.getServerServiceEntity("pubsub", "service", profile)
+            _options = {OPT_SUBSCRIPTION_TYPE:'items', OPT_SUBSCRIPTION_DEPTH:'1'}
+            d = self.host.plugins["XEP-0060"].subscribe(pubsub_ent, self._getRootNode(_pub_jid), options = _options, profile_key=profile)
+            d.addCallback(lambda x: debug(_("%(publisher)s's group node subscribed [%(profile)s]") % {'publisher':_pub_jid.userhost(), 'profile': profile}))
+            d.addErrback(lambda x: error(_("Can't subscribe group node [%(profile)s]") % {'profile': profile}))
+
+        client = self.host.getClient(profile)
+        if not client:
+            error(_('No client for this profile key: %s') % profile_key)
+            return
+        client.client_initialized.addCallback(after_init)
--- a/src/plugins/plugin_xep_0060.py	Sat Apr 09 13:17:31 2011 +0200
+++ b/src/plugins/plugin_xep_0060.py	Mon Apr 11 12:47:35 2011 +0200
@@ -113,6 +113,10 @@
     def deleteNode(self, service, nodeIdentifier, profile_key='@DEFAULT@'):
         profile,client = self.__getClientNProfile(profile_key, 'delete node')
         return client.deleteNode(service, nodeIdentifier)
+    
+    def subscribe(self, service, nodeIdentifier, sub_jid = None, options=None, profile_key='@DEFAULT@'):
+        profile,client = self.__getClientNProfile(profile_key, 'subscribe node')
+        return client.subscribe(service, nodeIdentifier, sub_jid or client.parent.jid.userhostJID(), options=options) 
 
         
 class SatPubSubClient(pubsub.PubSubClient):
--- a/src/plugins/plugin_xep_0277.py	Sat Apr 09 13:17:31 2011 +0200
+++ b/src/plugins/plugin_xep_0277.py	Mon Apr 11 12:47:35 2011 +0200
@@ -33,9 +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'
+OPT_ACCESS_MODEL = 'pubsub#access_model'
+OPT_PERSIST_ITEMS = 'pubsub#persist_items'
+OPT_MAX_ITEMS = 'pubsub#max_items'
 
 PLUGIN_INFO = {
 "name": "Microblogging over XMPP Plugin",
@@ -159,7 +159,7 @@
         @param profile_key: profile key"""
 
         _jid, xmlstream = self.host.getJidNStream(profile_key)
-        _options = {NS_ACCESS_MODEL:access, NS_PERSIST_ITEMS:1, NS_MAX_ITEMS:-1}
+        _options = {OPT_ACCESS_MODEL:access, OPT_PERSIST_ITEMS:1, OPT_MAX_ITEMS:-1}
         def cb(result):
             #Node is created with right permission
             debug(_("Microblog node has now access %s") % access)