While going through the Roster code I found a couple of TODO’s regarding multiple subscription request handling. Since it was something I need for my own app I went ahead and implemented it, and attached the patch to this post. I’m afraid my autoformatter spit some additional garbage into this patch, so it’s a bit larger than I expected. The actual code changes are as follows:
Add the dictionary class ( Could also be a plain object, but this one’s actually a bit faster ).
import flash.utils.Dictionary;
Switch the type of the pending subscription request parameter (and remove the old one).
/**
* List of <code>UnescapedJID</code> which are pending for subscription.
*/ private var pendingSubscriptionRequests : Dictionary = new Dictionary();
Move the creation of the iqID above the if statement, and use that iqID as a hash index for the pending subscription request.
public function addContact ( id : UnescapedJID, displayName : String, groupName : String = null, requestSubscription : Boolean = true ) : void
{
if ( displayName == null )
{
displayName = id.toString();
}
var callbackObj : Roster = null;
var callbackMethod : String = null;
var subscription : String = RosterExtension.SUBSCRIBE_TYPE_NONE;
var askType : String = RosterExtension.ASK_TYPE_NONE;
var iqID : String = XMPPStanza.generateID( "add_user_" );
if ( requestSubscription == true )
{
callbackObj = this;
callbackMethod = "addContact_result";
pendingSubscriptionRequests[ iqID.toString() ] = id;
subscription = RosterExtension.SUBSCRIBE_TYPE_TO;
askType = RosterExtension.ASK_TYPE_SUBSCRIBE
}
var tempIQ : IQ = new IQ( null, IQ.TYPE_SET, iqID, callbackMethod, callbackObj );
var ext : RosterExtension = new RosterExtension( tempIQ.getNode() );
ext.addItem( id.escaped, null, displayName, groupName ? [ groupName ] : null );
tempIQ.addExtension( ext );
_connection.send( tempIQ );
addRosterItem( id, displayName, RosterExtension.SHOW_PENDING, RosterExtension.SHOW_PENDING, [ groupName ], subscription, askType );
}
…and pull the subscription out of the Dictionary for a potential subscription request.
/**
*
* @param resultIQ
*/
public function addContact_result ( resultIQ : IQ ) : void
{
// Contact was added, request subscription
var iqID : String = resultIQ.id.toString();
if ( pendingSubscriptionRequests.hasOwnProperty( iqID ) )
{
var subscriptionId : UnescapedJID = pendingSubscriptionRequests[ iqID ] as UnescapedJID;
requestSubscription( subscriptionId );
delete( pendingSubscriptionRequests[ iqID ] );
}
}
patch.txt.zip (7001 Bytes)