diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index 46ac984..a8d6163 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -10,11 +10,11 @@ var userPublickey = "3235036bd0957dfb27ccda02d452d7c763be40c91a1ac082ba6983b2 //var userPublickey = "ed1d0e1f743a7d19aa2dfb0162df73bacdbc699f67cc55bb91a98c35f7deac69"; // melvin //var userPublickey = "52b4a076bcbbbdc3a1aefa3735816cf74993b1b8db202b01c883c58be7fad8bd"; // semisol - // program arguments const String requestArg = "request"; const String userArg = "user"; const String lastdaysArg = "days"; +const String relayArg = "relay"; // by default the threads that were started in last two days are shown // this can be changed with 'days' command line argument @@ -55,27 +55,34 @@ void printEventsAsTree(events) { Future main(List arguments) async { List events = []; - final parser = ArgParser()..addOption(requestArg, abbr: 'r') + final parser = ArgParser()..addOption(requestArg, abbr: 'q') ..addOption(userArg, abbr:"u") - ..addOption(lastdaysArg, abbr:"d"); + ..addOption(lastdaysArg, abbr:"d") + ..addOption(relayArg, abbr:"r"); ArgResults argResults = parser.parse(arguments); + if( argResults[relayArg] != null) { + defaultServerUrl = argResults[relayArg]; + print("Going to use relay: $defaultServerUrl"); + } + if( argResults[requestArg] != null) { - stdout.write("got argument request ${argResults[requestArg]}"); + stdout.write("Got argument request ${argResults[requestArg]}"); sendRequest("wss://nostr-pub.wellorder.net", argResults[requestArg], events); Future.delayed(const Duration(milliseconds: 6000), () { printEventsAsTree(events); exit(0); }); return; - } else { - if( argResults[userArg] != null) { - userPublickey = argResults[userArg]; - } - if( argResults[lastdaysArg] != null) { - numLastDays = int.parse(argResults[lastdaysArg]); - print("Going to show posts for last $numLastDays days"); - } + } + + if( argResults[userArg] != null) { + userPublickey = argResults[userArg]; + } + if( argResults[lastdaysArg] != null) { + + numLastDays = int.parse(argResults[lastdaysArg]); + print("Going to show posts for last $numLastDays days"); } // the default in case no arguments are given is: @@ -91,13 +98,11 @@ Future main(List arguments) async { Future.delayed(const Duration(milliseconds: numWaitSeconds), () { // count user events events.forEach((element) { element.eventData.kind == 1? numUserEvents++: numUserEvents;}); - stdout.write(".. got ${events.length} total events\n"); + stdout.write(".. received ${events.length} events made by the user\n"); // get user's feed ( from follows by looking at kind 3 event) List contactList = []; int latestContactsTime = 0; - - print("processing contact, event of kind 3"); int latestContactIndex = -1; for( int i = 0; i < events.length; i++) { var e = events[i]; @@ -108,8 +113,6 @@ Future main(List arguments) async { } if (latestContactIndex != -1) { - events[latestContactIndex].printEvent(0); - print("got latestContactIndex = $latestContactIndex"); contactList = getContactFeed(events[latestContactIndex].eventData.contactList, events, 300); print("number of contacts = ${contactList.length}"); } @@ -120,19 +123,19 @@ Future main(List arguments) async { // count feed events events.forEach((element) { element.eventData.kind == 1? numFeedEvents++: numFeedEvents;}); numFeedEvents = numFeedEvents - numUserEvents; - stdout.write("received $numFeedEvents from the follows\n"); + stdout.write("received $numFeedEvents events from the follows\n"); // get mentioned ptags, and then get the events for those users List pTags = getpTags(events); - print("Total number of pTags = ${pTags.length}\n"); getMultiUserEvents(defaultServerUrl, pTags, events, 300); - print('waiting for rest of events to come in....'); + print('Waiting for rest of events to come in....'); Future.delayed(const Duration(milliseconds: numWaitSeconds * 1), () { // count other events events.forEach((element) { element.eventData.kind == 1? numOtherEvents++: numOtherEvents;}); numOtherEvents = numOtherEvents - numFeedEvents - numUserEvents; + stdout.write("received $numOtherEvents other events\n"); printEventsAsTree(events); diff --git a/lib/nostr_console_ds.dart b/lib/nostr_console_ds.dart index 9b45c39..e0dbd9d 100644 --- a/lib/nostr_console_ds.dart +++ b/lib/nostr_console_ds.dart @@ -6,6 +6,8 @@ const int screenWidth = 120; const bool enableVerticalLines = false; const int spacesPerDepth = 8; int keyLenPrinted = 6; + +//String defaultServerUrl = 'wss://relay.damus.io'; String defaultServerUrl = 'wss://nostr-relay.untethr.me'; Map gKindONames = {}; // global names from kind 0 events @@ -14,13 +16,14 @@ List gBots = [ "3b57518d02e6acfd5eb7198530b2e351e5a52278fb2499d14b66db2 "887645fef0ce0c3c1218d2f5d8e6132a19304cdc57cd20281d082f38cfea0072" // bestofhn ]; +const int gDebug = 0; + // If given event is kind 0 event, then populates gKindONames with that info void processKind0Event(Event e) { if( e.eventData.kind != 0) { return; } - //print("In getNames: for event content: ${e.eventData.content}"); String content = e.eventData.content; if( content.isEmpty) { return; @@ -31,7 +34,7 @@ void processKind0Event(Event e) { gKindONames[e.eventData.pubkey] = json["name"]??""; } } catch(ex) { - print("Warning: In getNames: caught exception $ex for content ${e.eventData.content}"); + if( gDebug != 0) print("Warning: In processKind0Event: caught exception for content: ${e.eventData.content}"); } } @@ -253,8 +256,9 @@ class Tree { // @method create top level Tree from events. // first create a map. then add all top trees to the final list/ChildTrees. then add children to it. factory Tree.fromEvents(List events) { - stdout.write("in factory fromEvents list. number of events: ${events.length}\n"); - + if( events.isEmpty) { + return Tree(Event("","",EventData("non","", 0, 0, "", "", [], [], []), [""], "[json]"), []); + } // create a map from list of events, key is eventId and value is event itself Map mAllEvents = {}; events.forEach((element) { mAllEvents[element.eventData.id] = Tree(element, []); }); @@ -303,7 +307,6 @@ class Tree { } } - stdout.write("Ending: factory fromEvents list. number of processed events: ${processed.length}\n"); return Tree( events[0], topLevelTrees); // TODO remove events[0] } // end fromEvents() @@ -357,7 +360,6 @@ List getpTags(List events) { Set tempPtags = {}; pTags.retainWhere((x) => tempPtags.add(x)); - print("In getPtags. returning number of ptags = ${pTags.length}"); return pTags; } diff --git a/lib/relays.dart b/lib/relays.dart index 80ed1e6..458d836 100644 --- a/lib/relays.dart +++ b/lib/relays.dart @@ -49,7 +49,7 @@ class Relays { for(int i = 0; i < gBots.length; i++) { if( publicKey == gBots[i]) { - print("In gerUserEvents: ignoring bot: $publicKey"); + //print("In gerUserEvents: ignoring bot: $publicKey"); return; } } @@ -80,7 +80,7 @@ class Relays { } if( gBots.any( (bot) => bot == publicKeys[i] )) { - print("In getMultiUserEvents: ignoring a bot"); + //print("In getMultiUserEvents: ignoring a bot"); continue; } @@ -100,7 +100,7 @@ class Relays { fws = relays[relay]; } else { - print('\nconnecting to $relay'); + print('connecting to $relay'); try { fws = IOWebSocketChannel.connect(relay); @@ -127,7 +127,7 @@ class Relays { } } - //print('sending request: $request to $relay\n'); + print('sending request: $request to $relay\n'); fws?.sink.add(request); } @@ -148,6 +148,9 @@ List getContactFeed(List contacts, events, numEventsToGet) { } else { mContacts[contacts[i].relay] = [contacts[i].id]; } + if( contacts[i].id == "32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245") { + print("In getContactFeed: sending request for jb55 32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"); + } contactList.add(contacts[i].id); }