diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index acc61a3..9f52715 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -152,8 +152,12 @@ Future main(List arguments) async { if( gEventsFilename != "") { print("\n"); stdout.write('Reading events from ${whetherDefault}file.......'); - Set eventsFromFile = readEventsFromFile(gEventsFilename); + + // read file events and give the events to relays from where they're picked up later + Set eventsFromFile = await readEventsFromFile(gEventsFilename); setRelaysIntialEvents(eventsFromFile); + + // count events eventsFromFile.forEach((element) { element.eventData.kind == 1? numFileEvents++: numFileEvents;}); print("read $numFileEvents posts from file $gEventsFilename"); } diff --git a/lib/console_ui.dart b/lib/console_ui.dart index 315a46c..52c4146 100644 --- a/lib/console_ui.dart +++ b/lib/console_ui.dart @@ -415,8 +415,8 @@ Future mainMenuUi(Tree node, var contactList) async { //gDebug = 0; // at the very beginning, show the tree as it is, and then show the options menu - //bool repliesAndLikes (Tree t) => t.repliesAndLikes(userPublicKey); - node.printTree(0, DateTime.now().subtract(Duration(days:gNumLastDays)), selectAll); + bool hasRepliesAndLikes (Tree t) => t.hasRepliesAndLikes(userPublicKey); + node.printTree(0, DateTime.now().subtract(Duration(days:gNumLastDays)), hasRepliesAndLikes); bool userContinue = true; while(userContinue) { diff --git a/lib/event_ds.dart b/lib/event_ds.dart index 0200087..7fe15ef 100644 --- a/lib/event_ds.dart +++ b/lib/event_ds.dart @@ -27,7 +27,7 @@ class UserNameInfo { Map gKindONames = {}; // global reactions entry. Map of form -// reach Reactor is a list of 2-elements ( first is public id of reactor, second is comment) +// reach Reactor is a list of 2-elements ( first is public id of reactor event, second is comment) Map< String, List> > gReactions = {}; // global contact list of each user, including of the logged in user. diff --git a/lib/relays.dart b/lib/relays.dart index f88667d..42f3225 100644 --- a/lib/relays.dart +++ b/lib/relays.dart @@ -135,37 +135,45 @@ class Relays { relays[relay] = newRelay; fws = fws2; fws2.stream.listen( - (d) { - Event e; - try { - dynamic json = jsonDecode(d); - if( json.length < 3) { - return; - } - String id = json[2]['id'] as String; - if( uniqueIdsRecieved.contains(id)) { - return; - } + (d) { + Event e; + try { + dynamic json = jsonDecode(d); + if( json.length < 3) { + return; + } + newRelay.numReceived++; - uniqueIdsRecieved.add(id); + String id = json[2]['id'] as String; + if( uniqueIdsRecieved.contains(id)) { // rEvents is often cleared, but uniqueIdsRecieved contains everything received til now + //if( gDebug > 0) log.info("In listener: event already present."); + return; + } - e = Event.fromJson(d, relay); - - rEvents.add(e); - newRelay.numReceived++; - String receivedSubscription = json[1]; - if( gDebug > 0) log.info("In relay listener: after adding element rEvents Size = ${rEvents.length} numReceived = ${newRelay.numReceived} for relay $relay for subscription $receivedSubscription"); + e = Event.fromJson(d, relay); - } on FormatException { - print( 'exception in fromJson for event'); - return; - } catch(err) { - print('exception generic $err for relay $relay'); - return; - } - }, - onError: (err) { print("\n${gWarningColor}Warning: In SendRequest creating connection onError. Kindly check your internet connection or change the relay by command line --relay="); print(colorEndMarker); }, - onDone: () { if( gDebug != 0) print('Info: In onDone'); } + if( gDebug > 0) log.info("In listener: Event size before adding: ${rEvents.length}"); + if( rEvents.add(e) ) { + uniqueIdsRecieved.add(id); + String receivedSubscription = json[1]; + if( gDebug > 3) e.eventData.printEventData(0); + if( gDebug > 2) print(""); + + if( gDebug > 1) log.info("In relay listener for relay url $relay: after adding element, rEvents Size = ${rEvents.length} numReceived = ${newRelay.numReceived} for subscription $receivedSubscription"); + if( gDebug > 1) print("\n"); + } else { + //if( gDebug > 0) log.info("In listener: event was already in rEvents"); + } + } on FormatException { + print( 'exception in fromJson for event'); + return; + } catch(err) { + print('exception generic $err for relay $relay'); + return; + } + }, + onError: (err) { print("\n${gWarningColor}Warning: In SendRequest creating connection onError. Kindly check your internet connection or change the relay by command line --relay="); print(colorEndMarker); }, + onDone: () { if( gDebug != 0) print('Info: In onDone'); } ); } on WebSocketException { print('WebSocketException exception for relay $relay'); @@ -297,6 +305,7 @@ void clearEvents() { } void setRelaysIntialEvents(Set eventsFromFile) { + eventsFromFile.forEach((element) {relays.uniqueIdsRecieved.add(element.eventData.id);}); relays.rEvents = eventsFromFile; } diff --git a/lib/settings.dart b/lib/settings.dart index 8f08026..16541ba 100644 --- a/lib/settings.dart +++ b/lib/settings.dart @@ -5,7 +5,7 @@ final log = Logger('ExampleLogger'); // for debugging String gCheckEventId = "a4479de655094679cdfb10f347521aa58f24717cdc5ddba89fb346453a8a99ed"; -const int numWaitSeconds = 4000; +const int numWaitSeconds = 3000; const String gDefaultEventsFilename = "all_nostr_events.txt"; String gEventsFilename = ""; // is set in arguments, and if set, then file is read from and written to @@ -28,14 +28,13 @@ int numFileEvents = 0, numUserEvents = 0, numFeedEvents = 0, numOtherEvents = 0; //String defaultServerUrl = 'wss://relay.damus.io'; const String nostrRelayUnther = 'wss://nostr-relay.untethr.me'; const String relayNostrInfo = 'wss://relay.nostr.info'; -String defaultServerUrl = relayNostrInfo; +String defaultServerUrl = "wss://relay.damus.io"; -List gListRelayUrls = [ //defaultServerUrl, - // nostrRelayUnther, - // "wss://nostr-verified.wellorder.net", +List gListRelayUrls = [ defaultServerUrl, + "wss://nostr-verified.wellorder.net", "wss://nostr-relay.wlvs.space", - "wss://nostr-pub.wellorder.net", - "wss://relay.damus.io" + "wss://nostr-pub.wellorder.net" + //"wss://relay.damus.io" ]; // name of executable diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart index cb159b6..82e2f2b 100644 --- a/lib/tree_ds.dart +++ b/lib/tree_ds.dart @@ -178,6 +178,7 @@ class Tree { * @insertEvents inserts the given new events into the tree, and returns the id the ones actually inserted so that they can be printed as notifications */ Set insertEvents(Set newEventsSetToProcess) { + if( gDebug > 0) log.info("In insertEvetnts: called for ${newEventsSetToProcess.length} events"); Set newEventIdsSet = {}; @@ -214,7 +215,7 @@ class Tree { // expand mentions ( and translate if flag is set) newEvent.eventData.translateAndExpandMentions(); - if( gDebug > 0) print("In insertEvents: adding event to main children map"); + //if( gDebug > 0) print("In insertEvents: adding event to main children map"); allChildEventsMap[newEvent.eventData.id] = Tree(newEvent, [], {}, [], false, {}); @@ -276,8 +277,7 @@ class Tree { } }); - if(gDebug > 0) print("In end of insertEvents: Returning ${newEventIdsSet.length} new notification-type event: $newEventIdsSet "); - + if(gDebug > 0) print("In end of insertEvents: Returning ${newEventIdsSet.length} new notification-type events, which are ${newEventIdsSet.length < 10 ? newEventIdsSet: " latestTrees = []; - if( !whetherTopMost) { - e.printEvent(depth); - numPrinted++; - } else { + if( whetherTopMost) { depth = depth - 1; children.sort(sortTreeNewestReply); // sorting done only for top most threads. Lower threads aren't sorted so save cpu etc TODO improve top sorting + } else { + e.printEvent(depth); + numPrinted++; } bool leftShifted = false; @@ -397,9 +399,9 @@ class Tree { stdout.write("|\n"); } else { // continue if this children isn't going to get printed anyway; selector is only called for top most tree - if( !treeSelector(children[i])) { + if( treeSelector(children[i]) == false) { continue; - } + } int newestChildTime = children[i].getMostRecentTime(0); DateTime dTime = DateTime.fromMillisecondsSinceEpoch(newestChildTime *1000); @@ -714,41 +716,43 @@ class Tree { } } -/* - // TODO // returns true if the treee or its children has a post or like by user; and notification flags are set for such events - bool repliesAndLikes(String pubkey) { - bool hasReacted = false; + bool hasRepliesAndLikes(String pk) { + //print("----- pk = $pk"); + bool hasReaction = false; + bool childMatches = false; - if( gReactions.containsKey(e.eventData.id)) { + if( e.eventData.pubkey == pk && gReactions.containsKey(e.eventData.id)) { List>? reactions = gReactions[e.eventData.id]; if( reactions != null) { - for( int i = 0; i < reactions.length; i++) { - if( reactions[i][0] == pubkey) { - e.eventData.newLikes.add(pubkey); - hasReacted = true; - break; - } + if( reactions.length > 0) { + //print("has reactions"); + reactions.forEach((reaction) { e.eventData.newLikes.add(reaction[0]);}); + hasReaction = true; } } } - bool childMatches = false; + if( e.eventData.pubkey == pk && children.length > 0) { + for( int i = 0; i < children.length; i++ ) { + // if child is someone else then set notifications and flag + children.forEach((c) { c.e.eventData.isNotification = ((c.e.eventData.pubkey != pk)? true: false) ; childMatches = true; }); + } + } + for( int i = 0; i < children.length; i++ ) { - if( children[i].hasUserPostAndLike(pubkey)) { + if( children[i].hasRepliesAndLikes(pk)) { childMatches = true; } } - if( e.eventData.pubkey == pubkey) { - e.eventData.isNotification = true; - return true; - } - if( hasReacted || childMatches) { + + if( hasReaction || childMatches) { + //print("returning true"); return true; } return false; } -*/ + // returns true if the treee or its children has a post or like by user; and notification flags are set for such events bool hasUserPostAndLike(String pubkey) {