From 585fc4c789a5629580063d22e8e2c84eb187219b Mon Sep 17 00:00:00 2001 From: liamcottle Date: Mon, 6 May 2024 00:10:48 +1200 Subject: [PATCH] implement tab to display all known nomadnetwork nodes --- public/index.html | 121 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 1 deletion(-) diff --git a/public/index.html b/public/index.html index b6f6a05..0f052c8 100644 --- a/public/index.html +++ b/public/index.html @@ -64,7 +64,7 @@
- +
@@ -72,9 +72,11 @@
Peers
+
Nodes
+ + + +
@@ -421,6 +471,10 @@ peersSearchTerm: "", selectedPeer: null, + nodes: {}, + nodesSearchTerm: "", + selectedNode: null, + lxmfMessagesRequestSequence: 0, chatItems: [], @@ -432,6 +486,7 @@ mounted: function() { this.connectWebsocket(); this.getLxmfDeliveryAnnounces(); + this.getNomadnetworkNodeAnnounces(); }, methods: { connectWebsocket: function() { @@ -465,6 +520,8 @@ const aspect = json.announce.aspect; if(aspect === "lxmf.delivery"){ this.updatePeerFromAnnounce(json.announce); + } else if(aspect === "nomadnetwork.node"){ + this.updateNodeFromAnnounce(json.announce); } break; } @@ -803,6 +860,28 @@ } catch(e) { // do nothing if failed to load announces + console.log(e); + } + }, + async getNomadnetworkNodeAnnounces() { + try { + + // fetch announces for "nomadnetwork.node" aspect + const response = await window.axios.get(`/api/v1/announces`, { + params: { + aspect: "nomadnetwork.node", + }, + }); + + // update ui + const nodeAnnounces = response.data.announces; + for(const nodeAnnounce of nodeAnnounces){ + this.updateNodeFromAnnounce(nodeAnnounce); + } + + } catch(e) { + // do nothing if failed to load announces + console.log(e); } }, getPeerNameFromAppData: function(appData) { @@ -813,6 +892,14 @@ return "Anonymous Peer"; } }, + getNodeNameFromAppData: function(appData) { + try { + // app data should be node name, and our server provides it base64 encoded + return atob(appData); + } catch(e){ + return "Anonymous Node"; + } + }, updatePeerFromAnnounce: function(announce) { this.peers[announce.destination_hash] = { ...announce, @@ -820,6 +907,13 @@ name: this.getPeerNameFromAppData(announce.app_data), }; }, + updateNodeFromAnnounce: function(announce) { + this.nodes[announce.destination_hash] = { + ...announce, + // helper property for easily grabbing node name from app data + name: this.getNodeNameFromAppData(announce.app_data), + }; + }, async loadLxmfMessages(destinationHash) { const seq = ++this.lxmfMessagesRequestSequence; try { @@ -910,10 +1004,15 @@ }, onPeerClick: function(peer) { + this.selectedNode = null; this.selectedPeer = peer; this.chatItems = []; this.loadLxmfMessages(peer.destination_hash); }, + onNodeClick: function(node) { + this.selectedPeer = null; + this.selectedNode = node; + }, parseSeconds: function(secondsToFormat) { secondsToFormat = Number(secondsToFormat); var days = Math.floor(secondsToFormat / (3600 * 24)); @@ -1102,6 +1201,26 @@ return []; }, + nodesCount() { + return Object.keys(this.nodes).length; + }, + nodesOrderedByLatestAnnounce() { + const nodes = Object.values(this.nodes); + return nodes.sort(function(nodeA, nodeB) { + // order by updated_at desc + const nodeAUpdatedAt = new Date(nodeA.updated_at).getTime(); + const nodeBUpdatedAt = new Date(nodeB.updated_at).getTime(); + return nodeBUpdatedAt - nodeAUpdatedAt; + }); + }, + searchedNodes() { + return this.nodesOrderedByLatestAnnounce.filter((node) => { + const search = this.nodesSearchTerm.toLowerCase(); + const matchesAppData = node.name.toLowerCase().includes(search); + const matchesDestinationHash = node.destination_hash.toLowerCase().includes(search); + return matchesAppData || matchesDestinationHash; + }); + }, canSendMessage() { // can't send if empty message