printed notifications: threads with new replies are printed from their topmost comment

This commit is contained in:
vishalxl 2022-08-17 05:51:48 +05:30
parent 605fce8f63
commit dd5b80e2ab
2 changed files with 63 additions and 10 deletions

View File

@ -94,8 +94,9 @@ Future<void> 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<void> 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<void> main(List<String> 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

View File

@ -43,9 +43,12 @@ class Tree {
bool insertEvents(List<Event> newEvents) {
//print("In insertEvents num events: ${newEvents.length}");
List<String> 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<Event> 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;
}