Changeset 148:75cf9e14b776


Ignore:
Timestamp:
Aug 12, 2011, 3:57:20 PM (9 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
wokkel-muc-client-support-24
Message:

Redo room configuration.

  • The underlying request object now derives from generic.Request.
  • MUCClient.getConfiguration now returns a wokkel.data_form.Form.
  • MUCClient.configure now takes a simple dictionary of values.
Location:
wokkel
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • wokkel/iwokkel.py

    r141 r148  
    798798
    799799
    800     def configure(room_jid, fields=[]):
    801         """Configure a room. When a user has administration privledges they can configure
    802         a room.
    803 
    804         @param room_jid: A XMPP entity for the room.
    805         @type room_jid: L{jid.JID}
    806 
    807         @param fields: A list of fields to change or add to the room's configuration.
    808         @type fields: C{list}
    809 
    810         """
    811 
    812     def getConfigureForm(room_jid):
    813         """
    814         In order to know the fields to change in a room you will need to get the form.
    815        
    816         @param room_jid: The room you want the configuration form from.
    817         @type room_jid: L{jid.JID}
    818 
    819         """
     800    def configure(roomJID, options):
     801        """
     802        Configure a room.
     803
     804        @param roomJID: The room to configure.
     805        @type roomJID: L{jid.JID}
     806
     807        @param options: A mapping of field names to values, or C{None} to cancel.
     808        @type options: C{dict}
     809        """
     810
     811
     812    def getConfiguration(roomJID):
     813        """
     814        Grab the configuration from the room.
     815
     816        This sends an iq request to the room.
     817
     818        @param roomJID: The bare JID of the room.
     819        @type roomJID: L{jid.JID}
     820
     821        @return: A deferred that fires with the room's configuration form as
     822            a L{data_form.Form} or C{None} if there are no configuration
     823            options available.
     824        """
     825
    820826
    821827    def join(server, room, nick):
  • wokkel/muc.py

    r147 r148  
    4444
    4545
    46 class ConfigureRequest(xmlstream.IQ):
     46class ConfigureRequest(generic.Request):
    4747    """
    4848    Configure MUC room request.
     
    5454    """
    5555
    56     def __init__(self, xs, method='get', fields=[]):
    57         xmlstream.IQ.__init__(self, xs, method)
    58         q = self.addElement((NS_MUC_OWNER, 'query'))
    59         if method == 'set':
    60             # build data form
    61             form = data_form.Form('submit', formNamespace=NS_MUC_CONFIG)
    62             q.addChild(form.toElement())
    63 
    64             for f in fields:
    65                 # create a field
    66                 form.addField(f)
     56    def __init__(self, recipient, sender=None, options=None):
     57        if options is None:
     58            stanzaType = 'get'
     59        else:
     60            stanzaType = 'set'
     61
     62        generic.Request.__init__(self, recipient, sender, stanzaType)
     63        self.options = options
     64
     65
     66    def toElement(self):
     67        element = generic.Request.toElement(self)
     68
     69        query = element.addElement((NS_MUC_OWNER, 'query'))
     70        if self.options is None:
     71            # This is a request for the configuration form.
     72            form = None
     73        elif not self.options:
     74            form = data_form.Form(formType='cancel')
     75        else:
     76            form = data_form.Form(formType='submit', formNamespace=NS_MUC_CONFIG)
     77            form.makeFields(self.options)
     78
     79        if form:
     80            query.addChild(form.toElement())
     81
     82        return element
    6783
    6884
     
    826842
    827843
    828     def configure(self, roomJID, fields=[]):
     844    def configure(self, roomJID, options):
    829845        """
    830846        Configure a room.
    831847
     848        @param roomJID: The room to configure.
     849        @type roomJID: L{jid.JID}
     850
     851        @param options: A mapping of field names to values, or C{None} to cancel.
     852        @type options: C{dict}
     853        """
     854        if not options:
     855            options = False
     856        request = ConfigureRequest(recipient=roomJID, options=options)
     857        return self.request(request)
     858
     859
     860    def getConfiguration(self, roomJID):
     861        """
     862        Grab the configuration from the room.
     863
     864        This sends an iq request to the room.
     865
    832866        @param roomJID: The bare JID of the room.
    833867        @type roomJID: L{jid.JID}
    834868
    835         @param fields: The fields we want to modify.
    836         @type fields: A L{list} or L{dataform.Field}
    837         """
    838         request = ConfigureRequest(self.xmlstream, method='set', fields=fields)
    839         request['to'] = roomJID
    840 
    841         return request.send()
    842 
    843 
    844     def getConfigureForm(self, roomJID):
    845         """
    846         Grab the configuration form from the room.
    847 
    848         This sends an iq request to the room.
    849 
    850         @param roomJID: The bare JID of the room.
    851         @type roomJID: L{jid.JID}
    852         """
    853         request = ConfigureRequest(self.xmlstream)
    854         request['to'] = roomJID
    855         return request.send()
     869        @return: A deferred that fires with the room's configuration form as
     870            a L{data_form.Form} or C{None} if there are no configuration
     871            options available.
     872        """
     873        def cb(response):
     874            form = data_form.findForm(response.query, NS_MUC_CONFIG)
     875            return form
     876
     877        request = ConfigureRequest(recipient=roomJID, options=None)
     878        d = self.request(request)
     879        d.addCallback(cb)
     880        return d
    856881
    857882
  • wokkel/test/test_muc.py

    r147 r148  
    2020from wokkel import data_form, iwokkel, muc
    2121from wokkel.generic import parseXml
    22 from wokkel.test.helpers import XmlStreamStub, TestableStreamManager
     22from wokkel.test.helpers import TestableStreamManager
    2323
    2424
     
    590590
    591591
    592     def test_roomConfigure(self):
     592    def test_configure(self):
    593593        """
    594594        Default configure and changing the room name.
     
    598598            self.assertEquals('result', iq['type'], 'Not a result')
    599599
    600 
    601         fields = []
    602 
    603         fields.append(data_form.Field(label='Natural-Language Room Name',
    604                                       var='muc#roomconfig_roomname',
    605                                       value=self.roomIdentifier))
    606 
    607         d = self.protocol.configure(self.roomJID, fields)
    608         d.addCallback(cb)
    609 
    610         iq = self.stub.output[-1]
    611         query = "/iq/query[@xmlns='%s']/x"% muc.NS_MUC_OWNER
    612         self.assertTrue(xpath.matches(query, iq), 'Bad configure request')
     600        values = {'muc#roomconfig_roomname': self.roomIdentifier}
     601
     602        d = self.protocol.configure(self.roomJID, values)
     603        d.addCallback(cb)
     604
     605        iq = self.stub.output[-1]
     606
     607        self.assertEquals('set', iq.getAttribute('type'))
     608        self.assertEquals(self.roomJID.full(), iq.getAttribute('to'))
     609
     610        query = "/iq/query[@xmlns='%s']" % (muc.NS_MUC_OWNER)
     611        nodes = xpath.queryForNodes(query, iq)
     612        self.assertNotIdentical(None, nodes, 'Bad configure request')
     613
     614        form = data_form.findForm(nodes[0], muc.NS_MUC_CONFIG)
     615        self.assertNotIdentical(None, form, 'Missing configuration form')
     616        self.assertEquals('submit', form.formType)
    613617
    614618        response = toResponse(iq, 'result')
    615619        self.stub.send(response)
     620        return d
     621
     622
     623    def test_configureCancel(self):
     624        """
     625        Cancelling room configuration should send a cancel form.
     626        """
     627
     628        d = self.protocol.configure(self.roomJID, None)
     629
     630        iq = self.stub.output[-1]
     631
     632        query = "/iq/query[@xmlns='%s']" % (muc.NS_MUC_OWNER)
     633        nodes = xpath.queryForNodes(query, iq)
     634
     635        form = data_form.findForm(nodes[0], muc.NS_MUC_CONFIG)
     636        self.assertNotIdentical(None, form, 'Missing configuration form')
     637        self.assertEquals('cancel', form.formType)
     638
     639        response = toResponse(iq, 'result')
     640        self.stub.send(response)
     641        return d
     642
     643
     644    def test_getConfiguration(self):
     645        """
     646        The response of a configure form request should extract the form.
     647        """
     648
     649        def cb(form):
     650            self.assertEquals('form', form.formType)
     651
     652        d = self.protocol.getConfiguration(self.roomJID)
     653        d.addCallback(cb)
     654
     655        iq = self.stub.output[-1]
     656
     657        query = "/iq/query[@xmlns='%s']" % (muc.NS_MUC_OWNER)
     658        nodes = xpath.queryForNodes(query, iq)
     659        self.assertNotIdentical(None, nodes, 'Missing query element')
     660
     661        self.assertRaises(StopIteration, nodes[0].elements().next)
     662
     663        xml = u"""
     664            <iq from='%s' id='%s' to='%s' type='result'>
     665              <query xmlns='http://jabber.org/protocol/muc#owner'>
     666                <x xmlns='jabber:x:data' type='form'>
     667                  <field type='hidden'
     668                         var='FORM_TYPE'>
     669                    <value>http://jabber.org/protocol/muc#roomconfig</value>
     670                  </field>
     671                  <field label='Natural-Language Room Name'
     672                         type='text-single'
     673                         var='muc#roomconfig_roomname'/>
     674                </x>
     675              </query>
     676            </iq>
     677        """ % (self.roomJID, iq['id'], self.userJID)
     678        self.stub.send(parseXml(xml))
     679
     680        return d
     681
     682
     683    def test_getConfigurationNoOptions(self):
     684        """
     685        The response of a configure form request should extract the form.
     686        """
     687
     688        def cb(form):
     689            self.assertIdentical(None, form)
     690
     691        d = self.protocol.getConfiguration(self.roomJID)
     692        d.addCallback(cb)
     693
     694        iq = self.stub.output[-1]
     695
     696        xml = u"""
     697            <iq from='%s' id='%s' to='%s' type='result'>
     698              <query xmlns='http://jabber.org/protocol/muc#owner'/>
     699            </iq>
     700        """ % (self.roomJID, iq['id'], self.userJID)
     701        self.stub.send(parseXml(xml))
     702
    616703        return d
    617704
Note: See TracChangeset for help on using the changeset viewer.