added another treeSelector so that only events with likes etc are printed. minor refactoring.

This commit is contained in:
Vishal
2022-08-29 19:08:00 +05:30
parent 38ae89a3b2
commit a4e687d806
6 changed files with 83 additions and 67 deletions

View File

@@ -152,8 +152,12 @@ Future<void> main(List<String> arguments) async {
if( gEventsFilename != "") { if( gEventsFilename != "") {
print("\n"); print("\n");
stdout.write('Reading events from ${whetherDefault}file.......'); stdout.write('Reading events from ${whetherDefault}file.......');
Set<Event> eventsFromFile = readEventsFromFile(gEventsFilename);
// read file events and give the events to relays from where they're picked up later
Set<Event> eventsFromFile = await readEventsFromFile(gEventsFilename);
setRelaysIntialEvents(eventsFromFile); setRelaysIntialEvents(eventsFromFile);
// count events
eventsFromFile.forEach((element) { element.eventData.kind == 1? numFileEvents++: numFileEvents;}); eventsFromFile.forEach((element) { element.eventData.kind == 1? numFileEvents++: numFileEvents;});
print("read $numFileEvents posts from file $gEventsFilename"); print("read $numFileEvents posts from file $gEventsFilename");
} }

View File

@@ -415,8 +415,8 @@ Future<void> mainMenuUi(Tree node, var contactList) async {
//gDebug = 0; //gDebug = 0;
// at the very beginning, show the tree as it is, and then show the options menu // at the very beginning, show the tree as it is, and then show the options menu
//bool repliesAndLikes (Tree t) => t.repliesAndLikes(userPublicKey); bool hasRepliesAndLikes (Tree t) => t.hasRepliesAndLikes(userPublicKey);
node.printTree(0, DateTime.now().subtract(Duration(days:gNumLastDays)), selectAll); node.printTree(0, DateTime.now().subtract(Duration(days:gNumLastDays)), hasRepliesAndLikes);
bool userContinue = true; bool userContinue = true;
while(userContinue) { while(userContinue) {

View File

@@ -27,7 +27,7 @@ class UserNameInfo {
Map<String, UserNameInfo> gKindONames = {}; Map<String, UserNameInfo> gKindONames = {};
// global reactions entry. Map of form <if of event reacted to, List of Reactors> // global reactions entry. Map of form <if of event reacted to, List of Reactors>
// 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<List<String>> > gReactions = {}; Map< String, List<List<String>> > gReactions = {};
// global contact list of each user, including of the logged in user. // global contact list of each user, including of the logged in user.

View File

@@ -135,37 +135,45 @@ class Relays {
relays[relay] = newRelay; relays[relay] = newRelay;
fws = fws2; fws = fws2;
fws2.stream.listen( fws2.stream.listen(
(d) { (d) {
Event e; Event e;
try { try {
dynamic json = jsonDecode(d); dynamic json = jsonDecode(d);
if( json.length < 3) { if( json.length < 3) {
return; return;
} }
String id = json[2]['id'] as String; newRelay.numReceived++;
if( uniqueIdsRecieved.contains(id)) {
return;
}
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); 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");
} on FormatException { if( gDebug > 0) log.info("In listener: Event size before adding: ${rEvents.length}");
print( 'exception in fromJson for event'); if( rEvents.add(e) ) {
return; uniqueIdsRecieved.add(id);
} catch(err) { String receivedSubscription = json[1];
print('exception generic $err for relay $relay'); if( gDebug > 3) e.eventData.printEventData(0);
return; 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");
onError: (err) { print("\n${gWarningColor}Warning: In SendRequest creating connection onError. Kindly check your internet connection or change the relay by command line --relay=<relay wss url>"); print(colorEndMarker); }, if( gDebug > 1) print("\n");
onDone: () { if( gDebug != 0) print('Info: In onDone'); } } 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=<relay wss url>"); print(colorEndMarker); },
onDone: () { if( gDebug != 0) print('Info: In onDone'); }
); );
} on WebSocketException { } on WebSocketException {
print('WebSocketException exception for relay $relay'); print('WebSocketException exception for relay $relay');
@@ -297,6 +305,7 @@ void clearEvents() {
} }
void setRelaysIntialEvents(Set<Event> eventsFromFile) { void setRelaysIntialEvents(Set<Event> eventsFromFile) {
eventsFromFile.forEach((element) {relays.uniqueIdsRecieved.add(element.eventData.id);});
relays.rEvents = eventsFromFile; relays.rEvents = eventsFromFile;
} }

View File

@@ -5,7 +5,7 @@ final log = Logger('ExampleLogger');
// for debugging // for debugging
String gCheckEventId = "a4479de655094679cdfb10f347521aa58f24717cdc5ddba89fb346453a8a99ed"; String gCheckEventId = "a4479de655094679cdfb10f347521aa58f24717cdc5ddba89fb346453a8a99ed";
const int numWaitSeconds = 4000; const int numWaitSeconds = 3000;
const String gDefaultEventsFilename = "all_nostr_events.txt"; const String gDefaultEventsFilename = "all_nostr_events.txt";
String gEventsFilename = ""; // is set in arguments, and if set, then file is read from and written to 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'; //String defaultServerUrl = 'wss://relay.damus.io';
const String nostrRelayUnther = 'wss://nostr-relay.untethr.me'; const String nostrRelayUnther = 'wss://nostr-relay.untethr.me';
const String relayNostrInfo = 'wss://relay.nostr.info'; const String relayNostrInfo = 'wss://relay.nostr.info';
String defaultServerUrl = relayNostrInfo; String defaultServerUrl = "wss://relay.damus.io";
List<String> gListRelayUrls = [ //defaultServerUrl, List<String> gListRelayUrls = [ defaultServerUrl,
// nostrRelayUnther, "wss://nostr-verified.wellorder.net",
// "wss://nostr-verified.wellorder.net",
"wss://nostr-relay.wlvs.space", "wss://nostr-relay.wlvs.space",
"wss://nostr-pub.wellorder.net", "wss://nostr-pub.wellorder.net"
"wss://relay.damus.io" //"wss://relay.damus.io"
]; ];
// name of executable // name of executable

View File

@@ -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 * @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<String> insertEvents(Set<Event> newEventsSetToProcess) { Set<String> insertEvents(Set<Event> newEventsSetToProcess) {
if( gDebug > 0) log.info("In insertEvetnts: called for ${newEventsSetToProcess.length} events");
Set<String> newEventIdsSet = {}; Set<String> newEventIdsSet = {};
@@ -214,7 +215,7 @@ class Tree {
// expand mentions ( and translate if flag is set) // expand mentions ( and translate if flag is set)
newEvent.eventData.translateAndExpandMentions(); 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, {}); 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: " <had more than 10 elements"} ");
return newEventIdsSet; return newEventIdsSet;
} }
@@ -379,13 +379,15 @@ class Tree {
int numPrinted = 0; int numPrinted = 0;
// for the top most tree, create a smaller list which only has recent trees
List<Tree> latestTrees = [];
if( !whetherTopMost) { if( whetherTopMost) {
e.printEvent(depth);
numPrinted++;
} else {
depth = depth - 1; 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 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; bool leftShifted = false;
@@ -397,9 +399,9 @@ class Tree {
stdout.write("|\n"); stdout.write("|\n");
} else { } else {
// continue if this children isn't going to get printed anyway; selector is only called for top most tree // 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; continue;
} }
int newestChildTime = children[i].getMostRecentTime(0); int newestChildTime = children[i].getMostRecentTime(0);
DateTime dTime = DateTime.fromMillisecondsSinceEpoch(newestChildTime *1000); 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 // 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 hasRepliesAndLikes(String pk) {
bool hasReacted = false; //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<List<String>>? reactions = gReactions[e.eventData.id]; List<List<String>>? reactions = gReactions[e.eventData.id];
if( reactions != null) { if( reactions != null) {
for( int i = 0; i < reactions.length; i++) { if( reactions.length > 0) {
if( reactions[i][0] == pubkey) { //print("has reactions");
e.eventData.newLikes.add(pubkey); reactions.forEach((reaction) { e.eventData.newLikes.add(reaction[0]);});
hasReacted = true; hasReaction = true;
break;
}
} }
} }
} }
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++ ) { for( int i = 0; i < children.length; i++ ) {
if( children[i].hasUserPostAndLike(pubkey)) { if( children[i].hasRepliesAndLikes(pk)) {
childMatches = true; childMatches = true;
} }
} }
if( e.eventData.pubkey == pubkey) {
e.eventData.isNotification = true; if( hasReaction || childMatches) {
return true; //print("returning true");
}
if( hasReacted || childMatches) {
return true; return true;
} }
return false; 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 // 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) { bool hasUserPostAndLike(String pubkey) {