From 9c8d0c0af2c8dcf9c5cb81d581e76ef1464111d2 Mon Sep 17 00:00:00 2001 From: vishalxl <> Date: Wed, 27 Jul 2022 21:17:31 +0530 Subject: [PATCH] added mention of relay for an event, also printed it --- bin/nostr_console.dart | 5 +++-- lib/nostr_console.dart | 27 +++++++++++++++++--------- lib/relays.dart | 44 ++++++++++++++++++++++++------------------ 3 files changed, 46 insertions(+), 30 deletions(-) diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index 50751d8..7bf27da 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -21,14 +21,15 @@ Future main() async { var e = events[i]; if( e.eventData.kind == 3) { print('calling getfeed'); - getFeed(e.eventData.contactList, events, 3); + getFeed(e.eventData.contactList, events, 5); } } print('waiting for feed to come in'); Future.delayed(const Duration(milliseconds: 4000), () { - events.sort(ascendingTime); + //events.sort(ascendingTime); + events.removeWhere( (item) => item.eventData.kind != 1 ); print('====================all events ================='); printEvents(events); print('number of all events: ${events.length}'); diff --git a/lib/nostr_console.dart b/lib/nostr_console.dart index 5b4ca66..999de55 100644 --- a/lib/nostr_console.dart +++ b/lib/nostr_console.dart @@ -1,6 +1,6 @@ int keyLenPrinted = 6; -String defaultServerUrl = 'wss://nostr-pub.wellorder.net'; +String defaultServerUrl = 'wss://nostr.onsats.org'; class Contact { @@ -37,7 +37,10 @@ class EventData { var n = tag.length; String server = defaultServerUrl; if( n >=3 ) { - server = tag[2].toString(); + server = tag[2].toString(); + if( server == 'wss://nostr.rocks') { + server = 'wss://nostr.onsats.org'; + } } Contact c = Contact(tag[1] as String, server, 3.toString()); contactList.add(c); @@ -61,8 +64,10 @@ class EventData { String max3(String v) => v.length > 3? v.substring(0,3) : v.substring(0, v.length); DateTime dTime = DateTime.fromMillisecondsSinceEpoch(createdAt *1000); - - return '-------+\nid : ${max3(id)} \nAuthor : ${max3(pubkey)}\nTime : $dTime\nKind : $kind\nMessage: $content\n'; + if( createdAt == 0) { + print("createdAt == 0 for event $content"); + } + return '\n-------+\nAuthor : ${max3(pubkey)}\nMessage: $content\n\nid : ${max3(id)} Time: $dTime Kind: $kind'; } } @@ -71,25 +76,29 @@ class Event { String event; String id; EventData eventData; - Event(this.event, this.id, this.eventData); - factory Event.fromJson(dynamic json) { + Event(this.event, this.id, this.eventData, this.seenOnRelays); + + List seenOnRelays; + + factory Event.fromJson(dynamic json, String relay) { if( json.length < 3) { String e = ""; e = json.length > 1? json[0]: ""; - return Event(e,"",EventData("non","","", 0, 0, [])); + return Event(e,"",EventData("non","","", 0, 0, []), [relay]); } - return Event(json[0] as String, json[1] as String, EventData.fromJson(json[2]) ); + return Event(json[0] as String, json[1] as String, EventData.fromJson(json[2]), [relay] ); } @override String toString() { - return '$eventData'; + return '$eventData Seen on: ${seenOnRelays[0]}\n'; } } int ascendingTime(Event a, Event b) { if(a.eventData.createdAt < b.eventData.createdAt) { + print( 'ascendingTime : comparing two ${a.eventData.createdAt} and ${b.eventData.createdAt}'); return 0; } diff --git a/lib/relays.dart b/lib/relays.dart index 202487e..6f096bc 100644 --- a/lib/relays.dart +++ b/lib/relays.dart @@ -11,6 +11,10 @@ String getSubscriptionRequest(String publicKey, int numUserEvents) { return strSubscription1 + publicKey + strSubscription2; } +void handleSocketError() { + +} + class Relays { Map > relays; @@ -24,6 +28,7 @@ class Relays { return Relays(r); } + void connect(String relay, String publicKey, List events, int numUserEvents) { Future? fws; if(relays.containsKey(relay)) { @@ -36,25 +41,26 @@ class Relays { fws = WebSocket.connect(relay); relays[relay] = fws; fws.then((WebSocket ws) { - ws.listen( - (d) { - print(d); - Event e; - try { - e = Event.fromJson(jsonDecode(d)); - events.add(e); - if( e.eventData.kind == 3) { - + ws.listen( + (d) { + print(d); + Event e; + try { + e = Event.fromJson(jsonDecode(d), relay); + events.add(e); + if( e.eventData.kind == 3) { + + } + } on FormatException { + print( 'exception in fromJson for event'); } - } on FormatException { - print( 'exception in fromJson for event'); - } - }, - onError: (e) { print("error"); print(e); }, - onDone: () { print('in onDone'); ws.close() ; } - );}); - - + }, + onError: (e) { print("error"); print(e); }, + onDone: () { print('in onDone'); ws.close() ; } + );}).catchError((err) { + print('Error: Could not connect to $relay'); + //throw Exception('Some arbitrary error'); + }); } on WebSocketException { print('WebSocketException exception'); return; @@ -64,7 +70,7 @@ class Relays { } } - print('sending request ${getSubscriptionRequest(publicKey, numUserEvents)}'); + print('sending request ${getSubscriptionRequest(publicKey, numUserEvents)} to $relay'); fws?.then((WebSocket ws) { ws.add(getSubscriptionRequest(publicKey, numUserEvents)); }); }