| author | Stephen Paul Weber
<singpolyma@singpolyma.net> 2026-10-25 21:35:45 UTC |
| committer | Stephen Paul Weber
<singpolyma@singpolyma.net> 2026-10-25 21:35:45 UTC |
| parent | f8153cfe1a3eaad019ec71567f2b4edd7d6aaf14 |
| borogove/Chat.hx | +5 | -2 |
| borogove/Client.hx | +23 | -9 |
| borogove/queries/RosterGet.hx | +10 | -0 |
| borogove/streams/TestStream.hx | +19 | -1 |
| test/TestClient.hx | +184 | -0 |
diff --git a/borogove/Chat.hx b/borogove/Chat.hx index 2346ef1..202d3fc 100644 --- a/borogove/Chat.hx +++ b/borogove/Chat.hx @@ -603,11 +603,14 @@ abstract class Chat extends EventEmitter { @:allow(borogove) private function updateFromRoster(item: RosterItem) { - isBookmarked = true; + final isRemoved = item.subscription == "remove"; + + isBookmarked = !isRemoved; setTrusted(item.subscription == "both" || item.subscription == "from"); + extensions.removeChildren("group", "jabber:iq:roster"); + if (isRemoved) return; if (item.name != null && item.name != "") displayName = item.name; if (uiState == Invited) uiState = Open; - extensions.removeChildren("group", "jabber:iq:roster"); for (group in item.groups) { extensions.textTag("group", group, { xmlns: "jabber:iq:roster" }); } diff --git a/borogove/Client.hx b/borogove/Client.hx index dfe1460..18aafab 100644 --- a/borogove/Client.hx +++ b/borogove/Client.hx @@ -315,11 +315,13 @@ class Client extends EventEmitter { final chatsToUpdate = []; for (item in items) { - if (item.subscription != "remove") { - final chat = getDirectChat(item.jid, false); - chat.updateFromRoster(item); - chatsToUpdate.push(cast (chat, Chat)); - } + final chat = item.subscription == "remove" + ? Util.downcast(getChat(item.jid), DirectChat) + : getDirectChat(item.jid, false); + if (chat == null) continue; + + chat.updateFromRoster(item); + chatsToUpdate.push(cast (chat, Chat)); } persistence.storeChats(this.accountId(), chatsToUpdate); this.trigger("chats/update", chatsToUpdate); @@ -2211,14 +2213,26 @@ class Client extends EventEmitter { var rosterGet = new RosterGet(rosterVer); rosterGet.onFinished(() -> { final chatsToUpdate = []; - // TODO: We should update all chats not in this list as not trusted. - // Special case for when it is empty but there was no <query/> child - // to do nothing is needed also. + final rosterJids: Map<String, Bool> = new Map(); for (item in rosterGet.getResult()) { - var chat = getDirectChat(item.jid, false); + rosterJids[item.jid] = true; + final chat = getDirectChat(item.jid, false); chat.updateFromRoster(item); chatsToUpdate.push(cast (chat, Chat)); } + + // A versioned response with no query means our cached roster is still + // current and any modifications will arrive as roster pushes. + if (rosterGet.hasFullRoster()) { + for (chat in chats) { + final directChat = Util.downcast(chat, DirectChat); + if (directChat == null || rosterJids.exists(directChat.chatId)) continue; + + directChat.updateFromRoster(new Stanza("item", { jid: directChat.chatId, subscription: "remove" })); + chatsToUpdate.push(cast (directChat, Chat)); + } + } + if (rosterGet.ver != null) { rosterVer = rosterGet.ver; persistence.storeLogin(accountId(), stream.clientId ?? jid.resource, displayName(), rosterVer, null); diff --git a/borogove/queries/RosterGet.hx b/borogove/queries/RosterGet.hx index 2e7f66f..002d3ac 100644 --- a/borogove/queries/RosterGet.hx +++ b/borogove/queries/RosterGet.hx @@ -31,6 +31,16 @@ class RosterGet extends GenericQuery { finish(); } + /** + Whether the response contained the full roster. + + A versioned response may contain no query at all when the cached roster + is still current; that is different from an empty roster query. + **/ + public function hasFullRoster():Bool { + return responseStanza != null && responseStanza.getChild("query", xmlns) != null; + } + public function getResult() { if (responseStanza == null) { return []; diff --git a/borogove/streams/TestStream.hx b/borogove/streams/TestStream.hx index 3242eee..5bef1a7 100644 --- a/borogove/streams/TestStream.hx +++ b/borogove/streams/TestStream.hx @@ -8,6 +8,8 @@ import borogove.GenericStream; import borogove.Stanza; class TestStream extends GenericStream { + private final iqHandlers: Map<IqRequestType, Map<String, Stanza->IqResult>> = [IqRequestType.Get => [], IqRequestType.Set => []]; + public function register(domain: String, preAuth: Null<String>) { return Promise.resolve(null); } @@ -22,5 +24,21 @@ class TestStream extends GenericStream { this.trigger("sendStanza", stanza); } - public function onIq(type:IqRequestType, tag:String, xmlns:String, handler:(Stanza)->IqResult):Void { } + public function onIq(type:IqRequestType, tag:String, xmlns:String, handler:(Stanza)->IqResult):Void { + iqHandlers[type]['{' + xmlns + '}' + tag] = handler; + } + + /** + Dispatch an incoming IQ request to the handler registered for it. + **/ + public function dispatchIq(stanza:Stanza):IqResult { + final child = stanza.getFirstChild(); + if (child == null) return IqNoResult; + + final type = stanza.attr.get("type") == "get" ? IqRequestType.Get : IqRequestType.Set; + final handler = iqHandlers[type]['{' + child.attr.get("xmlns") + '}' + child.name]; + if (handler == null) return IqNoResult; + + return handler(stanza); + } } diff --git a/test/TestClient.hx b/test/TestClient.hx index 0bb6ea9..68023b2 100644 --- a/test/TestClient.hx +++ b/test/TestClient.hx @@ -599,6 +599,190 @@ class TestClient extends utest.Test { client.stream.onStanza(stanza); } + public function testLiveRosterRemovalClearsRosterState() { + final persistence = new Dummy(); + final client = new Client("test@example.com", persistence); + final chat = client.getDirectChat("bob@example.com"); + final stream = Std.downcast(client.stream, borogove.streams.TestStream); + + stream.dispatchIq( + new Stanza("iq", { xmlns: "jabber:client", type: "set", from: "example.com" }) + .tag("query", { xmlns: "jabber:iq:roster" }) + .tag("item", { jid: "bob@example.com", name: "Bob", subscription: "both" }) + ); + + Assert.isTrue(chat.isTrusted()); + Assert.isTrue(chat.isBookmarked); + Assert.equals("Bob", chat.getDisplayName()); + + chat.setTags(["friends"]); + Assert.same(["friends"], chat.getTags()); + + chat.uiState = Invited; + + stream.dispatchIq( + new Stanza("iq", { xmlns: "jabber:client", type: "set", from: "example.com" }) + .tag("query", { xmlns: "jabber:iq:roster" }) + .tag("item", { jid: "bob@example.com", subscription: "remove" }) + ); + + Assert.isFalse(chat.isTrusted()); + Assert.isFalse(chat.isBookmarked); + Assert.same([], chat.getTags()); + Assert.equals("Bob", chat.displayName); + Assert.equals(Invited, chat.uiState); + } + + public function testLiveRosterRemovalDoesNotCreateChat() { + final persistence = new Dummy(); + final client = new Client("test@example.com", persistence); + final stream = Std.downcast(client.stream, borogove.streams.TestStream); + + stream.dispatchIq( + new Stanza("iq", { xmlns: "jabber:client", type: "set", from: "example.com" }) + .tag("query", { xmlns: "jabber:iq:roster" }) + .tag("item", { jid: "nobody@example.com", subscription: "remove" }) + ); + + Assert.isNull(client.getChat("nobody@example.com")); + } + + public function testLiveRosterSubscriptionChangeUntrustsChat() { + final persistence = new Dummy(); + final client = new Client("test@example.com", persistence); + final chat = client.getDirectChat("bob@example.com"); + chat.setTrusted(true); + + final stream = Std.downcast(client.stream, borogove.streams.TestStream); + stream.dispatchIq( + new Stanza("iq", { xmlns: "jabber:client", type: "set", from: "example.com" }) + .tag("query", { xmlns: "jabber:iq:roster" }) + .tag("item", { jid: "bob@example.com", subscription: "to" }) + ); + + Assert.isFalse(chat.isTrusted()); + } + + public function testRosterGetFullRosterMissingItemClearsRosterState() { + final persistence = new Dummy(); + final client = new Client("test@example.com", persistence); + final chat = client.getDirectChat("bob@example.com"); + chat.setTrusted(true); + chat.isBookmarked = true; + chat.setTags(["friends"]); + chat.displayName = "Bob"; + chat.uiState = Invited; + + client.stream.on("sendStanza", (stanza: Stanza) -> { + if (stanza.name == "iq" && stanza.attr.get("type") == "get" && stanza.getChild("query", "jabber:iq:roster") != null) { + client.stream.onStanza( + new Stanza("iq", { xmlns: "jabber:client", type: "result", from: "example.com", id: stanza.attr.get("id") }) + .tag("query", { xmlns: "jabber:iq:roster", ver: "ver-1" }) + .tag("item", { jid: "carol@example.com", subscription: "both" }) + ); + return EventHandled; + } + return EventUnhandled; + }); + + client.rosterGet(); + + Assert.isFalse(chat.isTrusted()); + Assert.isFalse(chat.isBookmarked); + Assert.same([], chat.getTags()); + Assert.equals("Bob", chat.displayName); + Assert.equals(Invited, chat.uiState); + } + + public function testRosterGetCachedVersionDoesNotClearRosterState() { + final persistence = new Dummy(); + final client = new Client("test@example.com", persistence); + final chat = client.getDirectChat("bob@example.com"); + chat.setTrusted(true); + chat.isBookmarked = true; + chat.setTags(["friends"]); + chat.displayName = "Bob"; + chat.uiState = Invited; + + client.stream.on("sendStanza", (stanza: Stanza) -> { + if (stanza.name == "iq" && stanza.attr.get("type") == "get" && stanza.getChild("query", "jabber:iq:roster") != null) { + client.stream.onStanza( + new Stanza("iq", { xmlns: "jabber:client", type: "result", from: "example.com", id: stanza.attr.get("id") }) + ); + return EventHandled; + } + return EventUnhandled; + }); + + client.rosterGet(); + + Assert.isTrue(chat.isTrusted()); + Assert.isTrue(chat.isBookmarked); + Assert.same(["friends"], chat.getTags()); + Assert.equals("Bob", chat.displayName); + Assert.equals(Invited, chat.uiState); + } + + public function testRosterGetInterimRemovePushClearsRosterState() { + final persistence = new Dummy(); + final client = new Client("test@example.com", persistence); + final chat = client.getDirectChat("bob@example.com"); + chat.setTrusted(true); + chat.isBookmarked = true; + chat.setTags(["friends"]); + chat.displayName = "Bob"; + chat.uiState = Invited; + + client.stream.on("sendStanza", (stanza: Stanza) -> { + if (stanza.name == "iq" && stanza.attr.get("type") == "get" && stanza.getChild("query", "jabber:iq:roster") != null) { + client.stream.onStanza( + new Stanza("iq", { xmlns: "jabber:client", type: "result", from: "example.com", id: stanza.attr.get("id") }) + ); + return EventHandled; + } + return EventUnhandled; + }); + + client.rosterGet(); + Assert.isTrue(chat.isTrusted()); + + final stream = Std.downcast(client.stream, borogove.streams.TestStream); + stream.dispatchIq( + new Stanza("iq", { xmlns: "jabber:client", type: "set", from: "example.com" }) + .tag("query", { xmlns: "jabber:iq:roster", ver: "ver-2" }) + .tag("item", { jid: "bob@example.com", subscription: "remove" }) + ); + + Assert.isFalse(chat.isTrusted()); + Assert.isFalse(chat.isBookmarked); + Assert.same([], chat.getTags()); + Assert.equals("Bob", chat.displayName); + Assert.equals(Invited, chat.uiState); + } + + public function testRosterGetSubscriptionChangeUntrustsChat() { + final persistence = new Dummy(); + final client = new Client("test@example.com", persistence); + final chat = client.getDirectChat("bob@example.com"); + chat.setTrusted(true); + + client.stream.on("sendStanza", (stanza: Stanza) -> { + if (stanza.name == "iq" && stanza.attr.get("type") == "get" && stanza.getChild("query", "jabber:iq:roster") != null) { + client.stream.onStanza( + new Stanza("iq", { xmlns: "jabber:client", type: "result", from: "example.com", id: stanza.attr.get("id") }) + .tag("query", { xmlns: "jabber:iq:roster", ver: "ver-1" }) + .tag("item", { jid: "bob@example.com", subscription: "to" }) + ); + return EventHandled; + } + return EventUnhandled; + }); + + client.rosterGet(); + + Assert.isFalse(chat.isTrusted()); + } + public function testSortAfterDirectChat() { final persistence = new Dummy(); final client = new Client("test@example.com", persistence);