From 1906e1f892768ca91973293b94ac8eb729f9cefb Mon Sep 17 00:00:00 2001 From: Matthew Wild <mwild1@gmail.com> Date: Thu, 25 Jun 2020 16:42:09 +0100 Subject: [PATCH] bugfix: subscribe to federated servers for unread notifications --- src/plugins/converse-rai.js | 59 ++++++++++++++++++++++++------------- src/solid-xmpp-chat.js | 3 +- 2 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/plugins/converse-rai.js b/src/plugins/converse-rai.js index 33ba090..846ff51 100644 --- a/src/plugins/converse-rai.js +++ b/src/plugins/converse-rai.js @@ -6,6 +6,7 @@ } }(this, function (converse) { var Strophe, $iq, $msg, $pres, $build, b64_sha1, _ ,Backbone, dayjs, _converse; + var subscribedServers = new Set(); converse.plugins.add("rai", { 'dependencies': [], @@ -29,9 +30,7 @@ }); _converse.api.listen.on('connected', function () { - setupRoomActivityIndicators(function (supported) { - if (supported) listenForRoomActivityIndicators(); - }); + setupRoomActivityIndicators(); }); _converse.api.listen.on('chatRoomViewInitialized', function (view) @@ -103,27 +102,47 @@ } }); - function setupRoomActivityIndicators(callback) - { - try { - const id = Math.random().toString(36).substr(2, 9); - const to = "conference." + _converse.domain; - _converse.connection.send(converse.env.$pres({ - to: to, - id: id - }).c('rai', { - 'xmlns': "xmpp:prosody.im/protocol/rai" - })); - - if (callback) callback(true); - } catch (e) { - if (callback) callback(false); + + function subscribeServer(server_name) { + console.debug("RAI: Subscribing to " + server_name); + const id = Math.random().toString(36).substr(2, 9); + _converse.connection.send(converse.env.$pres({ + to: server_name, + id: id + }).c('rai', { + 'xmlns': "xmpp:prosody.im/protocol/rai" + })); + } + + function unsubscribeServer(server_name) { + console.debug("RAI: Unsubscribing from " + server_name); + _converse.connection.send(converse.env.$pres({ + to: server_name, type: "unavailable" + }).c('rai', { + 'xmlns': "xmpp:prosody.im/protocol/rai" + })); + } + + function updateSubscriptions(rooms) { + console.debug("RAI: Updating subscriptions") + var updated_interesting_servers = new Set(rooms.map(Strophe.getDomainFromJid)); + var new_servers = new Set([...updated_interesting_servers].filter(server => !subscribedServers.has(server))); + var obsolete_servers = new Set([...subscribedServers].filter(server => !updated_interesting_servers.has(server))); + console.log("interesting servers", updated_interesting_servers); + console.log("current servers", subscribedServers); + console.log("new servers", new_servers); + console.log("obsolete servers", obsolete_servers); + for(let server of obsolete_servers) { + unsubscribeServer(server); + } + for(let server of new_servers) { + subscribeServer(server); } } - function listenForRoomActivityIndicators() + function setupRoomActivityIndicators() { - console.debug("listenForRoomActivityIndicators"); + _converse.api.listen.on('raiRoomsUpdated', updateSubscriptions); // If we already have unread notifications stored for this session, emit them now for (var i = 0; i < sessionStorage.length; i++) diff --git a/src/solid-xmpp-chat.js b/src/solid-xmpp-chat.js index 694ff5a..702b310 100644 --- a/src/solid-xmpp-chat.js +++ b/src/solid-xmpp-chat.js @@ -194,7 +194,8 @@ export const SolidXMPPChat = { ])).flat(); // @MattJ Here userRooms is an array of each jabberID the user is on. - console.log(userRooms); + console.log("userRooms", userRooms); + this._converse.api.trigger('raiRoomsUpdated', userRooms); this._converse.api.listen.on('chatRoomActivityIndicators', function (jid) { window.dispatchEvent(new CustomEvent('newMessage', { -- GitLab