Not logged in. · Lost password · Register
Forum: MatriX RSS
Avatar
peternorman #1
Member since Feb 2013 · 19 posts
Group memberships: Members
Show profile · Link to this post
Subject: Questions about IqEventData
What is the best way to access the affiliations inside of the Iq object? When retrieving subscriptions back I have been doing the following which is great:

  1. var pubsub = e.Iq.Element<PubSub>();

and then iterating through the Subscriptions.

I had hoped that the affiliations would be the same way, unfortunately I am doing something wrong and was wondering if someone could point me in the right direction. I'm assuming there is a standard way of retrieving the data from an IqEventArgs apart from actually parsing the xml but I could be wrong.

  1. private void OnRequestAffiliation(object sender, IqEventArgs e)
  2. {   
  3.            var iq = e.Iq;
  4.  
  5.             if(iq != null)
  6.             {
  7.                 XNamespace ns = "http://jabber.org/protocol/pubsub#owner";
  8.  
  9.                 var a = iq.Query.Element(ns + "affiliations");
  10.             }
  11. }

This returns an affiliations XElement but when I attempt to drill down further to the affiliation element it always returns null... It shows up in the debugger so I know its there but for some reason I'm unable to retrieve.

Thanks in advance,
Peter Norman
This post was edited on 2013-03-14, 20:25 by Alex.
Avatar
Alex #2
Member since Feb 2003 · 4322 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
here is an example from one of our unit Tests. Let me know if it helps.

  1. public void AffiliationsTest()
  2. {
  3.    const string XML3
  4.             = @"<iq type='result'
  5.     from='pubsub.shakespeare.lit'
  6.     to='francisco@denmark.lit'
  7.     id='affil1' xmlns='jabber:client'>
  8.   <pubsub xmlns='http://jabber.org/protocol/pubsub'>
  9.     <affiliations>
  10.       <affiliation node='node1' affiliation='owner'/>
  11.       <affiliation node='node2' affiliation='publisher'/>
  12.       <affiliation node='node5' affiliation='outcast'/>
  13.       <affiliation node='node6' affiliation='owner'/>
  14.     </affiliations>
  15.   </pubsub>
  16. </iq>";
  17.   var iq = XmppXElement.LoadXml(XML3) as Iq;
  18.  
  19.   var pubsub = iq.Element<Matrix.Xmpp.PubSub.PubSub>();
  20.   var affs = pubsub.Element<Affiliations>();
  21.  
  22.  foreach (var affiliation in affs.GetAffiliations())
  23.  {
  24.       System.Diagnostics.Debug.WriteLine(affiliation.Node);
  25.  }
  26. }

I will add a Affiliations property for the next release to the PubSub class. Then you can do:

  1. var affs = pubsub.Affiliations.GetAffiliations())
Avatar
peternorman #3
Member since Feb 2013 · 19 posts
Group memberships: Members
Show profile · Link to this post
Thanks, this is perfect!
Avatar
peternorman #4
Member since Feb 2013 · 19 posts
Group memberships: Members
Show profile · Link to this post
Another question...

I'm receiving this xml:

  1. <iq from="pubsub.trinity" to="admin@trinity/MatriX" id="MX_50" type="result" xmlns="jabber:client">
  2.  <pubsub xmlns="http://jabber.org/protocol/pubsub#owner">
  3.     <affiliations node="/home/trinity/admin">
  4.       <affiliation jid="admin@trinity" affiliation="owner" />
  5.     </affiliations>
  6.  </pubsub>
  7. </iq>

  1. var iq = e.Iq;
  2. //var pubsub = iq.Element<Matrix.Xmpp.PubSub.PubSub>(); //<-- This is null
  3. var pubsub = iq.Element<Matrix.Xmpp.PubSub.Owner.PubSub>(); //<-- This is ok and I can see the affiliations
  4. var affs = pubsub.Element<Affiliations>(); //<-- This is null


I see in the unit test you posted the pubsub element doesn't contain the #owner, is that the reason I needed to use the PubSub element from the Owner namespace? Assuming , that is correct I don't undertand why affs is null.

Thanks,
Peter Norman
This post was edited 2 times, last on 2013-03-15, 18:48 by Alex.
Avatar
peternorman #5
Member since Feb 2013 · 19 posts
Group memberships: Members
Show profile · Link to this post
Alright I see I had to do the following to get the affs populated:

var affs = pubsub.Element<Matrix.Xmpp.PubSub.Owner.Affiliations>();

I see in your unit test that you have two nodes with owner affiliations yet you don't have the #owner portion in your pubsub element... is this a configuration option of ejabberd?

I'm asking because I will be having different types of affiliations and I would prefer your method of grabbing the affiliations.

Thanks
Avatar
Alex #6
Member since Feb 2003 · 4322 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
In reply to post #4
Quote by peternorman:
I see in the unit test you posted the pubsub element doesn't contain the #owner, is that the reason I needed to use the PubSub element from the Owner namespace? Assuming , that is correct I don't undertand why affs is null.

this are just 2 different namespaces for 2 different use cases, both exist.
MatriX is namespace aware, so they have both its own class.

Entity Use Cases Retrieve Affiliations

  1. <iq type='result'
  2.     from='pubsub.shakespeare.lit'
  3.     to='francisco@denmark.lit'
  4.     id='affil1'>
  5.  <pubsub xmlns='http://jabber.org/protocol/pubsub'>
  6.     <affiliations>
  7.       <affiliation node='node1' affiliation='owner'/>
  8.       <affiliation node='node2' affiliation='publisher'/>
  9.       <affiliation node='node5' affiliation='outcast'/>
  10.       <affiliation node='node6' affiliation='owner'/>
  11.     </affiliations>
  12.  </pubsub>
  13. </iq>

Node Owner Managing Affiliations:

  1. <iq type='result'
  2.     from='pubsub.shakespeare.lit'
  3.     to='hamlet@denmark.lit/elsinore'
  4.     id='ent1'>
  5.  <pubsub xmlns='http://jabber.org/protocol/pubsub#owner'>
  6.     <affiliations node='princely_musings'>
  7.       <affiliation jid='hamlet@denmark.lit' affiliation='owner'/>
  8.       <affiliation jid='polonius@denmark.lit' affiliation='outcast'/>
  9.     </affiliations>
  10.  </pubsub>
  11. </iq>

namespaces gets inherited to the children. This means the <affiliations/> and <affiliation/> tags are also in different namespaces and exist twice.

Alex
This post was edited on 2013-03-15, 19:04 by Alex.
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Forum: MatriX RSS