From dd5b80e2abaa28182b4e94362d5e98b15e12583c Mon Sep 17 00:00:00 2001 From: vishalxl <> Date: Wed, 17 Aug 2022 05:51:48 +0530 Subject: [PATCH] printed notifications: threads with new replies are printed from their topmost comment --- bin/nostr_console.dart | 10 ++++--- lib/tree_ds.dart | 63 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/bin/nostr_console.dart b/bin/nostr_console.dart index 0d73ac0..072ab75 100644 --- a/bin/nostr_console.dart +++ b/bin/nostr_console.dart @@ -94,8 +94,9 @@ Future terminalMenuUi(Tree node, var contactList) async { // relays to recieve and handle new events const int waitMilliSeconds = 400; Future.delayed(const Duration(milliseconds: waitMilliSeconds), () { - print("\n\n\n\n\n\n---------------------------------------\nNotifications: Number of new events = ${getRecievedEvents().length}"); + node.insertEvents(getRecievedEvents()); + node.printThreads(getRecievedEvents()); clearEvents(); }); @@ -135,6 +136,7 @@ Future terminalMenuUi(Tree node, var contactList) async { String sig = sign(userPrivateKey, id, "12345612345612345612345612345612"); String toSendMessage = '["EVENT", {"id": "$id","pubkey": "$userPublicKey","created_at": $createdAt,"kind": 1,"tags": [$strTags],"content": "$content","sig": "$sig"}]'; + //print(toSendMessage); relays.sendMessage(toSendMessage, defaultServerUrl); break; @@ -228,17 +230,17 @@ Future main(List arguments) async { if( e.eventData.kind == 3 && latestContactsTime < e.eventData.createdAt) { latestContactIndex = i; latestContactsTime = e.eventData.createdAt; - print("${DateTime.now().millisecondsSinceEpoch ~/ 1000} latestContactsTime = $latestContactsTime"); + //print("${DateTime.now().millisecondsSinceEpoch ~/ 1000} latestContactsTime = $latestContactsTime"); } } if (latestContactIndex != -1) { contactList = getContactFeed(getRecievedEvents()[latestContactIndex].eventData.contactList, 300); - print("number of contacts = ${contactList.length}"); + //print("number of contacts = ${contactList.length}"); } - stdout.write('waiting for feed to come in.....'); + stdout.write('waiting for feed to come in...............'); Future.delayed(const Duration(milliseconds: numWaitSeconds * 1), () { // count feed events diff --git a/lib/tree_ds.dart b/lib/tree_ds.dart index cad0e80..be0c250 100644 --- a/lib/tree_ds.dart +++ b/lib/tree_ds.dart @@ -43,9 +43,12 @@ class Tree { bool insertEvents(List newEvents) { //print("In insertEvents num events: ${newEvents.length}"); List newEventsId = []; + newEvents.forEach((element) { + // don't process if the event is already present in the map + // this condition also excludes any duplicate events sent as newEvents if( allEvents[element.eventData.id] != null) { - return; // don't process if the event is already present in the map + return; } if( element.eventData.kind != 1) { return; // only kind 1 events are added to the tree @@ -121,6 +124,53 @@ class Tree { } } + Tree getTopTree(Tree t) { + + while( true) { + Tree? parent = allEvents[ t.e.eventData.getParent()]; + if( parent != null) { + t = parent; + } else { + break; + } + } + return t; + } + + void printThreads(List events) { + + // remove duplicate + Set temp = {}; + events.retainWhere((x) => temp.add(x.eventData.id)); + + stdout.write("\n\n\n\n\n\n---------------------------------------\nNotifications:"); + + if( events.isEmpty) { + stdout.write("No new replies/posts."); + return; + } + + stdout.write("Number of new replies/posts = ${events.length}\n"); + stdout.write("\nHere are the threads with new replies: \n\n"); + //events.forEach((element) {element.eventData.printEventData(0);}); + + events.forEach((element) { + // ignore if not in Tree + if( allEvents[element.eventData.id] == null) { + return; + } + if( element.eventData.kind != 1) { + return; // only type 1 are printed + } + Tree ?t = allEvents[element.eventData.id]; + if( t != null) { + Tree topTree = getTopTree(t); + topTree.printTree(0, false, 0); + print("\n"); + } + + }); + } /* * @getTagsFromEvent Searches for all events, and creates a json of e-tag type which can be sent with event * Also adds 'client' tag with application name. @@ -130,6 +180,7 @@ class Tree { String strTags = ""; if( replyToId.isEmpty) { + strTags += '["client","$clientName"]' ; return strTags; } @@ -148,19 +199,19 @@ class Tree { } } - print("latestEventId = $latestEventId"); + //print("latestEventId = $latestEventId"); if( latestEventId.isNotEmpty) { strTags = '["e","$latestEventId"]'; } - /* + if( strTags != "") { strTags += ","; } - strTags += '["client", "$clientName"]' ; - */ - print(strTags); + strTags += '["client","$clientName"]' ; + + //print(strTags); return strTags; }