mirror of
https://github.com/vishalxl/nostr_console.git
synced 2025-04-02 17:08:12 +02:00
Imporoved logic so that main tree is created with file events, and net events are later added to it.
Makes it easier to track so that later only net events are printed.
This commit is contained in:
parent
00d3f3415c
commit
2b7c3e3124
@ -165,7 +165,6 @@ Future<void> main(List<String> arguments) async {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if( argResults[disableFileArg]) {
|
||||
gEventsFilename = "";
|
||||
print("Not going to use any file to read/write events.");
|
||||
@ -196,6 +195,12 @@ Future<void> main(List<String> arguments) async {
|
||||
print("read $numFileEvents posts from file $gEventsFilename");
|
||||
}
|
||||
|
||||
// get all events in Tree form
|
||||
Tree node = getTree(getRecievedEvents());
|
||||
|
||||
// call the mein UI function
|
||||
clearEvents();
|
||||
|
||||
// process request string. If this is blank then the application only reads from file and does not connect to internet.
|
||||
if( argResults[requestArg] != null) {
|
||||
int numWaitSeconds = gDefaultNumWaitSeconds;
|
||||
@ -212,17 +217,75 @@ Future<void> main(List<String> arguments) async {
|
||||
Set<Event> receivedEvents = getRecievedEvents();
|
||||
stdout.write("received ${receivedEvents.length - numFileEvents} events\n");
|
||||
|
||||
// create tree: will process reactions, remove bots, and then create main tree
|
||||
Tree node = getTree(getRecievedEvents());
|
||||
node.insertEvents(getRecievedEvents());
|
||||
clearEvents();
|
||||
|
||||
//clearEvents(); // cause we have consumed them above
|
||||
if( gDebug > 0) stdout.write("Total events of kind 1 in created tree: ${node.count()} events\n");
|
||||
clearEvents();
|
||||
|
||||
mainMenuUi(node);
|
||||
if( gDebug > 0) stdout.write("Total events of kind 1 in created tree: ${node.count()} events\n");
|
||||
mainMenuUi(node);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
getUserEvents(gListRelayUrls, userPublicKey, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor));
|
||||
|
||||
// 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,
|
||||
// then get the events of user-id's mentioned in p-tags of received events
|
||||
// then display them all
|
||||
stdout.write('Waiting for user posts to come in.....');
|
||||
Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds), () {
|
||||
// count user events
|
||||
getRecievedEvents().forEach((element) { element.eventData.kind == 1? numUserEvents++: numUserEvents;});
|
||||
stdout.write("...received $numUserEvents new posts made by the user\n");
|
||||
if( gDebug > 0) log.info("Received user events.");
|
||||
|
||||
getRecievedEvents().forEach((e) => processKind3Event(e)); // first process the kind 3 event
|
||||
// get the latest kind 3 event for the user, which lists his 'follows' list
|
||||
Event? contactEvent = getContactEvent(userPublicKey);
|
||||
|
||||
// if contact list was found, get user's feed, and keep the contact list for later use
|
||||
List<String> contactList = [];
|
||||
if (contactEvent != null ) {
|
||||
if(gDebug > 0) print("In main: found contact list: \n ${contactEvent.originalJson}");
|
||||
contactList = getContactFeed(gListRelayUrls, contactEvent.eventData.contactList, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor));
|
||||
|
||||
if( !gContactLists.containsKey(userPublicKey)) {
|
||||
gContactLists[userPublicKey] = contactEvent.eventData.contactList;
|
||||
}
|
||||
} else {
|
||||
if( gDebug > 0) print( "could not find contact list");
|
||||
}
|
||||
|
||||
stdout.write('Waiting for feed to come in..............');
|
||||
Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds * 1), () {
|
||||
|
||||
// count feed events
|
||||
getRecievedEvents().forEach((element) { element.eventData.kind == 1? numFeedEvents++: numFeedEvents;});
|
||||
numFeedEvents = numFeedEvents - numUserEvents;
|
||||
stdout.write("received $numFeedEvents new posts from the follows\n");
|
||||
if( gDebug > 0) log.info("Received feed.");
|
||||
|
||||
// get mentioned ptags, and then get the events for those users
|
||||
List<String> pTags = getpTags(getRecievedEvents(), gMaxPtagsToGet);
|
||||
getMultiUserEvents(gListRelayUrls, pTags, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor));
|
||||
|
||||
stdout.write('Waiting for rest of posts to come in.....');
|
||||
Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds * 2), () {
|
||||
|
||||
// count other events
|
||||
getRecievedEvents().forEach((element) { element.eventData.kind == 1? numOtherEvents++: numOtherEvents;});
|
||||
numOtherEvents = numOtherEvents - numFeedEvents - numUserEvents;
|
||||
stdout.write("received $numOtherEvents new posts by others\n");
|
||||
if( gDebug > 0) log.info("Received ptag events events.");
|
||||
|
||||
node.insertEvents(getRecievedEvents());
|
||||
clearEvents();
|
||||
mainMenuUi(node);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
} on FormatException catch (e) {
|
||||
print(e.message);
|
||||
return;
|
||||
@ -231,66 +294,4 @@ Future<void> main(List<String> arguments) async {
|
||||
return;
|
||||
}
|
||||
|
||||
getUserEvents(gListRelayUrls, userPublicKey, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor));
|
||||
|
||||
// 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,
|
||||
// then get the events of user-id's mentioned in p-tags of received events
|
||||
// then display them all
|
||||
stdout.write('Waiting for user posts to come in.....');
|
||||
Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds), () {
|
||||
// count user events
|
||||
getRecievedEvents().forEach((element) { element.eventData.kind == 1? numUserEvents++: numUserEvents;});
|
||||
numUserEvents -= numFileEvents;
|
||||
stdout.write("...received $numUserEvents new posts made by the user\n");
|
||||
if( gDebug > 0) log.info("Received user events.");
|
||||
|
||||
getRecievedEvents().forEach((e) => processKind3Event(e)); // first process the kind 3 event
|
||||
// get the latest kind 3 event for the user, which lists his 'follows' list
|
||||
Event? contactEvent = getContactEvent(userPublicKey);
|
||||
|
||||
// if contact list was found, get user's feed, and keep the contact list for later use
|
||||
List<String> contactList = [];
|
||||
if (contactEvent != null ) {
|
||||
if(gDebug > 0) print("In main: found contact list: \n ${contactEvent.originalJson}");
|
||||
contactList = getContactFeed(gListRelayUrls, contactEvent.eventData.contactList, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor));
|
||||
|
||||
if( !gContactLists.containsKey(userPublicKey)) {
|
||||
gContactLists[userPublicKey] = contactEvent.eventData.contactList;
|
||||
}
|
||||
} else {
|
||||
if( gDebug > 0) print( "could not find contact list");
|
||||
}
|
||||
|
||||
stdout.write('Waiting for feed to come in..............');
|
||||
Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds * 1), () {
|
||||
|
||||
// count feed events
|
||||
getRecievedEvents().forEach((element) { element.eventData.kind == 1? numFeedEvents++: numFeedEvents;});
|
||||
numFeedEvents = numFeedEvents - numUserEvents - numFileEvents;
|
||||
stdout.write("received $numFeedEvents new posts from the follows\n");
|
||||
if( gDebug > 0) log.info("Received feed.");
|
||||
|
||||
// get mentioned ptags, and then get the events for those users
|
||||
List<String> pTags = getpTags(getRecievedEvents(), gMaxPtagsToGet);
|
||||
getMultiUserEvents(gListRelayUrls, pTags, gLimitPerSubscription, getSecondsDaysAgo(gDaysToGetEventsFor));
|
||||
|
||||
stdout.write('Waiting for rest of posts to come in.....');
|
||||
Future.delayed(const Duration(milliseconds: gDefaultNumWaitSeconds * 2), () {
|
||||
|
||||
// count other events
|
||||
getRecievedEvents().forEach((element) { element.eventData.kind == 1? numOtherEvents++: numOtherEvents;});
|
||||
numOtherEvents = numOtherEvents - numFeedEvents - numUserEvents - numFileEvents;
|
||||
stdout.write("received $numOtherEvents new posts by others\n");
|
||||
if( gDebug > 0) log.info("Received ptag events events.");
|
||||
|
||||
// get all events in Tree form
|
||||
Tree node = getTree(getRecievedEvents());
|
||||
|
||||
// call the mein UI function
|
||||
clearEvents();
|
||||
mainMenuUi(node);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
@ -416,7 +416,7 @@ Future<void> channelMenuUI(Tree node) async {
|
||||
}
|
||||
|
||||
bool showChannelOption = true;
|
||||
stdout.write("\nType unique channel id or name, or their 1st few letters; or type 'x' to go back to exit channel: ");
|
||||
stdout.write("\nType channel id or name, or their 1st few letters; or type 'x' to go to menu: ");
|
||||
String? $tempUserInput = stdin.readLineSync();
|
||||
String channelId = $tempUserInput??"";
|
||||
|
||||
@ -432,7 +432,7 @@ Future<void> channelMenuUI(Tree node) async {
|
||||
break;
|
||||
}
|
||||
|
||||
stdout.write("\nType message to send to this room; or type 'x' to exit the channel, or just press <enter> to refresh: ");
|
||||
stdout.write("\nType message; or type 'x' to exit, or press <enter> to refresh: ");
|
||||
$tempUserInput = stdin.readLineSync(encoding: utf8);
|
||||
String messageToSend = $tempUserInput??"";
|
||||
|
||||
@ -546,9 +546,7 @@ Future<void> mainMenuUi(Tree node) async {
|
||||
default:
|
||||
userContinue = false;
|
||||
String authorName = getAuthorName(userPublicKey);
|
||||
print("\nFinished Nostr session for user with publick key: $userPublicKey ($authorName). Exiting");
|
||||
//contactList.forEach((x) => stdout.write("${getAuthorName(x)}, "));
|
||||
stdout.write("\n");
|
||||
print("\nFinished Nostr session for user with name and public key: ${authorName} ($userPublicKey).");
|
||||
if( gEventsFilename != "") {
|
||||
await node.writeEventsToFile(gEventsFilename);
|
||||
}
|
||||
|
@ -303,7 +303,7 @@ class Tree {
|
||||
stdout.write("${getNumDashes(strToWrite.length -1 )}\n$strToWrite");
|
||||
stdout.write("Total posts : ${count()}\n");
|
||||
stdout.write("Signed in as : $userName\n");
|
||||
stdout.write("\nHere are the threads with new replies or new likes: \n\n");
|
||||
stdout.write("\nHere are the threads with new replies or new likes: \n");
|
||||
|
||||
List<Tree> topNotificationTree = []; // collect all top tress to display in this list. only unique tress will be displayed
|
||||
newEventIdsSet.forEach((eventID) {
|
||||
@ -548,15 +548,14 @@ class Tree {
|
||||
try {
|
||||
final File file = File(filename);
|
||||
|
||||
// empty the file
|
||||
await file.writeAsString("", mode: FileMode.writeOnlyAppend).then( (file) => file);
|
||||
//await file.writeAsString("", mode: FileMode.append).then( (file) => file);
|
||||
int eventCounter = 0;
|
||||
String nLinesStr = "";
|
||||
int countPosts = 0;
|
||||
|
||||
const int numLinesTogether = 100; // number of lines to write in one write call
|
||||
int linesWritten = 0;
|
||||
print("eventsNotReadFromFile = ${eventsNotReadFromFile.length}");
|
||||
if(gDebug > 0) log.info("eventsNotReadFromFile = ${eventsNotReadFromFile.length}. start writing.");
|
||||
for( var k in eventsNotReadFromFile) {
|
||||
Tree? t = allChildEventsMap[k];
|
||||
if( t != null) {
|
||||
@ -587,10 +586,11 @@ class Tree {
|
||||
nLinesStr = "";
|
||||
}
|
||||
|
||||
print("\n\nWrote total $eventCounter events to file \"$gEventsFilename\" of which ${countPosts} are posts.") ; // TODO remove extra 1
|
||||
if(gDebug > 0) log.info("eventsNotReadFromFile = ${eventsNotReadFromFile.length}. finished writing eventCounter = ${eventCounter}.");
|
||||
print("Appended $eventCounter new events to file \"$gEventsFilename\" of which ${countPosts} are posts.");
|
||||
} on Exception catch (e) {
|
||||
print("Could not open file $filename.");
|
||||
if( gDebug > 0) print("Could not open file: $e");
|
||||
print("Could not open file $filename.");
|
||||
if( gDebug > 0) print("Could not open file: $e");
|
||||
}
|
||||
|
||||
return;
|
||||
@ -1100,7 +1100,7 @@ void processReactions(Set<Event> events) {
|
||||
*/
|
||||
Tree getTree(Set<Event> events) {
|
||||
if( events.isEmpty) {
|
||||
print("Warning: In printEventsAsTree: events length = 0");
|
||||
if(gDebug > 0) log.info("Warning: In printEventsAsTree: events length = 0");
|
||||
return Tree(Event("","",EventData("non","", 0, 0, "", [], [], [], [[]], {}), [""], "[json]"), [], {}, [], true, {}, {});
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@ description: A multi-platform nostr client built for terminal/console.
|
||||
version: 0.0.7-beta
|
||||
homepage: https://github.com/vishalxl/nostr_console
|
||||
|
||||
# testing pubspec action based on path, test build
|
||||
# testing pubspec action based on path, test build
|
||||
|
||||
environment:
|
||||
sdk: '>=2.17.3 <3.0.0'
|
||||
|
Loading…
x
Reference in New Issue
Block a user