git » sdk » commit 5844b8e

Actually wait until done

author Stephen Paul Weber
2026-07-21 20:40:32 UTC
committer Stephen Paul Weber
2026-07-21 20:40:32 UTC
parent 082d8524b62609f0ac2f4a0cf4f6b064590a174b

Actually wait until done

borogove/ChatMessage.hx +2 -2
borogove/Client.hx +7 -3

diff --git a/borogove/ChatMessage.hx b/borogove/ChatMessage.hx
index 3418802..f0c7d24 100644
--- a/borogove/ChatMessage.hx
+++ b/borogove/ChatMessage.hx
@@ -580,7 +580,7 @@ class ChatMessage {
 		@param client Client to use when fetching
 		@returns Promise resolving to a ChatAttachment with cachedAt filled in, if possible
 	**/
-	public function fetchAttachment(attachment: ChatAttachment, client: Client) {
+	public function fetchAttachment(attachment: ChatAttachment, client: Client, storeIfNeeded: Bool = true) {
 		final hasNoHashes = attachment.hashes.length < 1;
 		return client.fetchAttachment(attachment).then(r -> {
 			if (hasNoHashes && r.hashes.length > 0) {
@@ -592,7 +592,7 @@ class ChatMessage {
 					if (sims != null && stanza != null) stanza.removeChild(sims);
 					if (stanza != null) stanza.addChild(r.sims());
 				}
-				return client.storeMessages([this]).then(_ -> r);
+				if (storeIfNeeded) return client.storeMessages([this]).then(_ -> r);
 			}
 			return Promise.resolve(r);
 		});
diff --git a/borogove/Client.hx b/borogove/Client.hx
index a2d3f69..8940aab 100644
--- a/borogove/Client.hx
+++ b/borogove/Client.hx
@@ -1772,12 +1772,16 @@ class Client extends EventEmitter {
 		final ps = [];
 		for (attachment in message.attachments) {
 			if (attachment.uris.find(uri -> uri.split("/")[2].split(":")[0].endsWith(chatDomain)) != null) {
-				attachment.lookup(persistence).then(lookedUp -> {
-					if (lookedUp.cachedAt == null) ps.push(fetchAttachment(attachment));
-				});
+				ps.push(attachment.lookup(persistence).then(lookedUp -> {
+					if (lookedUp.cachedAt != null) return Promise.resolve(lookedUp);
+
+					return message.fetchAttachment(attachment, this, false);
+				}));
 			}
 		}
 
+		if (ps.length < 1) return Promise.resolve(false);
+
 		return thenshim.PromiseTools.all(ps).then(_ -> true);
 	}