mirror of
https://github.com/vishalxl/nostr_console.git
synced 2025-04-17 08:11:50 +02:00
added another treeSelector so that only events with likes etc are printed. minor refactoring.
This commit is contained in:
parent
38ae89a3b2
commit
a4e687d806
@ -152,8 +152,12 @@ Future<void> main(List<String> arguments) async {
|
||||
if( gEventsFilename != "") {
|
||||
print("\n");
|
||||
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);
|
||||
|
||||
// count events
|
||||
eventsFromFile.forEach((element) { element.eventData.kind == 1? numFileEvents++: numFileEvents;});
|
||||
print("read $numFileEvents posts from file $gEventsFilename");
|
||||
}
|
||||
|
@ -415,8 +415,8 @@ Future<void> 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) {
|
||||
|
@ -27,7 +27,7 @@ class UserNameInfo {
|
||||
Map<String, UserNameInfo> gKindONames = {};
|
||||
|
||||
// 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 = {};
|
||||
|
||||
// global contact list of each user, including of the logged in user.
|
||||
|
@ -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=<relay wss url>"); 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=<relay wss url>"); 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<Event> eventsFromFile) {
|
||||
eventsFromFile.forEach((element) {relays.uniqueIdsRecieved.add(element.eventData.id);});
|
||||
relays.rEvents = eventsFromFile;
|
||||
}
|
||||
|
||||
|
@ -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<String> gListRelayUrls = [ //defaultServerUrl,
|
||||
// nostrRelayUnther,
|
||||
// "wss://nostr-verified.wellorder.net",
|
||||
List<String> 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
|
||||
|
@ -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<String> insertEvents(Set<Event> newEventsSetToProcess) {
|
||||
if( gDebug > 0) log.info("In insertEvetnts: called for ${newEventsSetToProcess.length} events");
|
||||
|
||||
Set<String> 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: " <had more than 10 elements"} ");
|
||||
return newEventIdsSet;
|
||||
}
|
||||
|
||||
@ -379,13 +379,15 @@ class Tree {
|
||||
|
||||
int numPrinted = 0;
|
||||
|
||||
// for the top most tree, create a smaller list which only has recent trees
|
||||
List<Tree> 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<List<String>>? 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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user