Not logged in. · Lost password · Register
Forum: MatriX RSS
Avatar
mrbelk #1
Member since Feb 2017 · 23 posts
Group memberships: Members
Show profile · Link to this post
Subject: XPath presence filter vs. XmppClient.OnPresence
If I want to filter out presence updates from certain resources, I can use an XPath filter to do that, with a callback. Will that filter be respected in the .OnPresence event handler set up on my xmppClient object? Or do I have to so something in the filter callback in order to do whatever business logic I currently am doing in my .OnPresence event handler?

If I have to use the callback, how do I get the Presence object like I can get in the OnPresence event handler?

Thanks,
Matthew
Avatar
Alex #2
Member since Feb 2003 · 4296 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
Quote by mrbelk:
If I want to filter out presence updates from certain resources, I can use an XPath filter to do that, with a callback. Will that filter be respected in the .OnPresence event handler set up on my xmppClient object? Or do I have to so something in the filter callback in order to do whatever business logic I currently am doing in my .OnPresence event handler?

You have to add code to your business logic.
The main purpose of the filters is to structure your code base better without endless if/else or switch statements in the presence, message and iq handlers. Instead of XPath you can also use predicates with Linq.

Quote by mrbelk:
If I have to use the callback, how do I get the Presence object like I can get in the OnPresence event handler?

it gets passed to your callback (e.Stanza).
See the example from here:
https://www.ag-software.net/matrix-xmpp-sdk/matrix-develop…

Alex
Avatar
mrbelk #3
Member since Feb 2017 · 23 posts
Group memberships: Members
Show profile · Link to this post
OK. I figured.

New related question:

How would I set up a "PresenceFilter" to only pass presence updates from Jids with a particular resource?

Thanks,
Matthew
Avatar
Alex #4
Member since Feb 2003 · 4296 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
The presence filters right now have only a comparer for full Jid or bare Jid.
You could roll your own comparer, we is the code of the FullJidComparer as a reference.

  1. public class FullJidComparer : IComparer<Jid>   
  2. {
  3.     #region IComparer<Jid> Members
  4.     public int Compare(Jid x, Jid y)
  5.     {
  6.         if (x != null && y != null)
  7.         {
  8.             if (x.ToString() == y.ToString())
  9.                 return 0;
  10.  
  11.             return String.Compare(x.ToString(), y.ToString());
  12.         }
  13.  
  14.         if (x == null)
  15.         {
  16.             if (y == null)
  17.             {
  18.                 return 0;
  19.             }
  20.             return -1;
  21.         }
  22.            
  23.         return 1;
  24.     }
  25.     #endregion
  26. }
  27.  
  28.  
  29. public class BareJidComparer : IComparer<Jid>
  30. {
  31.     public int Compare(Jid x, Jid y)
  32.     {
  33.         if (x != null && y != null)
  34.         {
  35.             if (x.Bare == y.Bare)
  36.                 return 0;
  37.  
  38.             return String.Compare(x.Bare, y.Bare);
  39.         }
  40.         if (x == null)
  41.         {
  42.             if (y == null)
  43.             {
  44.                 return 0;
  45.             }
  46.             return -1;
  47.         }
  48.  
  49.         return 1;
  50.     }
  51. }
This post was edited 3 times, last on 2017-02-10, 16:45 by Alex.
Avatar
mrbelk #5
Member since Feb 2017 · 23 posts
Group memberships: Members
Show profile · Link to this post
OK, so it would be sort of like a "JidResourceComparer" type of object. I get that. Now, when Add the filter to the xmppClient, what would I use as the first argument, which is a Jid? Is that the Jid of the user being logged in via the client?

-Matthew
Avatar
Alex #6
Member since Feb 2003 · 4296 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
the jid you pass in a static Jid. for every incoming presence packet the filter reads the from Jid and compares it to the static Jid.

here is an example for group chat:
https://gitlab.com/matrix-xmpp/samples/blob/master/csharp/…

when I create a chat room then the room get a jid assigned which is for example matrix@conference.ag-software.net.
This Jid gets passed to the filter with a Bare Jid comparer. Because every participant of a room get a Jid of the following format assigned matrix@conference.ag-software.net/nickname.


presence from those Jids will match the bare jid filter:
  • matrix@conference.ag-software.net/Alex
  • matrix@conference.ag-software.net/Mattew

presence from those Jids will not match the bare jid filter, they would belong to other chatrooms:
  • xmpp@conference.ag-software.net/Alex
  • talk@conference.ag-software.net/Mattew

In this MiniClient example this filter is used to display presences and messages for a room in the correct Form, because every chatroom is getting its own windows form or tab.
Avatar
mrbelk #7
Member since Feb 2017 · 23 posts
Group memberships: Members
Show profile · Link to this post
I figured it out. I just used a valid Jid that had a resource on the end that was the resource I was wanting to filter on.  It seems to work just fine now.

-Matthew
Avatar
Alex #8
Member since Feb 2003 · 4296 posts · Location: Germany
Group memberships: Administrators, Members
Show profile · Link to this post
yes, this should work fine with a custom comparer
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