branch: elpa/jabber
commit 317e84b70700b3787c2cb0f34e98f81fc7310321
Author: Thanos Apollo <[email protected]>
Commit: Thanos Apollo <[email protected]>

    util: Add jabber-roster-contact-p
---
 lisp/jabber-util.el       |  6 ++++++
 tests/jabber-test-util.el | 27 +++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/lisp/jabber-util.el b/lisp/jabber-util.el
index d97091052c..a1f1f504b0 100644
--- a/lisp/jabber-util.el
+++ b/lisp/jabber-util.el
@@ -83,6 +83,12 @@ INITIAL-CONTENTS, HISTORY and DEFAULT-VALUE are passed 
straight through."
   (read-string prompt initial-contents history default-value t))
 
 (defvar jabber-connections)
+(defun jabber-roster-contact-p (jc jid)
+  "Return non-nil when JID's bare JID is on connection JC's roster."
+  (and jc jid
+       (memq (jabber-jid-symbol jid)
+             (plist-get (fsm-get-state-data jc) :roster))))
+
 (defun jabber-concat-rosters ()
   "Concatenate the rosters of all connected accounts."
   (apply #'append
diff --git a/tests/jabber-test-util.el b/tests/jabber-test-util.el
index ab54b5a481..6189839090 100644
--- a/tests/jabber-test-util.el
+++ b/tests/jabber-test-util.el
@@ -271,5 +271,32 @@
   (should-not (jabber--decrypt-failure-body-p
                "[OMEMO: could not decrypt] trailing text")))
 
+;;; Group: Roster membership predicate
+
+(defun jabber-test-util--make-jc-with-roster (&rest jids)
+  "Create a fake connection whose roster contains JIDS."
+  (let ((jc (gensym "jabber-test-util-jc-")))
+    (put jc :state-data
+         (list :roster (mapcar #'jabber-jid-symbol jids)))
+    jc))
+
+(ert-deftest jabber-test-util-roster-contact-p-bare-jid ()
+  (let ((jc (jabber-test-util--make-jc-with-roster "[email protected]")))
+    (should (jabber-roster-contact-p jc "[email protected]"))))
+
+(ert-deftest jabber-test-util-roster-contact-p-full-jid ()
+  "Resource is stripped before the roster lookup."
+  (let ((jc (jabber-test-util--make-jc-with-roster "[email protected]")))
+    (should (jabber-roster-contact-p jc "[email protected]/laptop"))))
+
+(ert-deftest jabber-test-util-roster-contact-p-stranger ()
+  (let ((jc (jabber-test-util--make-jc-with-roster "[email protected]")))
+    (should-not (jabber-roster-contact-p jc "[email protected]"))))
+
+(ert-deftest jabber-test-util-roster-contact-p-nil-args ()
+  (let ((jc (jabber-test-util--make-jc-with-roster "[email protected]")))
+    (should-not (jabber-roster-contact-p nil "[email protected]"))
+    (should-not (jabber-roster-contact-p jc nil))))
+
 (provide 'jabber-test-util)
 ;;; jabber-test-util.el ends here

Reply via email to