added --relay option

This commit is contained in:
vishalxl 2022-08-12 03:12:33 +05:30
parent 3a9fa74972
commit 56b44cb82a
3 changed files with 38 additions and 30 deletions

View File

@ -10,11 +10,11 @@ var userPublickey = "3235036bd0957dfb27ccda02d452d7c763be40c91a1ac082ba6983b2
//var userPublickey = "ed1d0e1f743a7d19aa2dfb0162df73bacdbc699f67cc55bb91a98c35f7deac69"; // melvin //var userPublickey = "ed1d0e1f743a7d19aa2dfb0162df73bacdbc699f67cc55bb91a98c35f7deac69"; // melvin
//var userPublickey = "52b4a076bcbbbdc3a1aefa3735816cf74993b1b8db202b01c883c58be7fad8bd"; // semisol //var userPublickey = "52b4a076bcbbbdc3a1aefa3735816cf74993b1b8db202b01c883c58be7fad8bd"; // semisol
// program arguments // program arguments
const String requestArg = "request"; const String requestArg = "request";
const String userArg = "user"; const String userArg = "user";
const String lastdaysArg = "days"; const String lastdaysArg = "days";
const String relayArg = "relay";
// by default the threads that were started in last two days are shown // by default the threads that were started in last two days are shown
// this can be changed with 'days' command line argument // this can be changed with 'days' command line argument
@ -55,28 +55,35 @@ void printEventsAsTree(events) {
Future<void> main(List<String> arguments) async { Future<void> main(List<String> arguments) async {
List<Event> events = []; List<Event> events = [];
final parser = ArgParser()..addOption(requestArg, abbr: 'r') final parser = ArgParser()..addOption(requestArg, abbr: 'q')
..addOption(userArg, abbr:"u") ..addOption(userArg, abbr:"u")
..addOption(lastdaysArg, abbr:"d"); ..addOption(lastdaysArg, abbr:"d")
..addOption(relayArg, abbr:"r");
ArgResults argResults = parser.parse(arguments); ArgResults argResults = parser.parse(arguments);
if( argResults[relayArg] != null) {
defaultServerUrl = argResults[relayArg];
print("Going to use relay: $defaultServerUrl");
}
if( argResults[requestArg] != null) { 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); sendRequest("wss://nostr-pub.wellorder.net", argResults[requestArg], events);
Future.delayed(const Duration(milliseconds: 6000), () { Future.delayed(const Duration(milliseconds: 6000), () {
printEventsAsTree(events); printEventsAsTree(events);
exit(0); exit(0);
}); });
return; return;
} else { }
if( argResults[userArg] != null) { if( argResults[userArg] != null) {
userPublickey = argResults[userArg]; userPublickey = argResults[userArg];
} }
if( argResults[lastdaysArg] != null) { if( argResults[lastdaysArg] != null) {
numLastDays = int.parse(argResults[lastdaysArg]); numLastDays = int.parse(argResults[lastdaysArg]);
print("Going to show posts for last $numLastDays days"); print("Going to show posts for last $numLastDays days");
} }
}
// the default in case no arguments are given is: // the default in case no arguments are given is:
// get a user's events, then from its type 3 event, gets events of its follows, // get a user's events, then from its type 3 event, gets events of its follows,
@ -91,13 +98,11 @@ Future<void> main(List<String> arguments) async {
Future.delayed(const Duration(milliseconds: numWaitSeconds), () { Future.delayed(const Duration(milliseconds: numWaitSeconds), () {
// count user events // count user events
events.forEach((element) { element.eventData.kind == 1? numUserEvents++: numUserEvents;}); 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) // get user's feed ( from follows by looking at kind 3 event)
List<String> contactList = []; List<String> contactList = [];
int latestContactsTime = 0; int latestContactsTime = 0;
print("processing contact, event of kind 3");
int latestContactIndex = -1; int latestContactIndex = -1;
for( int i = 0; i < events.length; i++) { for( int i = 0; i < events.length; i++) {
var e = events[i]; var e = events[i];
@ -108,8 +113,6 @@ Future<void> main(List<String> arguments) async {
} }
if (latestContactIndex != -1) { if (latestContactIndex != -1) {
events[latestContactIndex].printEvent(0);
print("got latestContactIndex = $latestContactIndex");
contactList = getContactFeed(events[latestContactIndex].eventData.contactList, events, 300); contactList = getContactFeed(events[latestContactIndex].eventData.contactList, events, 300);
print("number of contacts = ${contactList.length}"); print("number of contacts = ${contactList.length}");
} }
@ -120,19 +123,19 @@ Future<void> main(List<String> arguments) async {
// count feed events // count feed events
events.forEach((element) { element.eventData.kind == 1? numFeedEvents++: numFeedEvents;}); events.forEach((element) { element.eventData.kind == 1? numFeedEvents++: numFeedEvents;});
numFeedEvents = numFeedEvents - numUserEvents; 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 // get mentioned ptags, and then get the events for those users
List<String> pTags = getpTags(events); List<String> pTags = getpTags(events);
print("Total number of pTags = ${pTags.length}\n");
getMultiUserEvents(defaultServerUrl, pTags, events, 300); 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), () { Future.delayed(const Duration(milliseconds: numWaitSeconds * 1), () {
// count other events // count other events
events.forEach((element) { element.eventData.kind == 1? numOtherEvents++: numOtherEvents;}); events.forEach((element) { element.eventData.kind == 1? numOtherEvents++: numOtherEvents;});
numOtherEvents = numOtherEvents - numFeedEvents - numUserEvents; numOtherEvents = numOtherEvents - numFeedEvents - numUserEvents;
stdout.write("received $numOtherEvents other events\n");
printEventsAsTree(events); printEventsAsTree(events);

View File

@ -6,6 +6,8 @@ const int screenWidth = 120;
const bool enableVerticalLines = false; const bool enableVerticalLines = false;
const int spacesPerDepth = 8; const int spacesPerDepth = 8;
int keyLenPrinted = 6; int keyLenPrinted = 6;
//String defaultServerUrl = 'wss://relay.damus.io';
String defaultServerUrl = 'wss://nostr-relay.untethr.me'; String defaultServerUrl = 'wss://nostr-relay.untethr.me';
Map<String, String> gKindONames = {}; // global names from kind 0 events Map<String, String> gKindONames = {}; // global names from kind 0 events
@ -14,13 +16,14 @@ List<String> gBots = [ "3b57518d02e6acfd5eb7198530b2e351e5a52278fb2499d14b66db2
"887645fef0ce0c3c1218d2f5d8e6132a19304cdc57cd20281d082f38cfea0072" // bestofhn "887645fef0ce0c3c1218d2f5d8e6132a19304cdc57cd20281d082f38cfea0072" // bestofhn
]; ];
const int gDebug = 0;
// If given event is kind 0 event, then populates gKindONames with that info // If given event is kind 0 event, then populates gKindONames with that info
void processKind0Event(Event e) { void processKind0Event(Event e) {
if( e.eventData.kind != 0) { if( e.eventData.kind != 0) {
return; return;
} }
//print("In getNames: for event content: ${e.eventData.content}");
String content = e.eventData.content; String content = e.eventData.content;
if( content.isEmpty) { if( content.isEmpty) {
return; return;
@ -31,7 +34,7 @@ void processKind0Event(Event e) {
gKindONames[e.eventData.pubkey] = json["name"]??""; gKindONames[e.eventData.pubkey] = json["name"]??"";
} }
} catch(ex) { } 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. // @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. // first create a map. then add all top trees to the final list/ChildTrees. then add children to it.
factory Tree.fromEvents(List<Event> events) { factory Tree.fromEvents(List<Event> 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 // create a map from list of events, key is eventId and value is event itself
Map<String, Tree> mAllEvents = {}; Map<String, Tree> mAllEvents = {};
events.forEach((element) { mAllEvents[element.eventData.id] = Tree(element, []); }); 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] return Tree( events[0], topLevelTrees); // TODO remove events[0]
} // end fromEvents() } // end fromEvents()
@ -357,7 +360,6 @@ List<String> getpTags(List<Event> events) {
Set tempPtags = {}; Set tempPtags = {};
pTags.retainWhere((x) => tempPtags.add(x)); pTags.retainWhere((x) => tempPtags.add(x));
print("In getPtags. returning number of ptags = ${pTags.length}");
return pTags; return pTags;
} }

View File

@ -49,7 +49,7 @@ class Relays {
for(int i = 0; i < gBots.length; i++) { for(int i = 0; i < gBots.length; i++) {
if( publicKey == gBots[i]) { if( publicKey == gBots[i]) {
print("In gerUserEvents: ignoring bot: $publicKey"); //print("In gerUserEvents: ignoring bot: $publicKey");
return; return;
} }
} }
@ -80,7 +80,7 @@ class Relays {
} }
if( gBots.any( (bot) => bot == publicKeys[i] )) { if( gBots.any( (bot) => bot == publicKeys[i] )) {
print("In getMultiUserEvents: ignoring a bot"); //print("In getMultiUserEvents: ignoring a bot");
continue; continue;
} }
@ -100,7 +100,7 @@ class Relays {
fws = relays[relay]; fws = relays[relay];
} }
else { else {
print('\nconnecting to $relay'); print('connecting to $relay');
try { try {
fws = IOWebSocketChannel.connect(relay); 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); fws?.sink.add(request);
} }
@ -148,6 +148,9 @@ List<String> getContactFeed(List<Contact> contacts, events, numEventsToGet) {
} else { } else {
mContacts[contacts[i].relay] = [contacts[i].id]; 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); contactList.add(contacts[i].id);
} }