git » sdk » commit 732ec6a

Remove presence item when resource unavailable

author Stephen Paul Weber
2023-10-18 16:36:11 UTC
committer Stephen Paul Weber
2023-10-18 16:36:11 UTC
parent a0aaebea222a6a0277bb8042686027f5b0242b98

Remove presence item when resource unavailable

xmpp/Chat.hx +4 -0
xmpp/Client.hx +13 -1

diff --git a/xmpp/Chat.hx b/xmpp/Chat.hx
index d91bd23..0d054ee 100644
--- a/xmpp/Chat.hx
+++ b/xmpp/Chat.hx
@@ -60,6 +60,10 @@ abstract class Chat {
 		this.caps.set(resource, caps);
 	}
 
+	public function removeCaps(resource:String) {
+		this.caps.remove(resource);
+	}
+
 	public function getCaps():KeyValueIterator<String, Caps> {
 		return caps.keyValueIterator();
 	}
diff --git a/xmpp/Client.hx b/xmpp/Client.hx
index f9b874f..c79de7c 100644
--- a/xmpp/Client.hx
+++ b/xmpp/Client.hx
@@ -268,7 +268,7 @@ 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 (stanza.attr.get("from") != null) {
+			if (stanza.attr.get("from") != null && stanza.attr.get("type") == null) {
 				final chat = getChat(JID.parse(stanza.attr.get("from")).asBare().asString());
 				if (chat == null) {
 					trace("Presence for unknown JID: " + stanza.attr.get("from"));
@@ -299,6 +299,18 @@ class Client extends xmpp.EventEmitter {
 				return EventHandled;
 			}
 
+			if (stanza.attr.get("from") != null && stanza.attr.get("type") == "unavailable") {
+				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;
+				}
+				// Maybe in the future record it as offine rather than removing it
+				chat.removeCaps(JID.parse(stanza.attr.get("from")).resource);
+				persistence.storeChat(jid, chat);
+				this.trigger("chats/update", [chat]);
+			}
+
 			return EventUnhandled;
 		});