Patch to support multiple subscription requests

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)

Could you try to make a patch of just the functional changes?

Now there is so much cosmetics from the autoformatter so it is hard to read.

Earlier I went through all the files with some settings which I toughted to be good enough but they werent. I should write clear guidelines for this…

Here, try this on for size. You may have to change the source strings in the header.

### Eclipse Workspace Patch 1.0
#P Flex
Index: src-external/org/igniterealtime/xiff/im/Roster.as
===================================================================
--- src-external/org/igniterealtime/xiff/im/Roster.as (revision 11579)
+++ src-external/org/igniterealtime/xiff/im/Roster.as (working copy)
@@ -23,6 +23,8 @@
  */ package org.igniterealtime.xiff.im {
+ import flash.utils.Dictionary;
+   import org.igniterealtime.xiff.collections.ArrayCollection;
  import org.igniterealtime.xiff.core.*;
  import org.igniterealtime.xiff.data.*;
@@ -121,13 +123,10 @@
   */
  private var _presenceMap:Object = {}; - //FIXME: does not support multiple pending requests
- private var pendingSubscriptionRequestJID:UnescapedJID;
-   /**
   * TODO: List of <code>UnescapedJID</code> which are pending for subscription.
   */
- private var pendingSubscriptionRequests:Array = [];
+ private var pendingSubscriptionRequests:Dictionary = new Dictionary();   /**
   *
@@ -178,18 +177,18 @@
  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";
- pendingSubscriptionRequestJID = id;
- pendingSubscriptionRequests.push(id);
+ pendingSubscriptionRequests[ iqID.toString() ] = id;
  subscription = RosterExtension.SUBSCRIBE_TYPE_TO;
  askType = RosterExtension.ASK_TYPE_SUBSCRIBE
  } - var tempIQ:IQ = new IQ( null, IQ.TYPE_SET, XMPPStanza.generateID( "add_user_" ),
+ 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 ] :
@@ -209,9 +208,15 @@
  public function addContact_result( resultIQ:IQ ):void
  {
  // Contact was added, request subscription
- // TODO: Should check the JID of the result
- requestSubscription( pendingSubscriptionRequestJID );
- pendingSubscriptionRequestJID = null;
+ + var iqID : String = resultIQ.id.toString();
+ + if ( pendingSubscriptionRequests.hasOwnProperty( iqID ) )
+ {
+ var subscriptionId : UnescapedJID = pendingSubscriptionRequests[ iqID ] as UnescapedJID;
+ requestSubscription( subscriptionId );
+ delete( pendingSubscriptionRequests[ iqID ] );
+ }
  }   /**

Now the diff is not possible to apply as it mismatches the exiting and untouched code due to the missing tabs and etc.

Picky Picky. Seriously, Juga, it would take at most 5 minutes to apply these changes manually, rather than going back and forth with me on the forums asking for updates to a patch file.
patch.txt.zip (1143 Bytes)

Perhaps I do not have that extra five minutes.

Anyhow thank you for the fix, applied to trunk in rev 11600.