git » sdk » commit 47fdbe8

Record all presence even without caps

author Stephen Paul Weber
2023-10-18 16:20:07 UTC
committer Stephen Paul Weber
2023-10-18 16:20:07 UTC
parent 138031b45fab06aa69e569b206bde9a8b445fa7c

Record all presence even without caps

Probably want to record other details than just null, but this will do
for now

xmpp/Caps.hx +12 -8
xmpp/Chat.hx +1 -1
xmpp/Client.hx +26 -16

diff --git a/xmpp/Caps.hx b/xmpp/Caps.hx
index b3fe1c1..4aeae5a 100644
--- a/xmpp/Caps.hx
+++ b/xmpp/Caps.hx
@@ -11,24 +11,28 @@ class Caps {
 	public final features : Array<String>;
 	// TODO: data forms
 
-	public static function withIdentity(caps:KeyValueIterator<String, Caps>, category:Null<String>, type:Null<String>):Array<String> {
+	public static function withIdentity(caps:KeyValueIterator<String, Null<Caps>>, category:Null<String>, type:Null<String>):Array<String> {
 		final result = [];
 		for (cap in caps) {
-			for (identity in cap.value.identities) {
-				if ((category == null || category == identity.category) && (type == null || type == identity.type)) {
-					result.push(cap.key);
+			if (cap.value != null) {
+				for (identity in cap.value.identities) {
+					if ((category == null || category == identity.category) && (type == null || type == identity.type)) {
+						result.push(cap.key);
+					}
 				}
 			}
 		}
 		return result;
 	}
 
-	public static function withFeature(caps:KeyValueIterator<String, Caps>, feature:String):Array<String> {
+	public static function withFeature(caps:KeyValueIterator<String, Null<Caps>>, feature:String):Array<String> {
 		final result = [];
 		for (cap in caps) {
-			for (feat in cap.value.features) {
-				if (feature == feat) {
-					result.push(cap.key);
+			if (cap.value != null) {
+				for (feat in cap.value.features) {
+					if (feature == feat) {
+						result.push(cap.key);
+					}
 				}
 			}
 		}
diff --git a/xmpp/Chat.hx b/xmpp/Chat.hx
index 9fd8a0d..d67c3d0 100644
--- a/xmpp/Chat.hx
+++ b/xmpp/Chat.hx
@@ -17,7 +17,7 @@ abstract class Chat {
 	private var stream:GenericStream;
 	private var persistence:Persistence;
 	private var avatarSha1:Null<BytesData> = null;
-	private var caps:haxe.DynamicAccess<Caps> = {};
+	private var caps:haxe.DynamicAccess<Null<Caps>> = {};
 	private var trusted:Bool = false;
 	public var chatId(default, null):String;
 	public var jingleSessions: Map<String, xmpp.jingle.Session> = [];
diff --git a/xmpp/Client.hx b/xmpp/Client.hx
index 157409d..6cff23e 100644
--- a/xmpp/Client.hx
+++ b/xmpp/Client.hx
@@ -261,24 +261,34 @@ class Client extends xmpp.EventEmitter {
 		this.stream.on("presence", function(event) {
 			final stanza:Stanza = event.stanza;
 			final c = stanza.getChild("c", "http://jabber.org/protocol/caps");
-			if (c != null && stanza.attr.get("from") != null) {
-				final chat = getDirectChat(JID.parse(stanza.attr.get("from")).asBare().asString(), false);
-				persistence.getCaps(c.attr.get("ver"), (caps) -> {
-					if (caps == null) {
-						final discoGet = new DiscoInfoGet(stanza.attr.get("from"), c.attr.get("node") + "#" + c.attr.get("ver"));
-						discoGet.onFinished(() -> {
-							if (discoGet.getResult() != null) {
-								persistence.storeCaps(discoGet.getResult());
+			if (stanza.attr.get("from") != null) {
+				final chat = getChat(JID.parse(stanza.attr.get("from")).asBare().asString());
+				if (chat == null) {
+					trace("Presence for unknown JID: " + stanza.attr.get("from"));
+					return EventUnhandled;
+				}
+				if (c == null) {
+					chat.setCaps(JID.parse(stanza.attr.get("from")).resource, null);
+					persistence.storeChat(jid, chat);
+					this.trigger("chats/update", [chat]);
+				} else {
+					persistence.getCaps(c.attr.get("ver"), (caps) -> {
+						if (caps == null) {
+							final discoGet = new DiscoInfoGet(stanza.attr.get("from"), c.attr.get("node") + "#" + c.attr.get("ver"));
+							discoGet.onFinished(() -> {
 								chat.setCaps(JID.parse(stanza.attr.get("from")).resource, discoGet.getResult());
+								if (discoGet.getResult() != null) persistence.storeCaps(discoGet.getResult());
 								persistence.storeChat(jid, chat);
-							}
-						});
-						sendQuery(discoGet);
-					} else {
-						chat.setCaps(JID.parse(stanza.attr.get("from")).resource, caps);
-						persistence.storeChat(jid, chat);
-					}
-				});
+								this.trigger("chats/update", [chat]);
+							});
+							sendQuery(discoGet);
+						} else {
+							chat.setCaps(JID.parse(stanza.attr.get("from")).resource, caps);
+							persistence.storeChat(jid, chat);
+							this.trigger("chats/update", [chat]);
+						}
+					});
+				}
 				return EventHandled;
 			}