Changeset 693

Show
Ignore:
Timestamp:
06/18/08 15:20:48 (2 months ago)
Author:
sylvain
Message:

Better support for pubsub events

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • oss/headstock/headstock/example/microblog/microblog/jabber/client.py

    r688 r693  
    189189                    for body in m.bodies: 
    190190                        message = remove_BOM(body.plain_body).strip() 
    191                         print repr(message) 
    192191                        if message == 'help': 
    193192                            m = Message(self.from_jid, m.from_jid) 
     
    206205                            self.send(m, 'outbox') 
    207206                        else: 
     207                            print "Received message: %s" % repr(message) 
    208208                            try: 
    209209                                action, data = message.split(' ', 1) 
     
    304304                self.from_jid = self.recv('jid') 
    305305 
    306                 if '.microblogging' not in unicode(self.from_jid): 
    307                     sibling = JID(unicode('%s.microblogging' % self.from_jid.node), 
    308                                   self.from_jid.domain) 
    309                     p = Presence(from_jid=self.from_jid, to_jid=unicode(sibling), 
    310                                  type=u'subscribe') 
    311                     self.send(p, "outbox") 
     306                #if '.microblogging' in unicode(self.from_jid): 
     307                #    sibling = JID(unicode('%s.microblogging' % self.from_jid.node), 
     308                #                  self.from_jid.domain) 
     309                #    p = Presence(from_jid=self.from_jid, to_jid=unicode(sibling), 
     310                #                 type=u'subscribe') 
     311                #    self.send(p, "outbox") 
    312312 
    313313            if self.dataReady("subscribe"): 
     
    697697                                           ("discodisp",'subscription.outbox'):('xmpp','forward'), 
    698698                                           ("discodisp",'affiliation.outbox'):('xmpp','forward'), 
    699                                            ("discodisp",'out.subscription.result'):('discohandler','subscriptions.result'), 
    700                                            ("discodisp",'out.affiliation.result'):('discohandler','affiliations.result'), 
     699                                           ("discodisp",'out.subscription.result'): ('discohandler','subscriptions.result'), 
     700                                           ("discodisp",'out.affiliation.result'): ('discohandler','affiliations.result'), 
    701701                                           ("discodisp", 'out.items.result'): ('discohandler', 'items.result'), 
    702702                                           ("discodisp", 'out.items.error'): ('discohandler', 'items.error'), 
     
    740740                                           ("pubsubdisp", "retract.outbox"): ("xmpp", "forward"), 
    741741                                           ("pubsubdisp", "out.create.result"): ("discohandler", "created"), 
     742                                           ("pubsubdisp", "out.subscribe.result"): ("discohandler", "subscribed"), 
    742743                                           ("pubsubdisp", "out.delete.result"): ("discohandler", "deleted"), 
    743744                                           ("pubsubdisp", "out.create.error"): ("discohandler", "error"), 
    744745                                           ("pubsubdisp", "out.delete.error"): ("discohandler", "error"), 
     746                                           ("pubsubdisp", "out.publish.error"): ("itemshandler", "publish.error"), 
     747                                           ("pubsubdisp", "out.retract.error"): ("itemshandler", "retract.error"), 
     748                                           ("pubsubdisp", "out.publish.result"): ("itemshandler", "published"), 
    745749                                           ("pubsubdisp", "out.message"): ('pubsubmsgeventhandler', 'inbox'), 
    746750                                           ('itemshandler', 'publish'): ('pubsubdisp', 'publish.forward'), 
     
    784788            if self.dataReady("unhandled"): 
    785789                msg = self.recv('unhandled') 
    786                 #self.send(('UNHANDLED', msg), 'log') 
     790                self.send(('UNHANDLED', msg), 'log') 
    787791                 
    788792            if self.dataReady("inbox"): 
  • oss/headstock/headstock/example/microblog/microblog/jabber/pubsub.py

    r688 r693  
    2828 
    2929publish_item_rx = re.compile(r'\[(.*)\] ([\w ]*)') 
     30retract_item_rx = re.compile(r'\[(.*)\] ([\w:\-]*)') 
    3031 
    3132 
     
    4445               "dounsubscribe" : "", 
    4546               "dosubscribe" : "", 
     47               "subscribed": "", 
    4648               "subscriptions.result": "", 
    4749               "affiliations.result": "", 
     
    7072        self.session_id = session_id 
    7173        self.profile = profile 
    72         if profile: 
    73             self.collection = self.atompub.get_collection(profile.username) 
     74        self._collection = None 
    7475        self.pubsub_top_level_node = u'home/%s/%s' % (self.xmpphost, self.from_jid.node) 
     76 
     77    @property 
     78    def collection(self): 
     79        # Lazy loading of collection 
     80        if not self._collection: 
     81            self._collection = self.atompub.get_collection(self.profile.username) 
     82        return self._collection 
    7583 
    7684    def initComponents(self): 
     
    139147 
    140148            if self.dataReady('items.result'): 
    141                 items_disco = self.recv('items.result') 
    142                 print "%s has %d item(s)" % (items_disco.node_name, len(items_disco.items)) 
    143                  
    144                 p = Node(unicode(self.from_jid), u'pubsub.%s' % self.xmpphost, 
    145                          node_name=self.pubsub_top_level_node, sub_jid=self.from_jid.nodeid()) 
    146                 self.send(p, "subscribe-node") 
    147  
    148                 for item in items_disco.items: 
    149                     p = Node(unicode(self.from_jid), u'pubsub.%s' % self.xmpphost, 
    150                              node_name=self.pubsub_top_level_node, sub_jid=self.from_jid.nodeid()) 
    151                     self.send(p, "subscribe-node") 
    152                      
    153                 #        n = ItemsDiscovery(unicode(self.from_jid), u'pubsub.%s' % self.xmpphost,  
    154                 #                           node_name=item.node) 
    155                 #        self.send(n, "items-disco") 
    156                 #        yield 1 
     149                items = self.recv('items.result') 
     150                print "%s has %d item(s)" % (items.node_name, len(items.items)) 
     151 
     152                for item in items.items: 
     153                    n = ItemsDiscovery(unicode(self.from_jid), u'pubsub.%s' % self.xmpphost,  
     154                                       node_name=item.node) 
     155                    self.send(n, "items-disco") 
    157156 
    158157            if self.dataReady('items.error'): 
     
    214213                node = self.recv('created') 
    215214                print "Node created: %s" % node.node_name 
    216                  
     215                p = Node(unicode(self.from_jid), u'pubsub.%s' % self.xmpphost, 
     216                         node_name=node.node_name, sub_jid=self.from_jid.nodeid()) 
     217                self.send(p, "subscribe-node") 
     218                 
     219            if self.dataReady('subscribed'): 
     220                node = self.recv('subscribed') 
     221                print "Node subscribed: %s" % node.node_name 
     222 
    217223            if self.dataReady('deleted'): 
    218224                node = self.recv('deleted') 
     
    235241               "control"    : "",  
    236242               "xmpp.result": "", 
     243               "published": "", 
     244               "publish.error": "", 
     245               "retract.error": "", 
    237246               "jid"        : "", 
    238247               "_feedresponse": "", 
     
    253262        self.xmpphost = host 
    254263        self.session_id = session_id 
    255         self.collection = None 
    256264        self.profile = profile 
    257         if profile: 
    258             self.collection = self.atompub.get_collection(profile.username) 
     265        self._collection = None 
    259266        self.pubsub_top_level_node = u'home/%s/%s' % (self.xmpphost, self.from_jid.node) 
     267 
     268    @property 
     269    def collection(self): 
     270        # Lazy loading of collection 
     271        if not self._collection: 
     272            self._collection = self.atompub.get_collection(self.profile.username) 
     273        return self._collection 
    260274 
    261275    def initComponents(self): 
     
    319333                i = Item(id=uuid, payload=entry) 
    320334                p = Node(unicode(self.from_jid), u'pubsub.%s' % self.xmpphost, 
    321                          node_name=node, item=i) 
     335                         node_name=unicode(node), item=i) 
    322336                self.send(p, "publish") 
    323337                yield 1 
     
    325339            if self.dataReady("todelete"): 
    326340                item_id = self.recv("todelete") 
     341                node = self.pubsub_top_level_node 
     342                m = retract_item_rx.match(item_id) 
     343                if m: 
     344                    node, item_id = m.groups() 
    327345                i = Item(id=unicode(item_id)) 
    328346                p = Node(unicode(self.from_jid), u'pubsub.%s' % self.xmpphost, 
    329                          node_name=self.pubsub_top_level_node, item=i) 
     347                         node_name=unicode(node), item=i) 
    330348                self.send(p, "delete") 
    331349                yield 1 
     
    340358                          'method': 'GET'} 
    341359                self.send(params, '_feedrequest')  
     360 
     361            if self.dataReady("published"): 
     362                node = self.recv("published") 
     363                print "Item published: %s" % node 
     364 
     365            if self.dataReady("publish.error"): 
     366                node = self.recv("publish.error") 
     367                print node.error 
     368 
     369            if self.dataReady("retract.error"): 
     370                node = self.recv("retract.error") 
     371                print node.error 
    342372 
    343373            if self.dataReady("_feedresponse"): 
     
    375405        self.session_id = session_id 
    376406        self.profile = profile 
    377         self.collection = self.atompub.get_collection(profile.username) 
     407        self._collection = None 
     408         
     409    @property 
     410    def collection(self): 
     411        # Lazy loading of collection 
     412        if not self._collection: 
     413            self._collection = self.atompub.get_collection(self.profile.username) 
     414        return self._collection 
    378415 
    379416    def initComponents(self): 
     
    411448            if self.dataReady("inbox"): 
    412449                msg = self.recv("inbox") 
    413                 for item in msg.items: 
    414                     if item.event == 'item' and item.payload: 
    415                         member = self.collection.get_member(item.id) 
    416                         if not member: 
    417                             body = item.payload.xml() 
    418                             params = {'url': self.collection.get_base_edit_uri(),  
    419                                       'method': 'POST', 'postbody': body, 
    420                                       'extraheaders': {'content-type': 'application/atom+xml;type=entry', 
    421                                                        'content-length': str(len(body)), 
    422                                                        'slug': item.id}} 
     450                collection = self.collection 
     451                if collection: 
     452                    for item in msg.items: 
     453                        if item.event == 'item' and item.payload: 
     454                            print "Published item: %s" % item.id 
     455                            member = collection.get_member(item.id) 
     456                            if not member: 
     457                                body = item.payload.xml() 
     458                                params = {'url': collection.get_base_edit_uri(),  
     459                                          'method': 'POST', 'postbody': body, 
     460                                          'extraheaders': {'content-type': 'application/atom+xml;type=entry', 
     461                                                           'content-length': str(len(body)), 
     462                                                           'slug': item.id}} 
     463                                self.send(params, '_request')  
     464                        elif item.event == 'retract': 
     465                            print "Removed item: %s" % item.id 
     466                            params = {'url': '%s/%s' % (collection.get_base_edit_uri().rstrip('/'), 
     467                                                        item.id.encode('utf-8')),  
     468                                      'method': 'DELETE'} 
    423469                            self.send(params, '_request')  
    424                     elif item.event == 'retract': 
    425                         params = {'url': '%s/%s' % (self.collection.get_base_edit_uri().rstrip('/'), 
    426                                                     item.id.encode('utf-8')),  
    427                                   'method': 'DELETE'} 
    428                         self.send(params, '_request')  
    429470 
    430471            if not self.anyReady():