diff --git a/public/index.html b/public/index.html index e87b327..92e0d38 100644 --- a/public/index.html +++ b/public/index.html @@ -66,54 +66,58 @@
-
-
- - - -
-
Peers Discovered ({{ peersCount }})
-
-
- -
-
-
-
- -
-
-
{{ peer.app_data || "Anonymous Peer" }}
-
{{ formatTimeAgo(peer.last_announce_timestamp) }}
-
+ + +
+
+ +
Peers
-
- -
-
- - - -
-
No Peers Discovered
-
Waiting for someone to announce!
+ +
@@ -159,7 +163,7 @@
-
{{ selectedPeer.app_data || "Unknown" }}
+
{{ selectedPeer.name }}
@<{{ selectedPeer.destination_hash }}>
@@ -197,7 +201,7 @@
-
{{ selectedPeer.app_data || "Unknown" }}
+
{{ selectedPeer.name }}
@@ -406,6 +410,9 @@ displayName: "Anonymous Peer", config: null, + lxmfDeliveryAnnounces: [], + + tab: "peers", peers: {}, peersSearchTerm: "", selectedPeer: null, @@ -420,6 +427,7 @@ }, mounted: function() { this.connectWebsocket(); + this.getLxmfDeliveryAnnounces(); }, methods: { connectWebsocket: function() { @@ -450,20 +458,9 @@ break; } case 'announce': { - this.peers[json.announce.destination_hash] = { - destination_hash: json.announce.destination_hash, - app_data: json.announce.app_data, - last_announce_timestamp: json.announce.last_announce_timestamp, - } - break; - } - case 'known_peers': { - for(const known_peer of json.known_peers){ - this.peers[known_peer.destination_hash] = { - destination_hash: known_peer.destination_hash, - app_data: known_peer.app_data, - last_announce_timestamp: known_peer.last_announce_timestamp, - } + const aspect = json.announce.aspect; + if(aspect === "lxmf.delivery"){ + this.updatePeerFromAnnounce(json.announce); } break; } @@ -784,6 +781,41 @@ } }, + async getLxmfDeliveryAnnounces() { + try { + + // fetch announces for "lxmf.delivery" aspect + const response = await window.axios.get(`/api/v1/announces`, { + params: { + aspect: "lxmf.delivery", + }, + }); + + // update ui + const lxmfDeliveryAnnounces = response.data.announces; + for(const lxmfDeliveryAnnounce of lxmfDeliveryAnnounces){ + this.updatePeerFromAnnounce(lxmfDeliveryAnnounce); + } + + } catch(e) { + // do nothing if failed to load announces + } + }, + getPeerNameFromAppData: function(appData) { + try { + // app data should be peer name, and our server provides it base64 encoded + return atob(appData); + } catch(e){ + return "Anonymous Peer"; + } + }, + updatePeerFromAnnounce: function(announce) { + this.peers[announce.destination_hash] = { + ...announce, + // helper property for easily grabbing peer name from app data + name: this.getPeerNameFromAppData(announce.app_data), + }; + }, async loadLxmfMessages(destinationHash) { const seq = ++this.lxmfMessagesRequestSequence; try { @@ -891,9 +923,10 @@ seconds: seconds, }; }, - formatTimeAgo: function(seconds) { + formatTimeAgo: function(datetimeString) { - const secondsAgo = Math.round((Date.now() / 1000) - seconds); + const millisecondsAgo = Date.now() - new Date(datetimeString).getTime(); + const secondsAgo = Math.round(millisecondsAgo / 1000); const parsedSeconds = this.parseSeconds(secondsAgo); if(parsedSeconds.days > 0){ @@ -1029,15 +1062,17 @@ peersOrderedByLatestAnnounce() { const peers = Object.values(this.peers); return peers.sort(function(peerA, peerB) { - // order by last_announce_timestamp desc - return peerB.last_announce_timestamp - peerA.last_announce_timestamp; + // order by updated_at desc + const peerAUpdatedAt = new Date(peerA.updated_at).getTime(); + const peerBUpdatedAt = new Date(peerB.updated_at).getTime(); + return peerBUpdatedAt - peerAUpdatedAt; }); }, searchedPeers() { return this.peersOrderedByLatestAnnounce.filter((peer) => { const search = this.peersSearchTerm.toLowerCase(); - const matchesAppData = (peer.app_data || "").toLowerCase().includes(search); - const matchesDestinationHash = (peer.destination_hash || "").toLowerCase().includes(search); + const matchesAppData = peer.name.toLowerCase().includes(search); + const matchesDestinationHash = peer.destination_hash.toLowerCase().includes(search); return matchesAppData || matchesDestinationHash; }); }, diff --git a/web.py b/web.py index db5abd8..ecc045f 100644 --- a/web.py +++ b/web.py @@ -4,7 +4,6 @@ import argparse import json import os import signal -import time from datetime import datetime, timezone from typing import Callable, List @@ -121,9 +120,6 @@ class ReticulumWebChat: # send config to all clients await self.send_config_to_websocket_clients() - # send known peers to all clients - await self.send_known_peers_to_websocket_clients() - # handle websocket messages until disconnected async for msg in websocket_response: msg: WSMessage = msg @@ -164,22 +160,13 @@ class ReticulumWebChat: # process announces announces = [] for announce in query_results: - announces.append({ - "id": announce.id, - "destination_hash": announce.destination_hash, - "aspect": announce.aspect, - "identity_hash": announce.identity_hash, - "identity_public_key": announce.identity_public_key, - "app_data": announce.app_data, - "created_at": announce.created_at, - "updated_at": announce.updated_at, - }) + announces.append(self.convert_db_announce_to_dict(announce)) return web.json_response({ "announces": announces, }) - # serve lxmf messages + # delete lxmf message @routes.delete("/api/v1/lxmf-messages/{id}") async def index(request): @@ -199,7 +186,7 @@ class ReticulumWebChat: "message": "ok", }) - # serve lxmf messages + # serve lxmf messages for conversation @routes.get("/api/v1/lxmf-messages/conversation/{destination_hash}") async def index(request): @@ -459,26 +446,6 @@ class ReticulumWebChat: }, })) - # broadcasts known peers to all websocket clients - async def send_known_peers_to_websocket_clients(self): - - # process known peers - known_peers = [] - for destination_hash in RNS.Identity.known_destinations: - known_destination = RNS.Identity.known_destinations[destination_hash] - last_announce_timestamp = known_destination[0] - known_peers.append({ - "destination_hash": destination_hash.hex(), - "app_data": self.convert_app_data_to_string(RNS.Identity.recall_app_data(destination_hash)), - "last_announce_timestamp": last_announce_timestamp, - }) - - # send known peers to websocket clients - await self.websocket_broadcast(json.dumps({ - "type": "known_peers", - "known_peers": known_peers, - })) - # convert app data to string, or return none unable to do so def convert_app_data_to_string(self, app_data): @@ -564,6 +531,19 @@ class ReticulumWebChat: return lxmf_message_state + # convert database announce to a dictionary + def convert_db_announce_to_dict(self, announce: database.Announce): + return { + "id": announce.id, + "destination_hash": announce.destination_hash, + "aspect": announce.aspect, + "identity_hash": announce.identity_hash, + "identity_public_key": announce.identity_public_key, + "app_data": announce.app_data, + "created_at": announce.created_at, + "updated_at": announce.updated_at, + } + # handle an lxmf delivery from reticulum # NOTE: cant be async, as Reticulum doesn't await it def on_lxmf_delivery(self, lxmf_message): @@ -742,19 +722,15 @@ class ReticulumWebChat: # upsert announce to database self.db_upsert_announce(announced_identity, destination_hash, "lxmf.delivery", app_data) - # parse app data - parsed_app_data = None - if app_data is not None: - parsed_app_data = app_data.decode("utf-8") + # find announce from database + announce = database.Announce.get_or_none(database.Announce.destination_hash == destination_hash.hex()) + if announce is None: + return - # send received lxmf announce to all websocket clients + # send database announce to all websocket clients asyncio.run(self.websocket_broadcast(json.dumps({ "type": "announce", - "announce": { - "destination_hash": destination_hash.hex(), - "app_data": parsed_app_data, - "last_announce_timestamp": time.time(), - }, + "announce": self.convert_db_announce_to_dict(announce), }))) # handle an announce received from reticulum, for a nomadnet node @@ -767,6 +743,17 @@ class ReticulumWebChat: # upsert announce to database self.db_upsert_announce(announced_identity, destination_hash, "nomadnetwork.node", app_data) + # find announce from database + announce = database.Announce.get_or_none(database.Announce.destination_hash == destination_hash.hex()) + if announce is None: + return + + # send database announce to all websocket clients + asyncio.run(self.websocket_broadcast(json.dumps({ + "type": "announce", + "announce": self.convert_db_announce_to_dict(announce), + }))) + # class to manage config stored in database class Config: