Ignore:
Timestamp:
May 9, 2012, 2:24:28 PM (8 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Message:

Add support for adding and updating roster items.

Roster items can be added or updated by sending a roster set request using the
new wokkel.xmppim.RosterClientProtocol.setItem. It takes a RosterItem as
the only argument. Note that changes in presence subscriptions for the contact
need to be done using presence as provided by
wokkel.xmppim.PresenceProtocol.

Author: ralphm.
Fixes: #56.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/xmppim.py

    r173 r174  
    694694
    695695
    696     def toElement(self):
     696    def toElement(self, rosterSet=False):
     697        """
     698        Render to a DOM representation.
     699
     700        If C{rosterSet} is set, some attributes, that may not be sent
     701        as a roster set, will not be rendered.
     702
     703        @type rosterSet: C{boolean}.
     704        """
    697705        element = domish.Element((NS_ROSTER, 'item'))
    698706        element['jid'] = self.entity.full()
     
    701709            subscription = 'remove'
    702710        else:
    703             subscription = self.__subscriptionStates[self.subscriptionTo,
    704                                                      self.subscriptionFrom]
    705 
    706             if self.pendingOut:
    707                 element['ask'] = u'subscribe'
    708 
    709711            if self.name:
    710712                element['name'] = self.name
    711 
    712             if self.approved:
    713                 element['approved'] = u'true'
    714713
    715714            if self.groups:
    716715                for group in self.groups:
    717716                    element.addElement('group', content=group)
     717
     718            if rosterSet:
     719                subscription = None
     720            else:
     721                subscription = self.__subscriptionStates[self.subscriptionTo,
     722                                                         self.subscriptionFrom]
     723
     724                if self.pendingOut:
     725                    element['ask'] = u'subscribe'
     726
     727                if self.approved:
     728                    element['approved'] = u'true'
    718729
    719730        if subscription:
     
    755766        versioning.
    756767    @type version: C{unicode}
     768
     769    @ivar rosterSet: If set, this is a roster set request. This flag is used
     770        to make sure some attributes of the roster item are not rendered by
     771        L{toElement}.
     772    @type roster: C{boolean}
    757773    """
    758774    item = None
    759775    version = None
     776    rosterSet = False
    760777
    761778    def parseRequest(self, element):
     
    773790            query['ver'] = self.version
    774791        if self.item:
    775             query.addChild(self.item.toElement())
     792            query.addChild(self.item.toElement(rosterSet=self.rosterSet))
    776793        return element
    777794
     
    893910
    894911
     912    def setItem(self, item):
     913        """
     914        Add or modify a roster item.
     915
     916        Note that RFC 6121 doesn't allow all properties of a roster item to
     917        be sent when setting a roster item. Only the C{name} and C{groups}
     918        attributes from C{item} are sent to the server. Presence subscription
     919        management must be done through L{PresenceProtocol}.
     920
     921        @param item: The roster item to be set.
     922        @type item: L{RosterItem}.
     923
     924        @rtype: L{twisted.internet.defer.Deferred}
     925        """
     926        request = RosterRequest(stanzaType='set')
     927        request.rosterSet = True
     928        request.item = item
     929        return self.request(request)
     930
     931
    895932    def removeItem(self, entity):
    896933        """
     
    899936        @param entity: The contact to remove the roster item for.
    900937        @type entity: L{JID<twisted.words.protocols.jabber.jid.JID>}
     938
    901939        @rtype: L{twisted.internet.defer.Deferred}
    902940        """
    903         request = RosterRequest(stanzaType='set')
    904         request.item = RosterItem(entity)
    905         request.item.remove = True
    906         return self.request(request)
     941        item = RosterItem(entity)
     942        item.remove = True
     943        return self.setItem(item)
    907944
    908945
Note: See TracChangeset for help on using the changeset viewer.