git » sdk » commit 1271761

Ring for calls that came in while offline if still active

author Stephen Paul Weber
2023-10-04 14:39:06 UTC
committer Stephen Paul Weber
2023-10-05 03:28:01 UTC
parent 62d0e0562952ae996d1534c4164b603a340a465a

Ring for calls that came in while offline if still active

xmpp/Client.hx +21 -3
xmpp/MessageSync.hx +15 -7
xmpp/Stanza.hx +1 -1

diff --git a/xmpp/Client.hx b/xmpp/Client.hx
index 2a2563c..41a3ad6 100644
--- a/xmpp/Client.hx
+++ b/xmpp/Client.hx
@@ -81,9 +81,11 @@ class Client extends xmpp.EventEmitter {
 			if (jmiP != null && jmiP.attr.get("id") != null) {
 				final session = new IncomingProposedSession(this, from, jmiP.attr.get("id"));
 				final chat = getDirectChat(from.asBare().asString());
-				chat.jingleSessions.set(session.sid, session);
-				chatActivity(chat);
-				session.ring();
+				if (!chat.jingleSessions.exists(session.sid)) {
+					chat.jingleSessions.set(session.sid, session);
+					chatActivity(chat);
+					session.ring();
+				}
 			}
 
 			final jmiR = stanza.getChild("retract", "urn:xmpp:jingle-message:0");
@@ -449,6 +451,19 @@ class Client extends xmpp.EventEmitter {
 		persistence.lastId(jid, null, (lastId) -> doSync(callback, lastId));
 	}
 
+	private function onMAMJMI(sid: String, stanza: Stanza) {
+		if (stanza.attr.get("from") == null) return;
+		final from = JID.parse(stanza.attr.get("from"));
+		final chat = getDirectChat(from.asBare().asString());
+		if (chat.jingleSessions.exists(sid)) return; // Already know about this session
+		final jmiP = stanza.getChild("propose", "urn:xmpp:jingle-message:0");
+		if (jmiP == null) return;
+		final session = new IncomingProposedSession(this, from, sid);
+		chat.jingleSessions.set(session.sid, session);
+		chatActivity(chat);
+		session.ring();
+	}
+
 	private function doSync(callback: Null<()->Void>, lastId: Null<String>) {
 		var thirtyDaysAgo = Date.format(
 			DateTools.delta(std.Date.now(), DateTools.days(-30))
@@ -466,6 +481,9 @@ class Client extends xmpp.EventEmitter {
 			if (sync.hasMore()) {
 				sync.fetchNext();
 			} else {
+				for (sid => stanza in sync.jmi) {
+					onMAMJMI(sid, stanza);
+				}
 				if (callback != null) callback();
 			}
 		});
diff --git a/xmpp/MessageSync.hx b/xmpp/MessageSync.hx
index f22b2d9..2e5bddf 100644
--- a/xmpp/MessageSync.hx
+++ b/xmpp/MessageSync.hx
@@ -28,6 +28,7 @@ class MessageSync {
 	private var lastPage:ResultSetPageResult;
 	private var complete:Bool = false;
 	private var newestPageFirst:Bool = true;
+	public var jmi(default, null): Map<String, Stanza> = [];
 
 	public function new(client:Client, stream:GenericStream, filter:MessageFilter, ?serviceJID:String) {
 		this.client = client;
@@ -43,7 +44,7 @@ class MessageSync {
 		if (complete) {
 			throw new Exception("Attempt to fetch messages, but already complete");
 		}
-		var messages:Array<ChatMessage> = [];
+		final messages:Array<ChatMessage> = [];
 		if(lastPage == null) {
 			if(newestPageFirst == true && (filter.page == null || filter.page.before == null)) {
 				if (filter.page == null) filter.page = {};
@@ -74,8 +75,13 @@ class MessageSync {
 			}
 			var timestamp = result.findText("{urn:xmpp:forward:0}forwarded/{urn:xmpp:delay}delay@stamp");
 
+			final jmiChildren = originalMessage.allTags(null, "urn:xmpp:jingle-message:0");
+			if (jmiChildren.length > 0) {
+				jmi.set(jmiChildren[0].attr.get("id"), originalMessage);
+			}
+
 			var msg = ChatMessage.fromStanza(originalMessage, client.jid);
-			if (msg == null) return EventUnhandled;
+			if (msg == null) return EventHandled;
 
 			msg.set_serverId(result.attr.get("id"));
 			msg.set_timestamp(timestamp);
@@ -90,15 +96,17 @@ class MessageSync {
 			if(result == null) {
 				trace("Error from MAM, stopping sync");
 				complete = true;
-				errorHandler(query.responseStanza);
+				if (errorHandler != null) errorHandler(query.responseStanza);
 			} else {
 				complete = result.complete;
 				lastPage = result.page;
 			}
-			handler({
-				sync: this,
-				messages: messages,
-			});
+			if (result != null || errorHandler == null) {
+				handler({
+					sync: this,
+					messages: messages,
+				});
+			}
 		});
 		client.sendQuery(query);
 	}
diff --git a/xmpp/Stanza.hx b/xmpp/Stanza.hx
index 261058d..7e10bb0 100644
--- a/xmpp/Stanza.hx
+++ b/xmpp/Stanza.hx
@@ -166,7 +166,7 @@ class Stanza implements NodeInterface {
 			var ourXmlns = this.attr.get("xmlns");
 			tags = tags.filter(function (child:Stanza):Bool {
 				var childXmlns = child.attr.get("xmlns");
-				return (name == null || child.name == name
+				return ((name == null || child.name == name)
 				  && ((xmlns == null && (ourXmlns == childXmlns || childXmlns == null))
 				     || childXmlns == xmlns));
 			});